1

I am trying dodge a pair of means and confidence intervals for two models using ggplot. I've looked into similar questions like this and this, but their code doesn't address my problem.

I have the following data frame (note that condition is a factor, values increase as you descend down the data frame):

>print(dat) condition pH lowerH upperH model 1 Black 650 0.7863719 0.6863438 0.8548062 pdp 2 Black 650 0.8428675 0.7123000 0.9574000 ddm 3 White 650 0.8133078 0.7310262 0.8864712 pdp 4 White 650 0.8211373 0.6838000 0.9488000 ddm 5 Black 800 0.8754109 0.7908725 0.9306012 pdp 6 Black 800 0.9408192 0.8410000 0.9941000 ddm 7 White 800 0.9044102 0.8349587 0.9623462 pdp 8 White 800 0.9183600 0.8145000 0.9867000 ddm 9 Black 950 0.9003072 0.8403200 0.9601112 pdp 10 Black 950 0.9569349 0.8812000 0.9967000 ddm 11 White 950 0.9131546 0.8494812 0.9648262 pdp 12 White 950 0.9473685 0.8683000 0.9934000 ddm 13 Black 1100 0.9091797 0.8342850 0.9622412 pdp 14 Black 1100 0.9708420 0.8864000 0.9977000 ddm 15 White 1100 0.9274862 0.8636063 0.9691300 pdp 16 White 1100 0.9570067 0.8727000 0.9954000 ddm 

I am trying to plot the mean (pH) and confidence interval (lowerH, upperH) for each condition as a function of the model used (model). What I'd like is for the means and CIs to be dodged so that they are not overlapping. I've written the following ggplot code:

ggplot(dat, aes(x = condition, y = pH)) + geom_point(size = 3, shape = 5) + #pred cond mean geom_pointrange(aes(ymin = lowerH, ymax = upperH), size = .2) + #95% HDI coord_cartesian(ylim = c(.5, 1.01)) + #adjust x-axis ylab("Hit Rate") + #label y axis theme_bw() + #black and white theme theme(axis.title.x = element_blank(), panel.background = element_blank(), panel.grid = element_blank(), panel.border = element_rect(colour = "black", fill = NA, size = 1)) 

Which creates this output

As you can see, the means and CIs for both models overlap. However, when I try to dodge them, as I've done in the code below, I only succeed in moving all data points to the left (with the model means and CIs still overlapping).

ggplot(dat, aes(x = condition, y = pH)) + geom_point(size = 3, shape = 5, position = position_dodge(5)) + #pred cond mean geom_pointrange(aes(ymin = lowerH, ymax = upperH), size = .2) + #95% HDI coord_cartesian(ylim = c(.5, 1.01)) + #adjust x-axis ylab("Hit Rate") + #label y axis theme_bw() + #black and white theme theme(axis.title.x = element_blank(), panel.background = element_blank(), panel.grid = element_blank(), panel.border = element_rect(colour = "black", fill = NA, size = 1)) 

What I'm looking for is a way to dodge the model means and CIs so that they are nonoverlapping. Sample code is especially helpful.

1

1 Answer 1

2

You need to add a level of group or color to the plot. Here I'm using col, you can also use group to keep the colours the same:

ggplot(dat, aes(x = condition, y = pH, col = model)) + geom_point(size = 3, shape = 5, position = position_dodge(1)) + geom_pointrange(aes(ymin = lowerH, ymax = upperH), position = position_dodge(1), size = .2) 

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This solved my problem! One thing I'd like to point out is that any other data that is being used in the plot must also have columns with "x", "y", and "group", even if the data are not grouped (i.e., an observed mean that is not a function of either model--used to compare the models to).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.