Dot plot with error bars in ggplot changes order of presentation of data

Viewed 102

Oh Wise Men & Women of the R!

I created a dot plot with error bars in ggplot2. I got it to work as intended, for the most part. However, it seems to "randomly" (as far as I could tell) invert the order in which it presents the data for the two types of material I analyzed.

This is my code:


data<-data.frame("Mat_art"=c("Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners"), "CompIDinv"=c(17, 17, 16, 16, 15, 15, 14, 14, 13, 13, 12, 12, 11, 11, 10, 10), "Comp"=c("Cheese", "Cheese", "Mayo", "Mayo", "Ketchup", "Ketchup", "Glue", "Glue", "Tofu", "Tofu", "Ranch", "Ranch", "Marmite", "Marmite", "Butter", "Butter"), "Prom"=c(1.15968339356004, 1.09598175499146, 0.606871622602421, 0, 0.477223208284233, 0, 1.52931048485049, 0.499586619837113, 1.33614656021619, 1.95359709169813, 1.22093637869439, 2.34963432630937, 1.35262980291428, 1.69298650050846, 1.35086700266383, 1.24031665670217), "infCI95"=c(0.775987209086803, 0.727123902187271, 0.388630354128953, 0, 0.406186682979503, 0, 1.1431692694034, 0.318904748424858, 1.19987305571909, 1.03758502964521, 1.11361766267652, 2.09487026135963, 1.24530044274135, 0.543063809010205, 0.993468721657989, 0.695707000558221), "supCI95"=c(1.54337957803327, 1.46483960779564, 0.825112891075889, 0, 0.548259733588963, 0, 1.91545170029758, 0.680268491249369, 1.47242006471328, 2.86960915375105, 1.32825509471225, 2.60439839125911, 1.4599591630872, 2.84290919200671, 1.70826528366967, 1.78492631284613), "Color"=c("4", "3", "2", "2", "2", "2", "4", "2", "4", "4", "4", "4", "4", "4", "4", "4"), "Etiqueta"=c("Immobilization", "Conservation", "Degradation", "Degradation", "Degradation", "Degradation", "Immobilization", "Degradation", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization"))

data$Comp<-as.factor(data$Comp)
data$Mat_art<-as.factor(data$Mat_art)
data$Etiqueta<-as.factor(data$Etiqueta)

ggplot(data = data, aes(x = reorder(Comp,CompIDinv), y = Prom, ymin = infCI95, ymax = supCI95, colour = reorder(Etiqueta, Color), shape=Mat_art)) +
  scale_shape_manual(values=c(16, 2)) +
  geom_point(position = position_dodge(width = 0.75), size = 2) +
  geom_errorbar(position = position_dodge(width = 0.75), width = 0.5) +
  scale_colour_manual(values = c("#0072B2", "#009E73", "#D55E00")) +
  geom_hline(yintercept=1, linetype="dashed", color = "black", size=1) +
  labs(x = "Dip", y = "Yummyness", colour = "Behavior", shape = "Material") +
  coord_flip() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))

And this is the plot I'm getting out of it:

Dotplot with orders randomly changing

As you can see, for "Cheese", Apples comes first, but then it switches to Naners first up to "Glue" and then again till the end. My intention is for Apples to come first always (no offense meant to those that like naners better :D). This happens a couple more times in the complete data set, of which these are actual values. Of course it's not about dipping your naners in glue, don't worry.

Anyway, I would be super grateful if you'd be willing to help me out on this!

1 Answers

I have modified your code, mainly reordered factors first instead of within ggplot:

data$Comp<-as.factor(data$Comp)
data$Mat_art<-factor(data$Mat_art, levels = c("Naners", "Apples"))
data$Etiqueta<-factor(data$Etiqueta, levels = c("Degradation", "Conservation", "Immobilization"))

ggplot(data = data, aes(x = reorder(Comp,CompIDinv), y = Prom, ymin = infCI95, ymax = supCI95, 
                        colour = Etiqueta, shape=Mat_art
                       )) +
  scale_shape_manual(values=c(16, 2)) +
  geom_point(position = position_dodge(width = 0.75), size = 2) +
  geom_errorbar(position = position_dodge(width = 0.75), width = 0.5) +
  scale_colour_manual(values = c("#0072B2", "#009E73", "#D55E00")) +
  geom_hline(yintercept=1, linetype="dashed", color = "black", size=1) +
  labs(x = "Dip", y = "Yummyness", colour = "Behavior", shape = "Material") +
  coord_flip() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))

enter image description here

Related