【发布时间】: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”或其他名称。虽然如果我只是剪掉传说并假设读者不一定对哪条线是哪条确切的情节感兴趣,那实际上可能是一个解决方案......我一定会试一试!