ggplot boxplot中的连接点 [英] Connected points in ggplot boxplot

查看:86
本文介绍了ggplot boxplot中的连接点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的箱形图,该箱形图的连接线类似于此问题中所述:

解决方案

有些变化类似于我的



(其余垂直线来自箱线图。)


I'm trying to create a simple boxplot with connected lines similar to the one described in this question: Connect ggplot boxplots using lines and multiple factor. However, the interaction term in that example produces an error:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

I would like to connect each point using the index variable. Here is the code:

group <- c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")
session <- c("one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two")
value <- c(1.02375,1.01425,1.00505,0.98105,1.09345,1.09495,0.98255,0.90240,0.99185,0.99855,0.88135,0.72685,0.94275,0.84775,1.01010,0.96825,0.85215,0.84175,0.89145,0.86985)
index <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10)
df <- data.frame(group,session,value,index)

# Graph plots
p <- ggplot(df, aes(x=group, y=value, fill=session))
p <- p + geom_boxplot(color="grey40", outlier.alpha=0.0) #alpha=0.6
p <- p + stat_summary(fun.y=mean,geom="point",pch="-",color="white",size=8, position = position_dodge(width=0.75)) # size=2 color="black"
p <- p + geom_point(size=2, alpha=0.6, aes(group=session), data=df, position = position_dodge(width=0.75))
p <- p + geom_line(aes(group = index), alpha = 0.6, colour = "black", position = position_dodge(width=0.75), data=df) #
p <- p + scale_fill_manual(values=c("#969696","#74c476"))
p <- p + theme(
       axis.text.x = element_text(colour = "black"), #angle = 60, hjust = 1
       axis.text.y = element_text(colour = "black"),
       axis.title.x = element_blank(), #element_text(colour = "black"),
       axis.title.y = element_text(colour = "black"),
       legend.position = "none"
       #panel.background = element_blank(), #element_rect(fill="white", colour="black", size=2),
       #panel.grid.major = element_blank(),
       #panel.grid.minor = element_blank(),
       #panel.border = element_blank(),
       #axis.line = element_line(size=1.5, colour = "black")
       #panel.grid.major = element_line(size = .5, colour = "grey")
       )
ggsave("~/Desktop/test.pdf", width=4, height=6, units=c("in"), plot=p)

However, that produces only vertical lines as in this image:

解决方案

Some changes analogous as in my other answer:

df <- data.frame(group, session, value, index, U = interaction(session, group))
p <- ggplot(df, aes(x = U, y = value, fill = session)) + 
  scale_x_discrete(labels = rep(unique(group), each = 2))
p <- p + geom_line(aes(group = index), alpha = 0.6, colour = "black", data = df) 
                                                             # no need for dodge

The rest is the same as in your code.

(The remaining vertical lines are from the boxplot.)

这篇关于ggplot boxplot中的连接点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆