0

Dear StackOverflowians,

I want to add errorbars to the interaction plot. Without the errorbar code, the code works well. But if I add the error bar code , I'll get the follwoing message:

Drucker(Confidence) Warning messages: 1: In Ops.factor(z, se) : ‘-’ not meaningful for factors 2: In Ops.factor(z, se) : ‘+’ not meaningful for factors 3: Removed 668 rows containing missing values (geom_errorbar).

If I transform the variable to a numeric, i'll get following message

Drucker(x$Intelligence) Error in z - se : non-numeric argument to binary operator

Here's my code + the output of dput(head(x, 20)) (requested in the comments)

##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)

Here a picture from the status quo.

I'm thankful for any help!

enter image description here

DATA

# 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"))
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    How are you calling the function? Can you post code and sample data? Please edit **the question** with the code you ran and the output of `dput(df)`. Or, if it is too big with the output of `dput(head(df, 20))`. (`df` is the name of your dataset.) – Rui Barradas Mar 11 '20 at 13:05
  • As already mentioned, if you add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) its way easier for others to find a solution to your problem. Regarding your error: Could it be that `se` is somehow of type `factor` ? How did you calculate the standard error? – dario Mar 11 '20 at 13:09
  • @RuiBarradas I added the code and an subset of the data. I agree with dario : I want to compute SE from a factor. – Felix Gruen Mar 12 '20 at 10:59
  • 1
    you could boil the question down to 'how to calculate standard errors for categorical data'. Short answer: There is no standard deviation/ standard error for categorical data. But you can calculate [the error of the multinomial proportions](https://stats.stackexchange.com/questions/51248/how-can-i-find-the-standard-deviation-in-categorical-distribution) – tjebo Mar 12 '20 at 13:26

0 Answers0