r - 从 ggplot 中的 ggtheme 主题访问颜色
问题描述
我用 ggplot 制作了一个非常漂亮的小情节,看起来很棒。我有一些横杆,然后是一些横杆。我正在使用theme_economist()
包装中的ggthemes
,我想将条形设为该主题中的一种颜色,并将横条设为对比色。但我不知道如何进入主题并为这些元素挑选几种颜色。我可以将它们更改为指定颜色,也可以将它们更改为特定的十六进制颜色,但似乎我应该能够进入主题并说,“给我这个主题的两种对比色!” 我怎么做?
这是一个显示我所拥有的reprex...
library(tidyverse)
library(ggthemes)
prices <- data.frame(year=2001:2010,
price=rnorm(10))
additional_junk <- data.frame(year=2001:2010,
thing=rnorm(10))
g_price <- ggplot() + theme_economist() +
scale_fill_economist() +
scale_colour_economist() +
geom_bar(aes(y = price , x = year),
data = prices, stat="identity") +
geom_crossbar(data=additional_junk, aes(x=year, y=thing,
ymin=0, ymax=0)
)
g_price
解决方案
ggthemes
包括一个列表对象,其中包含ggthemes_data
包使用的各种调色板和其他数据(见下文)。您可以从这些颜色中进行选择。
library(ggthemes)
ggthemes_data$economist
$bg ebg edkbg red ltgray dkgray "#d5e4eb" "#c3d6df" "#ed111a" "#ebebeb" "#c9c9c9" $fg blue_gray blue_dark green_light blue_mid blue_light green_dark gray blue_light red_dark red_light "#6794a7" "#014d64" "#76c0c1" "#01a2d9" "#7ad2f6" "#00887d" "#adadad" "#7bd3f6" "#7c260b" "#ee8f71" green_light brown "#76c0c1" "#a18376" $stata $stata$bg ebg edkbg "#C6D3DF" "#B2BFCB" $stata$fg edkblue emidblue eltblue emerald erose ebblue eltgreen stone navy maroon brown lavender "#3E647D" "#7B92A8" "#82C0E9" "#2D6D66" "#BFA19C" "#008BBC" "#97B6B0" "#D7D29E" "#1A476F" "#90353B" "#9C8847" "#938DD2" teal cranberry khaki "#6E8E84" "#C10534" "#CAC27E"
此外,正如评论者所指出的,您可以使用economist_pal()
例如economist_pal()(2)
或生成调色板economist_pal(stata=TRUE)(3)
。
library(scales)
show_col(economist_pal()(9))
show_col(economist_pal(stata=TRUE)(9))
推荐阅读
- git - 如何在不授予对 gitlab 中的 repo 访问权限的情况下向用户授予拉取请求权
- c# - 在 Azure 上部署的 Web 无法访问 .less 文件(错误代码 404)。本地 IIS 部署正常工作
- git - 变基后,我的功能分支上的 master 的长时间提交和文件更改
- javascript - 尽管路径明显正确,如何修复 Node.js 文件路径错误?
- javascript - 在 React 组件中设置多个 Google Map 引脚
- html - 我有一个 html 内容,它占用了页面的完整宽度,但在 PDF 中它只占用了 50% 的宽度
- android - Android将listview数据转换为pdf文件并保存
- angular - Angular File Input 所选文件未显示
- mips - MIPS 嵌套过程
- java - 交易块之外会发生什么?