ggplot2:具有点和填充分隔的箱线图 [英] ggplot2: Boxplots with points and fill separation
问题描述
我有一个可以通过两个分隔符分开的数据。一个是年份,第二个是字段特征。
box< -as.data.frame(1:36)
box $ year<-c(1996,1996,1996,1996,1996,1996,1996,1996,1996,
1997,1997,1997,1997,1997,1997,1997,1997,1997,
1996,1996,1996,1996,1996,1996,1996,1996,1996,
1997,1997,1997,1997,1997,1997,1997,1997,1997,1997)
盒年<-as.character(box $ year)
box $ case<-c(6.40,6.75,6.11,6.33,5.50,5.40,5.83,4.57,5.80,
6.00 ,6.11,6.40,7.00,NA,5.44,6.00,NA,6.00,
6.00,6.20,6.40,6.64,6.33,6.60,7.14,6.89,7.10,
6.73,6.27,6.64,6.41 ,6.42,6.17,6.05,5.89,5.82)
box $ code<-c( L, L, L, L, L ;, L, L, L, L, L, L, L,
L, L, , L, L, L, L, M, M , M, M, M, M,
M, M, M, M, M ;, M, M, M, M, M, M, M)
颜色<-系数(box $ code,标签= c(#F8766D,#00BFC4)))
在方框图中,我想在它们上面显示点,以查看数据如何分布。每年只需用一个箱形图即可轻松做到:
ggplot(box,aes(x = year,y = case,fill =" #F8766D))+
geom_boxplot(alpha = 0.80)+
geom_point(color =颜色,大小= 5)+
主题(text = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major .x = element_blank(),
legend.position = none)
但是随着我在其中添加填充参数,它变得更加复杂:
ggplot(box,aes(x =年,y =案例,填充=代码))+
geom_boxplot(alpha = 0.80)+
geom_point(颜色=颜色,大小= 5)+
主题(文本= element_text(大小= 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank() ,
legend.position =无)
I have a data which can be divaded via two seperators. One is year and second is a field characteristics.
box<-as.data.frame(1:36)
box$year <- c(1996,1996,1996,1996,1996,1996,1996,1996,1996,
1997,1997,1997,1997,1997,1997,1997,1997,1997,
1996,1996,1996,1996,1996,1996,1996,1996,1996,
1997,1997,1997,1997,1997,1997,1997,1997,1997)
box$year <- as.character(box$year)
box$case <- c(6.40,6.75,6.11,6.33,5.50,5.40,5.83,4.57,5.80,
6.00,6.11,6.40,7.00,NA,5.44,6.00, NA,6.00,
6.00,6.20,6.40,6.64,6.33,6.60,7.14,6.89,7.10,
6.73,6.27,6.64,6.41,6.42,6.17,6.05,5.89,5.82)
box$code <- c("L","L","L","L","L","L","L","L","L","L","L","L",
"L","L","L","L","L","L","M","M","M","M","M","M",
"M","M","M","M","M","M","M","M","M","M","M","M")
colour <- factor(box$code, labels = c("#F8766D", "#00BFC4"))
In boxplots, I want to display points over them, to see how data is distributed. That is easily done with one single boxplot for every year:
ggplot(box, aes(x = year, y = case, fill = "#F8766D")) +
geom_boxplot(alpha = 0.80) +
geom_point(colour = colour, size = 5) +
theme(text = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
legend.position = "none")
But it become more complicated as I add fill parameter in them:
ggplot(box, aes(x = year, y = case, fill = code)) +
geom_boxplot(alpha = 0.80) +
geom_point(colour = colour, size = 5) +
theme(text = element_text(size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
legend.position = "none")
And now the question: How to move these points to boxplot axes, where they belong? As blue points to blue boxplot and red to red one.
Like Henrik said, use position_jitterdodge()
and shape = 21
. You can clean up your code a bit too:
- No need to define box, then fill it piece by piece
You can let
ggplot
hash out the colors if you wish and skip constructing the colors factor. If you want to change the defaults, look intoscale_fill_manual
andscale_color_manual
.box <- data.frame(year = c(1996,1996,1996,1996,1996,1996,1996,1996,1996, 1997,1997,1997,1997,1997,1997,1997,1997,1997, 1996,1996,1996,1996,1996,1996,1996,1996,1996, 1997,1997,1997,1997,1997,1997,1997,1997,1997), case = c(6.40,6.75,6.11,6.33,5.50,5.40,5.83,4.57,5.80, 6.00,6.11,6.40,7.00,NA,5.44,6.00, NA,6.00, 6.00,6.20,6.40,6.64,6.33,6.60,7.14,6.89,7.10, 6.73,6.27,6.64,6.41,6.42,6.17,6.05,5.89,5.82), code = c("L","L","L","L","L","L","L","L","L","L","L","L", "L","L","L","L","L","L","M","M","M","M","M","M", "M","M","M","M","M","M","M","M","M","M","M","M")) ggplot(box, aes(x = factor(year), y = case, fill = code)) + geom_boxplot(alpha = 0.80) + geom_point(aes(fill = code), size = 5, shape = 21, position = position_jitterdodge()) + theme(text = element_text(size = 18), axis.title.x = element_blank(), axis.title.y = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), legend.position = "none")
这篇关于ggplot2:具有点和填充分隔的箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!