【问题标题】:Use two colour scales possible (with work around)?可能使用两种色标(解决方法)?
【发布时间】:2019-12-18 11:47:02
【问题描述】:

我正在尝试将 18 个实验地块中 2 个物种的昆虫数量绘制到一个图表上。由于第二个物种种群的高峰期较晚,因此在视觉上是可行的(见下图)。我希望物种 1 的 18 条种群为绿色(使用 RColorBrewer 的“Greens”),而物种 2 的 18 条种群为红色(使用“Reds”)。我确实意识到这对于色盲观众来说可能是个问题,但这在这里无关紧要。

我在这里读到,使用标准 ggplot2 选项是不可能的:R ggplot two color palette on the same plot 但这篇文章已有两年多的历史了。

积分有一个简短的“作弊”:Using two scale colour gradients ggplot2,但由于我更喜欢​​线条来显示时间的人口,所以我不能使用它。

是否有任何新的“秘籍”可用于此? 或者有没有人有另一个想法来可视化我的数据,以在所有图中显示人口趋势随时间变化并显示峰值时间的差异?我在底部添加了一张图片,显示了我的真实数据,但所有数据都采用相同的色标。

示例代码

# example data frame
plot <- as.factor(rep(c("A","B","C"),each=5))
time <- as.numeric(rep(c(1:5),times=3))
S1 <- c(1,4,7,5,2, 2,8,9,3,1, 1,6,6,3,1)
S2 <- c(0,0,2,3,2, 1,2,1,5,3, 0,1,1,6,7)
df <- data.frame(time, plot, S1, S2)

# example colour scales
S1Colours <- colorRampPalette(brewer.pal(9,"Greens"))(3)
S2Colours <- colorRampPalette(brewer.pal(9,"Reds"))(3)
names(S1Colours) <- levels(df$plot)
names(S2Colours) <- levels(df$plot)

# example plot
ggplot(data=df) +
  geom_line(aes(x=time, y=S1, colour=plot)) +
  geom_line(aes(x=time, y=S2, colour=plot)) +
  scale_colour_manual(name = "plot", values = S1Colours) +
  scale_colour_manual(name = "plot", values = S2Colours)
  # this gives the note "Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale."

绘制真实数据

【问题讨论】:

  • 也许您可以对同一组的 S1 和 S2 使用不同的线型?
  • 这确实是我之前尝试过的东西,但是因为我有 36 行有很多重叠,所以不太明显,这就是我喜欢不同颜色的原因。
  • 你试过手动色标吗?通过连接从浅绿色到深红色渐变中选择的 18 个值以及从浅红色到深红色渐变中选择的另外 18 个值来创建所需的主题? (我手头没有关于如何从两种颜色创建连续调色板然后从中采样 n 种颜色的说明,但这只是几行代码)。
  • 但是我会创建一个包含 36 个图的图例,并且可能需要将其中的 18 个重命名为“plotS2”或其他名称。虽然如果我只是剪掉传说并假设读者不一定对哪条线是哪条确切的情节感兴趣,那实际上可能是一个解决方案......我一定会试一试!

标签: r ggplot2 colors


【解决方案1】:

我也会为所有组合创建手动色标。

library(tidyverse)
library(RColorBrewer)
df_long=pivot_longer(df,cols=c(S1,S2),names_to = "Species",values_to = "counts") %>% # create long format and
  mutate(plot_Species=paste(plot,Species,sep="_")) # make identifiers for combined plot and Species 


#make color palette
mycolors=c(colorRampPalette(brewer.pal(9,"Greens"))(sum(grepl("S1",unique(df_long$plot_Species)))),
         colorRampPalette(brewer.pal(9,"Reds"))(sum(grepl("S2",unique(df_long$plot_Species)))))
names(mycolors)=c(grep("S1",unique(df_long$plot_Species),value = T),
                grep("S2",unique(df_long$plot_Species),value = T))


# example plot
ggplot(data=df_long) +
  geom_line(aes(x=time, y=counts, colour=plot_Species)) +
  scale_colour_manual(name = "Species by plot", values = mycolors)

【讨论】:

  • 谢谢!我学到了一些东西:-)并相应地编辑了我的答案
  • @Tingolfin 如果你想让图例按物种排序,只需在plot_Species=paste()语句中切换变量的顺序
【解决方案2】:

您可以使用ggnewscale 包轻松完成此操作(免责声明:我是作者)。

你会这样做:

library(RColorBrewer)
library(ggplot2)
library(ggnewscale)


plot <- as.factor(rep(c("A","B","C"),each=5))
time <- as.numeric(rep(c(1:5),times=3))
S1 <- c(1,4,7,5,2, 2,8,9,3,1, 1,6,6,3,1)
S2 <- c(0,0,2,3,2, 1,2,1,5,3, 0,1,1,6,7)
df <- data.frame(time, plot, S1, S2)

# example colour scales
S1Colours <- colorRampPalette(brewer.pal(9,"Greens"))(3)
S2Colours <- colorRampPalette(brewer.pal(9,"Reds"))(3)
names(S1Colours) <- levels(df$plot)
names(S2Colours) <- levels(df$plot)


ggplot(data=df) +
  geom_line(aes(x=time, y=S1, colour=plot)) +
  scale_colour_manual(name = "plot 1", values = S1Colours) +

  new_scale_color() +

  geom_line(aes(x=time, y=S2, colour=plot)) +
  scale_colour_manual(name = "plot 2", values = S2Colours)

由reprex package (v0.3.0) 于 2019 年 12 月 19 日创建

【讨论】:

  • 你的包裹非常好,恭喜!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多