绘图意味着使用ggplot作为散点图的线图

时间:2014-09-25 12:50:30

标签: r ggplot2 mean

我有这个简单的数据框,每个因子(CT)有三个重复(值)。我想将它绘制为geom_point,而不是geom_line的点。

gene <- c("Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5")
value <- c(0.86443, 0.79032, 0.86517, 0.79782, 0.79439, 0.89221, 0.93071, 0.87170, 0.86488, 0.91133, 0.87202, 0.84028, 0.83242, 0.74016, 0.86656)
CT <- c("ET","ET","ET", "HP","HP","HP","HT","HT","HT", "LT","LT","LT","P","P","P")
df<- cbind(gene,value,CT)
df<- data.frame(df)

所以,我可以制作散点图。

ggplot(df, aes(x=CT, y=value)) + geom_point()

enter image description here

如何获得表示每个因子的均值的geom_line。我试过了stat_summary:

ggplot(df, aes(x=CT, y=value)) + geom_point() +
stat_summary(aes(y = value,group = CT), fun.y=mean, colour="red", geom="line")

但它不起作用。 “geom_path:每组只包含一个观察。你需要调整群体审美吗?”

但每组都有三个观察结果,出了什么问题?

聚苯乙烯。我也对顺畅的路线感兴趣。

2 个答案:

答案 0 :(得分:14)

您应该将group aes设置为1:

ggplot(df, aes(x=CT, y=value)) + geom_point() +
  stat_summary(aes(y = value,group=1), fun.y=mean, colour="red", geom="line",group=1)

enter image description here

答案 1 :(得分:0)

您可以使用dplyr包来获取每个因素的方法。

library(dplyr)
group_means <- df %>%
  group_by(CT) %>%
  summarise(mean = mean(value))

然后,您需要将因子转换为数字,以便使用geom_segment函数在图表上绘制线条。此外,scale_x_continuous功能可让您设置x轴的标签。

ggplot(df, aes(x=as.numeric(CT), y=value)) + geom_point() + 
  geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean), 
               data=group_means, colour="red") +
  scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT))

根据hrbrmstr的评论,您可以使用以下内容添加平滑线:

ggplot(df, aes(x=as.numeric(CT), y=value, group=1)) + geom_point() + 
  geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean), 
               data=group_means, colour="red") +
  scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT)) + 
  geom_smooth()