首页 > 解决方案 > 从 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

标签: rggplot2colors

解决方案


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

在此处输入图像描述


推荐阅读