根据特定因素规定相同的颜色图
Prescribe same color map based on specific factors
我在 ggplot2 中进行颜色映射时遇到了一些困难。四个地块中的两个具有相同的六个县变量。但是,"Carbon Monoxide" 和 "Coarse Particulate matter" 子图只有五个(皮尔斯县缺失)。
每种污染物在 y-axis 上都有不同的单位,在单个 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)
感谢评论的一些帮助,我已经设法产生了预期的结果。这比我想象的要容易得多。 hue_pal()
这个功能以后肯定对我有用。谢谢大家!
library(scales)
## determine what the default color palette is for ggplot2 with 6 factor levels:
hue_pal()(6)
#[1] "#F8766D" "#B79F00" "#00BA38" "#00BFC4" "#619CFF" "#F564E3"
## next we need to create a custom color palette using these six colors:
cols <- c("Ada" = "#F8766D", "Bernalillo" = "#B79F00",
"Multnomah" = "#00BA38", "Pierce" = "#00BFC4",
"Sacramento" = "#619CFF", "Salt Lake" = "#F564E3")
## I'm mapping each color to the specific factor level (County Name)
## This ensures that each county maintains the same color throughout each plot
## regardless if there are 5 or 6 factor levels!
p1 <- ggplot(filter(df, pollutant == "Nitrogen dioxide"),
aes(x = as.factor(season),
y = value,
fill = as.factor(county))) +
geom_boxplot(position = "dodge") +
## here's where we'll add our custom color palette:
scale_fill_manual(values = cols)
p1
我在 ggplot2 中进行颜色映射时遇到了一些困难。四个地块中的两个具有相同的六个县变量。但是,"Carbon Monoxide" 和 "Coarse Particulate matter" 子图只有五个(皮尔斯县缺失)。
每种污染物在 y-axis 上都有不同的单位,在单个 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)
感谢评论的一些帮助,我已经设法产生了预期的结果。这比我想象的要容易得多。 hue_pal()
这个功能以后肯定对我有用。谢谢大家!
library(scales)
## determine what the default color palette is for ggplot2 with 6 factor levels:
hue_pal()(6)
#[1] "#F8766D" "#B79F00" "#00BA38" "#00BFC4" "#619CFF" "#F564E3"
## next we need to create a custom color palette using these six colors:
cols <- c("Ada" = "#F8766D", "Bernalillo" = "#B79F00",
"Multnomah" = "#00BA38", "Pierce" = "#00BFC4",
"Sacramento" = "#619CFF", "Salt Lake" = "#F564E3")
## I'm mapping each color to the specific factor level (County Name)
## This ensures that each county maintains the same color throughout each plot
## regardless if there are 5 or 6 factor levels!
p1 <- ggplot(filter(df, pollutant == "Nitrogen dioxide"),
aes(x = as.factor(season),
y = value,
fill = as.factor(county))) +
geom_boxplot(position = "dodge") +
## here's where we'll add our custom color palette:
scale_fill_manual(values = cols)
p1