R 如何根据分组值而不是ggplot中的填充值更改颜色?
我想根据方框图的分组值或单独更改方框图的颜色,而不是ggplot中的填充值。我该怎么做?我需要定义fill变量,以便能够将组细分为类型,就像我想要的那样,但是我似乎无法再控制颜色了 以下是一些示例数据以及我如何定义绘图:R 如何根据分组值而不是ggplot中的填充值更改颜色?,r,ggplot2,colors,boxplot,fill,R,Ggplot2,Colors,Boxplot,Fill,我想根据方框图的分组值或单独更改方框图的颜色,而不是ggplot中的填充值。我该怎么做?我需要定义fill变量,以便能够将组细分为类型,就像我想要的那样,但是我似乎无法再控制颜色了 以下是一些示例数据以及我如何定义绘图: library(ggplot2) data <- data.frame( id = rep(1:120), group = as.factor(rep(1:3, times = 40)),
library(ggplot2)
data <- data.frame( id = rep(1:120),
group = as.factor(rep(1:3, times = 40)),
type = as.factor(rep(1:2, times = 60)),
value = rnorm(120)+1)
ggplot(data, aes(x = group, y = value, fill = type)) +
geom_boxplot(aes(fill = type)) +
geom_point(aes(fill = type), position = position_jitterdodge(dodge.width = 0.65, jitter.width = 0.1, jitter.height = 0.1))
有什么建议吗?我们通过“组”、“类型”的
交互创建一个新列,并在比例填充手册中使用该列
library(dplyr)
library(ggplot2)
data <- data %>%
mutate(grptype = interaction(group, type))
gg <- ggplot(data, aes(x = group, y = value, fill = grptype)) +
geom_boxplot(aes(fill = grptype)) +
geom_point(aes(fill = grptype), position = position_jitterdodge(dodge.width = 0.65, jitter.width = 0.1, jitter.height = 0.1))
colors <- c("#b76e79", "#80a3dd",
"#a5bf9f", "#e3f0cd",
"#8a9a5b", "#ffdead")
gg +
scale_fill_manual(name="grptype",
labels = levels(data$grptype),
values = setNames(colors, levels(data$grptype))) +
theme(legend.title = element_text(size=12, color = "black", face="bold"),
legend.justification=c(0,1),
legend.position=c(0.05, 0.95),
legend.background = element_blank(),
legend.key = element_blank())
库(dplyr)
图书馆(GG2)
数据%
突变(grptype=相互作用(组,类型))
游戏打得好
ggplot(data, aes(x = group, y = value, fill = type)) +
geom_boxplot(aes(fill = type)) +
geom_point(aes(fill = type), position = position_jitterdodge(dodge.width = 0.65, jitter.width = 0.1, jitter.height = 0.1)) +
scale_fill_manual(values = colors)
library(dplyr)
library(ggplot2)
data <- data %>%
mutate(grptype = interaction(group, type))
gg <- ggplot(data, aes(x = group, y = value, fill = grptype)) +
geom_boxplot(aes(fill = grptype)) +
geom_point(aes(fill = grptype), position = position_jitterdodge(dodge.width = 0.65, jitter.width = 0.1, jitter.height = 0.1))
colors <- c("#b76e79", "#80a3dd",
"#a5bf9f", "#e3f0cd",
"#8a9a5b", "#ffdead")
gg +
scale_fill_manual(name="grptype",
labels = levels(data$grptype),
values = setNames(colors, levels(data$grptype))) +
theme(legend.title = element_text(size=12, color = "black", face="bold"),
legend.justification=c(0,1),
legend.position=c(0.05, 0.95),
legend.background = element_blank(),
legend.key = element_blank())