首页 > 解决方案 > 如果我使用交互图,如何在 GGplot2 中添加误差线

问题描述

亲爱的 StackOverflow 人,

我想在交互图中添加误差线。没有错误条码,代码运行良好。但是,如果我添加错误条码,我将收到以下消息:

Drucker(Confidence) 警告消息:1:在 Ops.factor(z, se) 中:'-' 对因素没有意义 2:在 Ops.factor(z, se) 中:'+' 对因素没有意义 3:删除 668 行包含缺失值(geom_errorbar)。

如果我将变量转换为数字,我会收到以下消息

z - se 中的 Drucker(x$Intelligence) 错误:二元运算符的非数字参数

这是我的代码 + dput(head(x, 20)) 的输出(在评论中要求)

##Prepare Data
x = ForkinDatenFlamingo

##UV
#Small
Posture= factor(x$Pos)
Sex= factor(x$Sex)

View(x)

contrasts(Posture) <- -1*contr.sum(2)
contrasts(Sex) <- -1*contr.sum(2)

##Fixed


Intelligence = factor(x$Intelligence)

#######################
Drucker <- 
  function(z) {
    library(ggplot2)  
    # library(FSA)
    name1 <- deparse(substitute(z))
    filename <- sprintf("Plot%s.png", gsub(" ", "_", name1))
    p<-(ggplot() + aes(x = Posture, color = Sex, group = Sex, y = z) +
          stat_summary(fun.y = mean, geom = "point") +
          stat_summary(fun.y = mean, geom = "line") + coord_cartesian(ylim = c(1.5, 3.5)) +
          labs(title = name1, x="Physician's posture") +
          ylab (name1))+ 
      #geom_errorbar(aes(ymin=z-se, ymax=z+se), width=.2) +

      scale_y_discrete(breaks=c("1","2","3","4"),labels=c("4th rank", "3rd rank", "2nd rank", "1st rank")) +
      scale_color_discrete(name = "Sex of physician", labels = c("female physician", "male physician"))
    ggsave(filename,p, width = 5, height = 5)
  }

Drucker(Intelligence)

这是现状的照片。

我很感谢任何帮助!

在此处输入图像描述

数据

# first 20 rows
> x <- structure(list(No = c(31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
41, 42, 43, 44, 45, 46, 47, 48, 49, 50), SetID = c(2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), Picid = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), PicName = c("F1A", 
"F1A", "F1A", "F1A", "F1A", "F1A", "F1A", "F1A", "F1A", "F1A", 
"F1A", "F1A", "F1A", "F1A", "F1A", "F1A", "F1A", "F1A", "F1A", 
"F1A"), Sex = c("F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F"), Person = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Pos = c("Open", 
"Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", 
"Open", "Open", "Open", "Open", "Open", "Open", "Open", "Open", 
"Open", "Open", "Open"), `Single Posture` = c(1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Leader = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Confidence = c(2, 
3, 3, 2, 2, 2, 4, 4, 3, 1, 4, 4, 4, 4, 4, 3, 4, 4, 3, 4), Power = c(2, 
3, 1, 3, 3, 2, 2, 2, 2, 1, 2, 3, 3, 4, 2, 4, 3, 4, 4, 4), Intelligence = c(2, 
3, 2, 3, 2, 2, 4, 4, 2, 1, 3, 4, 3, 4, 1, 4, 3, 4, 4, 4), `Commitement Patient` = c(2, 
3, 3, 1, 1, 2, 3, 2, 2, 2, 3, 3, 4, 3, 3, 3, 4, 4, 4, 3), `Ethical behaviour` = c(4, 
3, 3, 3, 2, 2, 4, 3, 2, 1, 4, 4, 4, 4, 2, 3, 4, 4, 1, 4), Teamplayer = c(1, 
3, 2, 1, 2, 2, 2, 3, 3, 1, 3, 3, 2, 2, 3, 3, 2, 4, 3, 3), Communicator = c(2, 
1, 3, 1, 2, 2, 4, 4, 1, 1, 4, 4, 2, 1, 2, 4, 2, 4, 2, 4), Advocate = c(1, 
3, 3, 1, 1, 2, 4, 3, 3, 2, 4, 2, 2, 2, 4, 3, 4, 4, 1, 4), `care family` = c(2, 
3, 1, 1, 3, 2, 4, 2, 3, 2, 1, 3, 4, 4, 2, 4, 4, 4, 1, 4), EBD = c(2, 
3, 3, 3, 3, 2, 4, 4, 1, 1, 4, 1, 4, 3, 4, 4, 4, 4, 3, 4), `Medical Xpert` = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), Ask = c(2, 
1, 3, 1, 2, 3, 3, 2, 1, 1, 3, 2, 3, 3, 1, 2, 2, 4, 3, 4), `Personal preferences` = c(3, 
1, 3, 1, 1, 2, 4, 4, 4, 1, 1, 2, 4, 3, 4, 3, 2, 4, 3, 3), `Pro n Cons` = c(3, 
1, 2, 1, 1, 3, 3, 4, 4, 2, 3, 4, 3, 3, 4, 4, 4, 4, 4, 4), Deriates = c(1, 
4, 2, 1, 2, 2, 4, 1, 1, 2, 3, 1, 1, 3, 2, 2, 2, 1, 3, 1), Serious = c(4, 
4, 2, 1, 4, 1, 4, 1, 2, 1, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4), Solari = c("AB", 
"CD", "BC", "BD", "AB", "DE", "BD", "CE", "BC", "DE", "BC", "AD", 
"CD", "BD", "BD", "BD", "BC", "BC", "AB", "BD"), NSol = c(1, 
2, 1, 3, 1, 2, 3, 3, 1, 2, 1, 3, 2, 3, 3, 3, 1, 1, 1, 3), SolriTranskr = c(1, 
3, 2, 7, 1, 4, 7, 7, 2, 4, 2, 7, 3, 7, 7, 7, 2, 2, 1, 7), VPSex = c(2, 
2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2), Alter = c(25, 
31, 46, 39, 25, 28, 25, 31, 28, 35, 28, 35, 35, 48, 35, 25, 39, 
58, 35, 29), Bildung = c(4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 3, 4, 4, 4, 3, 4, 3), Ethnicity = c(4, 1, 1, 1, 4, 4, 1, 4, 
5, 3, 1, 1, 1, 1, 2, 4, 1, 1, 1, 1), `Doctor Frequency` = c(5, 
2, 6, 1, 5, 3, 5, 4, 2, 6, 0, 1, 1, 4, 1, 10, 3, 2, 0, 0)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

标签: rggplot2

解决方案


推荐阅读