从ggplot中的ggtheme主题访问颜色

JD *_*ong 4 r colors ggplot2

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

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

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
Run Code Online (Sandbox Code Playgroud)

eip*_*i10 7

ggthemes包括一个列表对象,ggthemes_data包含各种调色板和包使用的其他数据(见下文).您可以从这些颜色中进行选择.

library(ggthemes)

ggthemes_data$economist
Run Code Online (Sandbox Code Playgroud)
$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"
Run Code Online (Sandbox Code Playgroud)

此外,正如评论者所指出的,您可以使用economist_pal()例如economist_pal()(2)或生成调色板economist_pal(stata=TRUE)(3).

library(scales)

show_col(economist_pal()(9))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

show_col(economist_pal(stata=TRUE)(9))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述