绘图矩阵中按列显示的面板背景颜色,ggplot2

Panel background color by column in plot matrix, ggplot2

我想制作一个图表矩阵,背景颜色编码为与特定列相对应(即,我想要我想要的颜色,并按特定顺序排列)。


我使用这里的信息 Conditionally change panel background with facet_grid? 想出了一种方法,在 ggplot2 中的图表矩阵中按列对背景颜色进行颜色编码,运行 在 RStudio 中。

但是,以前我一直在使用一些色盲颜色(虽然我不是色盲),但我终其一生都无法将这些颜色更改为一组新颜色,即使我从 RStudio 的全局环境中清除对象,然后在新的 RStudio 会话中重新打开 R 脚本。不知何故,旧的颜色还记得。

我看到的输出(作为 JPG 图像文件)是 https://drive.google.com/file/d/1I_5GJHOW8VPZShJ4C61Uayo8zrEMPCDZ/ - 当 运行 我的代码时你肯定会得到其他颜色。

两个问题:

  1. 我怎样才能将那些 colors/assign 特定颜色控制到 climate$elevation 水平?
  2. 我使用的方法似乎在绘图中掩盖了我的网格线 - 在我的代码底部,您可以看到我尝试使用 'theme' 添加网格线,但它不起作用,或者落后海拔高度的颜色。有更好的方法吗?

这是我的代码:

# install.packages("ggplot2")
library(ggplot2)

#### create Data ####
#####################
temperature<-c(12,43,15,8,7,9,12,15,14,10,34,27,10,12,33,11,15,7,9,12,25,14,9,10,34,37,10,12,33,15,8,7,9,12,15,14,10,34,37,10,12,33,11,15,7,9,12,15,14,9,10,34,37,10,12,33,15,8,7,9,12,15,14,10,34,37,10,12,33,11,15,7,9,12,15,14,9,10,34,37,10,12,15,14,9,36,10,34,37,10)
hour<-c(2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12)
elevation<-c("Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast")
region<-c("Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America")
climate<-data.frame(region,elevation,hour,temperature) # create data frame

# Ensures sequence corresponds to sequence of cave zones
climate$elevation <-factor(climate$elevation,
                      levels = c('Alpine','Montane','Steppe','Valley','Coast'),order=TRUE)

# create plot matrix, background colored by elevation
base<-ggplot(data = climate, aes(hour, temperature)) +
  geom_rect(aes(fill = elevation),xmin = -Inf,xmax = Inf, ymin = -Inf,ymax = Inf,alpha = 0.3) +
  geom_line(color = "steelblue", size = 1) +
  geom_point(color="steelblue") +
  labs(title = "Elevation by Region & Time of Day",
       subtitle = "(Temperature)",
       y = "Mean Temperature", x = "Hour of Day") + 
  facet_grid(region ~ elevation)
base

###Plot looks like this to me:
https://drive.google.com/file/d/1I_5GJHOW8VPZShJ4C61Uayo8zrEMPCDZ/
# I'm certain colors will look different for you!

# Trying to add gridlines (does not work)
base + theme(
  panel.background = element_rect(fill = "transparent",colour = NA),
  plot.background = element_rect(fill = "transparent",colour = NA),
  panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                  colour = "white"), 
  panel.grid.minor = element_blank()
)

其实我和你的颜色一样。原因是你的 elevation 是一个有序的因素。因此 ggplot2 默认使用 viridis 调色板。

  1. 您可以使用 scale_fill_manual 设置填充颜色,例如命名的颜色向量。

  2. 网格线有两种选择。您可以简单地使用 geom_h/vline 手动添加网格线。或者您可以将绘图和面板背景的 fill 设置为 NA 并使用 panel.ontop 将在绘图顶部绘制面板和网格线。

library(ggplot2)

cols <- rev(scales::hue_pal()(5))
cols <- setNames(cols, levels(climate$elevation))
cols
#>    Alpine   Montane    Steppe    Valley     Coast 
#> "#E76BF3" "#00B0F6" "#00BF7D" "#A3A500" "#F8766D"

base <- ggplot(data = climate, aes(hour, temperature)) +
  geom_rect(aes(fill = elevation), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) +
  scale_fill_manual(values = cols) +
  geom_line(color = "steelblue", size = 1) +
  geom_point(color = "steelblue") +
  labs(
    title = "Elevation by Region & Time of Day",
    subtitle = "(Temperature)",
    y = "Mean Temperature", x = "Hour of Day"
  ) +
  facet_grid(region ~ elevation)

breaks_x <- seq(2.5, 10, 2.5)
breaks_y <- seq(10, 40, 10)

base +
  geom_vline(xintercept = breaks_x, color = "white", size = 0.5) +
  geom_hline(yintercept = breaks_y, color = "white", size = 0.5) +
  scale_x_continuous(breaks = breaks_x) +
  scale_y_continuous(breaks = breaks_y)

# Trying to add gridlines (does not work)
base + theme(
  panel.background = element_rect(fill = NA, colour = NA),
  plot.background = element_rect(fill = NA, colour = NA),
  panel.grid.major = element_line(
    size = 0.5, linetype = "solid",
    colour = "white"
  ),
  panel.ontop = TRUE,
  panel.grid.minor = element_blank()
)