GGPLOT2 制作的一半箱线图
Half of the boxplot made in GGPLOT2
我正在使用 ggplot2 包绘制箱线图,但是,由于某些外部原因,只有一半的箱线图是为“控制”和“商业 IMD”处理制作的。
见下文,使用“boxplot”函数制作图表时,图表正常。
mediasCon = tapply(dados$CS, dados$Trat, mean)
boxplot(dados$CS ~ dados$Trat, data = dados, col="gray",
xlab = 'Tratamentos', ylab = 'Espermatozoides - Cabeça Solta')
points(1:3, mediasCon, col = 'Red', pch = 16)
然而,当使用 GGPLOT2 函数制作相同的图表时,看到前两次处理只完成了一半的图表,为什么会这样?
此外,如何使用 ggplot2 函数添加箱线图“尾部”?
library(ggplot2)
ggplot(data=dados, aes(x=Trat, y=CS)) + geom_boxplot(fill=c("#DEEBF7","#2171B5","#034E7B"),color="black") +
xlab('Tratamentos') +
ylab('Espermatozoides - Cabeça Solta') +
stat_summary(fun=mean, colour="black", geom="point",
shape=18, size=5) +
theme(axis.title = element_text(size = 20),
axis.text = element_text(size = 16))
如果您查看 ?geom_boxplot
下的帮助文件,您将看到:
The lower and upper hinges correspond to the first and third quartiles (the 25th and 75th percentiles). This differs slightly from the method used by the boxplot() function, and may be apparent with small samples. See boxplot.stats() for more information on how hinge positions are calculated for boxplot().
在你的情况下,IMD Commercial
的 4 个条目是 c(0, 1, 1, 1)
,这当然是一个小样本。
解决此问题的一种方法是使用 stat = "identity"
计算您希望铰链的位置并将该数据传递给 ggplot
。这使得代码有点复杂,但是当您尝试修改默认行为时通常会出现这种情况:
library(ggplot2)
library(dplyr)
dados %>%
group_by(Trat) %>%
summarize(median = median(CS), mean = mean(CS),
upper = quantile(CS, 0.75, type = 2),
lower = quantile(CS, 0.25, type = 2),
max = max(CS), min = min(CS)) %>%
ggplot(aes(x = Trat, y = mean, fill = Trat)) +
geom_boxplot(aes(ymin = min, lower = lower,
middle = median, upper = upper, ymax = max),
stat = "identity", color = "black") +
geom_point(size = 3, shape = 21, fill = "red") +
scale_fill_manual(values = c("#DEEBF7","#2171B5","#034E7B")) +
theme_classic() +
xlab('Tratamentos') +
ylab('Espermatozoides - Cabeça Solta')
我正在使用 ggplot2 包绘制箱线图,但是,由于某些外部原因,只有一半的箱线图是为“控制”和“商业 IMD”处理制作的。
见下文,使用“boxplot”函数制作图表时,图表正常。
mediasCon = tapply(dados$CS, dados$Trat, mean)
boxplot(dados$CS ~ dados$Trat, data = dados, col="gray",
xlab = 'Tratamentos', ylab = 'Espermatozoides - Cabeça Solta')
points(1:3, mediasCon, col = 'Red', pch = 16)
然而,当使用 GGPLOT2 函数制作相同的图表时,看到前两次处理只完成了一半的图表,为什么会这样?
此外,如何使用 ggplot2 函数添加箱线图“尾部”?
library(ggplot2)
ggplot(data=dados, aes(x=Trat, y=CS)) + geom_boxplot(fill=c("#DEEBF7","#2171B5","#034E7B"),color="black") +
xlab('Tratamentos') +
ylab('Espermatozoides - Cabeça Solta') +
stat_summary(fun=mean, colour="black", geom="point",
shape=18, size=5) +
theme(axis.title = element_text(size = 20),
axis.text = element_text(size = 16))
如果您查看 ?geom_boxplot
下的帮助文件,您将看到:
The lower and upper hinges correspond to the first and third quartiles (the 25th and 75th percentiles). This differs slightly from the method used by the boxplot() function, and may be apparent with small samples. See boxplot.stats() for more information on how hinge positions are calculated for boxplot().
在你的情况下,IMD Commercial
的 4 个条目是 c(0, 1, 1, 1)
,这当然是一个小样本。
解决此问题的一种方法是使用 stat = "identity"
计算您希望铰链的位置并将该数据传递给 ggplot
。这使得代码有点复杂,但是当您尝试修改默认行为时通常会出现这种情况:
library(ggplot2)
library(dplyr)
dados %>%
group_by(Trat) %>%
summarize(median = median(CS), mean = mean(CS),
upper = quantile(CS, 0.75, type = 2),
lower = quantile(CS, 0.25, type = 2),
max = max(CS), min = min(CS)) %>%
ggplot(aes(x = Trat, y = mean, fill = Trat)) +
geom_boxplot(aes(ymin = min, lower = lower,
middle = median, upper = upper, ymax = max),
stat = "identity", color = "black") +
geom_point(size = 3, shape = 21, fill = "red") +
scale_fill_manual(values = c("#DEEBF7","#2171B5","#034E7B")) +
theme_classic() +
xlab('Tratamentos') +
ylab('Espermatozoides - Cabeça Solta')