美文网首页
plotmeans图,用ggplot2这样画

plotmeans图,用ggplot2这样画

作者: 小洁忘了怎么分身 | 来源:发表于2020-04-10 23:56 被阅读0次

花花写于2020年4月10日,听一位云南农大的学生说,今天陈疏影老师在生信课堂上讲到了豆豆写的简书教程,并推荐同学们关注我们的公众号,在这里我们对陈老师表示衷心的感谢吖~

最近我在学统计,学到方差分析的R语言实现,但是还没有整理成文。今天应该豆豆写推文的,但他又又又又又又又睡了!!!所以刚讲课讲了三小时的我,并不疲惫(大概是耐受性增强了吧),我来顶班了。先玩一个我擅长的画图好了。

gplot的plotmeans


这张图可以直观的展示各组数据的均值和置信区间,并显示了每组的样本量。可以用ggplot2画的更好看一些。

仿画思路

1.直接搜索!找ggplot2版的plotmeans,找到了,但是现成的代码不包含样本数。
2.但是可以找到带有每组样本数的箱线图代码!两个合起来就好啦

代码实现

01.带有每组样本数的箱线图

library(tidyverse)
x = group_by(iris,Species) %>%summarise(mean = mean(Sepal.Length))

library(dplyr)
data <- data.frame(
  name=c( rep("A",500), rep("B",500), rep("B",500), rep("C",20), rep('D', 100)  ),
  value=c( rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(500, 18, 1), rnorm(20, 25, 4), rnorm(100, 12, 1) )
)

sample_size = data %>% group_by(name) %>% summarize(num=dplyr::n())
# Plot
data %>%
  left_join(sample_size) %>%
  mutate(myaxis = paste0(name, "\n", "n=", num)) %>%
  ggplot( aes(x=myaxis, y=value, fill=name)) +
  geom_boxplot() +
  xlab("")

2.ggplot2版的plotmeans(不含样本数)

library(Rmisc)
tgc <- summarySE(ToothGrowth, measurevar="len", groupvars=c("supp","dose"))
tgc
pd <- position_dodge(0.1) # move them .05 to the left and right
tg = ToothGrowth

ggplot(tgc, aes(x=dose, y=len, colour=supp, group=supp)) + 
  geom_errorbar(aes(ymin=len-se, ymax=len+se), colour="black", width=.1, position=pd) +
  geom_line(position=pd) +
  geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle
  xlab("Dose (mg)") +
  ylab("Tooth length") +
  scale_colour_hue(name="Supplement type",    # Legend label, use darker colors
                   breaks=c("OJ", "VC"),
                   labels=c("Orange juice", "Ascorbic acid"),
                   l=40) +                    # Use darker colors, lightness=40
  ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs") +
  expand_limits(y=0) +                        # Expand y range
  scale_y_continuous(breaks=0:20*4) +         # Set tick every 4
  theme_bw() +
  theme(legend.justification=c(1,0),
        legend.position=c(1,0))               # Position legend in bottom right

3.二者结合

library(Rmisc)
tgc <- summarySE(ToothGrowth, measurevar="len", groupvars=c("supp","dose"))
tgc
pd <- position_dodge(0.1) # move them .05 to the left and right
tg = ToothGrowth

sample_size = dplyr::count(ToothGrowth,dose,name = "num")
tgc %>%
  left_join(sample_size) %>%
  mutate(myaxis = paste0(dose, "\n", "n=", num)) %>%
  ggplot(aes(x=myaxis, y=len, colour=supp, group=supp)) + 
  geom_errorbar(aes(ymin=len-se, ymax=len+se), colour="black", width=.1, position=pd) +
  geom_line(position=pd) +
  geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle                   # Use darker colors, lightness=40
  expand_limits(y=0) +                        # Expand y range
  scale_y_continuous(breaks=0:20*4) +         # Set tick every 4
  theme_bw() +
  theme(legend.justification=c(1,0),
        legend.position=c(1,0))               # Position legend in bottom right

相关文章

网友评论

      本文标题:plotmeans图,用ggplot2这样画

      本文链接:https://www.haomeiwen.com/subject/xaygphtx.html