R 如何在ggplot2中手动添加错误条和n=样本大小?

R 如何在ggplot2中手动添加错误条和n=样本大小?,r,ggplot2,R,Ggplot2,我试图绘制一个条形图,但我对这段代码有一些问题,我不确定代码中的问题是什么。1) 我想要一个带有平均值的条形图,在样本1和样本2下面还有n=12和n=15。2)在另一种情况下,我想要n=12,n=15以上的误差条在每个样本中或在条形图的中间。我得到了一些wierd输出。这是密码 survey <- data.frame(sample=rep(c("sample1","sample1", "sample1", "sample1", "sample1", "sample1", "sample1

我试图绘制一个条形图,但我对这段代码有一些问题,我不确定代码中的问题是什么。1) 我想要一个带有平均值的条形图,在样本1和样本2下面还有n=12和n=15。2)在另一种情况下,我想要n=12,n=15以上的误差条在每个样本中或在条形图的中间。我得到了一些wierd输出。这是密码

survey <- data.frame(sample=rep(c("sample1","sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2"),1), 
                 values=c(200, 100, 150, 175, 145, 50, 75, 60, 45, 56, 300, 200, 150, 100, 125, 25, 50, 75, 45, 35)) 
survey

survey$label = c("n=12", "n = 15") 
survey <- transform(survey, labelx = paste(sample, "\n", label)) 

ggplot(survey, aes(x=labelx, y = values, fill=sample)) + 
    geom_bar(stat="identity", position=position_dodge(width = 0.25),width=0.25)+ 
    stat_summary(geom = "bar", fun.y = mean, position = "dodge", width= 0.25) +
    stat_summary(geom = "errorbar", fun.data = mean_se, position = position_dodge2(0.9), width=0.1)+
    geom_text(aes(label = label), vjust = -1) 

survey我认为问题在于你对统计摘要和几何工具栏的使用。Geom_栏和Geom_文本不会使用stat_summary参数中的操纵数据(即y均值),这会导致绘制的值不正确。这和你要找的有点接近吗?(可以使用vjust调整条形标签的位置)

库(ggplot2)

谢谢你。但这里的问题是,我的数据显示在4列中。它应该只包含sample1和sample2,您是否可以在错误条上方或sample1和sample2下方使用n=12或n=15(即,不能同时使用两者)。没问题!:)为了更好地理解您的问题,值“n=12”和“n=15”是否分别对应于样本1和样本2?(即样本1:“n=12”,样本2:“n=15”)。如果是这样,用以下代码替换创建标签变量的行:
survey$label=ifelse(survey$sample==“sample1”、“n=12”、“n=15”)
应该可以解决这个问题。至于标签,如果我理解正确,您可以使用示例变量来标记x轴。也为格式问题道歉,我是新手!非常感谢。是的,值n=12”和“n=15”分别对应于sample1和sample2。我使用ifelse重新创建了绘图。它起作用了。此外,您还建议将n=12和n=15显示在错误栏上方或sample1和sample2下方(根据首选项,当我想要添加到任何我想要的位置时),您建议我可以使用样本变量来标记我的x轴。您能告诉我如何进行此修改吗?是的,因此您只需将ggplot代码的第一行中的x美学替换为:
ggplot(调查,aes(x=样本,y=值,fill=样本))
谢谢。我更改了x=sample,它在错误条上方的n=12和n=15下工作。但是当我将vjust=-1更改为x=0时,它显示了一些错误。这就是我试图将n=12和n=15保持在样本1和样本2下方的地方。下面是代码:ggplot(调查,aes(x=sample,y=values,fill=sample))+stat\u summary(geom=“bar“,fun.y=mean,position=“dodge”,width=0.25)+统计摘要(geom=“errorbar”,fun.data=mean\u se,position=position\u dodge2(0.9),width=0.1)+统计摘要(aes(label=label),fun.y=mean,geom=“text”,size=3,x=0)
library(ggplot2)
survey <- data.frame(sample=rep(c("sample1","sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample1", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2", "sample2"),1), 
                     values=c(200, 100, 150, 175, 145, 50, 75, 60, 45, 56, 300, 200, 150, 100, 125, 25, 50, 75, 45, 35)) 
survey

survey$label = c("n = 12", "n = 15") 
survey <- transform(survey, labelx = paste(sample, "\n", label)) 

ggplot(survey, aes(x=labelx, y = values, fill=sample)) + 
  stat_summary(geom = "bar", fun.y = mean, position = "dodge", width= 0.25) +
  stat_summary(geom = "errorbar", fun.data = mean_se, position = position_dodge2(0.9), width=0.1) +
  stat_summary(aes(label = label), fun.y = mean, geom = "text", size = 3,
               vjust = -1)