简体   繁体   English

从ggplot中的ggtheme主题访问颜色

[英]accessing colors from a ggtheme theme in ggplot

I have a pretty nice little plot I've made with ggplot and it looks great. 我用ggplot做了一个非常漂亮的小情节,它看起来很棒。 I have some bars and then some crossbars. 我有一些酒吧,然后是一些横杆。 I'm using the theme_economist() from the ggthemes package and I'd like to make the bars all one color from that theme and the crossbars a contrasting color. 我正在使用ggthemes包中的theme_economist() ,我想从这个主题中选择一种颜色的条形图,而横杆则是对比色。 But I can't figure out how to reach into the theme and grab out a couple of colors for these elements. 但我无法弄清楚如何进入主题并为这些元素抓出几种颜色。 I can change them to a named color and I can change them to a specific hex color, but it seems like I should be able to reach into the theme and say, "gimme two contrasting colors from this theme!" 我可以将它们更改为命名颜色,我可以将它们更改为特定的十六进制颜色,但似乎我应该能够进入主题并说“从这个主题中给出两种截然不同的颜色!” How do I do that? 我怎么做?

Here's a reprex showing what I have... 这是一个代表我所拥有的......

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 includes a list object ggthemes_data with various palettes and other data used by the package (see below). ggthemes包含一个列表对象ggthemes_data其中包含各种调色板和包使用的其他数据(见下文)。 You could choose from among those colors. 您可以从这些颜色中进行选择。

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" 

In addition, as noted by the commenters, you can generate palettes with economist_pal() , for example, economist_pal()(2) or economist_pal(stata=TRUE)(3) . 此外,如评论者所述,您可以使用economist_pal()生成调色板,例如, economist_pal(stata=TRUE)(3) economist_pal()(2)economist_pal(stata=TRUE)(3)

library(scales)

show_col(economist_pal()(9))

在此输入图像描述

show_col(economist_pal(stata=TRUE)(9))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM