【发布时间】:2017-11-30 00:48:07
【问题描述】:
我在 ggplot2 中进行颜色映射时遇到了一些困难。四个地块中有两个具有相同的六个县变量。但是,“一氧化碳”和“粗颗粒物”子图只有五个(缺少皮尔斯县)。
每种污染物在 y 轴上都有不同的单位,在单个 ggplot 中标记各个方面是一项挑战。为了解决这个问题,我分别生成每个图,然后使用gridExtra 将它们组合在一起。
是否可以将来自六个县的三个地块的相同颜色映射应用于一氧化碳地块?
这是我如何制作单个情节的示例:
library(ggplot2)
library(dplyr)
library(gridExtra)
p1 <- ggplot(filter(df, pollutant == "Nitrogen dioxide"), aes(x = as.factor(season), y = value, fill = as.factor(county))) +
geom_boxplot(position = "dodge")
## I'm excluding a bunch of code to format the font and size of the title/legend/axis labels
g <- grid.extra(p1, p2, p3, p4)
【问题讨论】:
-
使用
scale_color_manual为每个县分配相同的颜色,无论特定地块中是否存在特定县。 Here's an example. -
在对 6 个因子水平使用
scale_color_manual时,有没有办法分配 ggplot2 的默认调色板? -
library(scales)然后hue_pal()(6)应该给出默认ggplot2调色板的前6种颜色。 -
非常感谢!我一直想知道如何做到这一点......