从ggplot中的ggtheme主题访问颜色

时间:2018-05-25 21:46:56

标签: r ggplot2 colors

我有一个非常好的小情节,我用ggplot制作,看起来很棒。我有一些酒吧,然后是一些横杆。我正在使用theme_economist()包中的ggthemes,我希望这些主题中的条形颜色和横杆都是对比色。但我无法弄清楚如何进入主题并为这些元素攫取几种颜色。我可以将它们更改为命名颜色,我可以将它们更改为特定的十六进制颜色,但似乎我应该能够进入主题并说出,"从这个主题中给出两种截然不同的颜色!&#34 ;我该怎么做?

这里有一个代表我所拥有的......

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

1 个答案:

答案 0 :(得分:7)

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))

enter image description here

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

enter image description here

相关问题