在R中设置ggpubr程序包中的误差线值

0lvr5msh  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(167)

我有误差线的值,我想在“ggpubr”中指定值。看起来adderror.plot函数有很多可能性(例如,“mean_sd”),但是我找不到任何允许我自己指定值的东西。但它不能正常工作。我知道下次我将使用ggplot2以获得灵活性。
示例代码-

df <- data.frame(stringsAsFactors = FALSE, "pse" = c(40, 42, 41, 40, 60, 61, 62, 60, 39, 38, 40, 39, 59, 58, 60, 59 ))

df[1:4,2]="30 cm"
df[5:8,2]="60 cm"
df[9:12,2]="30 cm"
df[13:16,2]="60 cm"
df[1:8,3] = "3.5 cm"
df[9:16,3] = "6.5 cm"
colnames(df)[2]="Size"
colnames(df)[3]="Distance"

my_comparisons <- list( c("Near", "Far"))
ggbarplot(df, x = "Size", y = "pse", fill ="Distance", color = "Distance", ylim=c(25,75), width = 0.6, add = c("mean_se", "jitter"), palette = c("#000000", "#111111"),
position = position_dodge(0.65))+
theme(legend.position = "top")+ theme_bw() + theme(axis.text=element_text(size=14),axis.title=element_text(size=14))+ scale_fill_grey(start=0.8, end=0.95)+ theme(legend.position = "top")+ ylab ("PSE (mm)")[![enter image description here][1]][1]

第一个字符

e5njpo68

e5njpo681#

library(ggpubr)

df <- data.frame(stringsAsFactors = FALSE, "pse" = c(40, 42, 41, 40, 60, 61, 62, 60, 39, 38, 40, 39, 59, 58, 60, 59 ))

df[1:4,2]="30 cm"
df[5:8,2]="60 cm"
df[9:12,2]="30 cm"
df[13:16,2]="60 cm"
df[1:8,3] = "3.5 cm"
df[9:16,3] = "6.5 cm"
colnames(df)[2]="Size"
colnames(df)[3]="Distance"

mean_30_3.5 <- mean(df$pse[df$Size == "30 cm" & df$Distance == "3.5 cm"])
mean_30_6.5 <- mean(df$pse[df$Size == "30 cm" & df$Distance == "6.5 cm"])

mean_60_3.5 <- mean(df$pse[df$Size == "60 cm" & df$Distance == "3.5 cm"])
mean_60_6.5 <- mean(df$pse[df$Size == "60 cm" & df$Distance == "6.5 cm"])

my_comparisons <- list( c("Near", "Far"))
ggbarplot(df, x = "Size", y = "pse", fill ="Distance",color = "Distance", ylim=c(25,75),label = F, width = 0.6, add = c("mean_se", "jitter"),
 palette = c("#000000", "#111111"),
          position = position_dodge(0.65))+
  theme(legend.position = "top")+ theme_bw() + theme(axis.text=element_text(size=14),axis.title=element_text(size=14))+ 
scale_fill_grey(start=0.8, end=0.95)+ 
  theme(legend.position = "top")+ ylab ("PSE (mm)") +
  annotate("text", x = 0.85, y = mean_30_3.5 + 3, label = "your_value1")+
  annotate("text", x = 1.15, y = mean_30_6.5 + 3, label = "your_value2")+
  annotate("text", x = 1.85, y = mean_60_3.5 + 3, label = "your_value3")+
  annotate("text", x = 2.15, y = mean_60_6.5 + 3, label = "your_value4")

你的意思是这样的吗?:

rvpgvaaj

rvpgvaaj2#

谢谢!我也找到了一个不同的解决方案。在这里分享。

data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

下一个

df2 <- data_summary(x, varname="PSE", 
                    groupnames=c("Size", "Distance"))

df2$Size=as.factor(df2$Size)

my_comparisons <- list( c("Near", "Far"))
ggbarplot(x, x = "Size", y = "PSE", fill ="Distance",  color = "Distance", 
ylim=c(25,75), width = 0.6, add = c( "mean", "jitter"), palette = c("#000000", 
"#111111"),
position = position_dodge(0.65))+ theme_bw() 
+theme(axis.text=element_text(size=14),axis.title=element_text(size=14))+ 
scale_fill_grey(start=0.8, end=0.95)+ theme(legend.position = "top")+ ylab ("PSE 
(mm)")+geom_errorbar(data=df2, mapping=aes(x=Size, y=PSE, color=Distance, ymin=PSE- 
0.32, ymax=PSE+0.32), width=.15, position=position_dodge(.6))

相关问题