3

I have a dataset containing measurements at two timepoints. I have made a barplot of the data of the first timepoint, and would like to add points for the second timepoint (this is just meant as a reference). As you can see below the points are there, but they are not in the correct x-axis position (i.e. they are all on the same x-value, unlike the bars).

How to solve this?

library(ggplot2)

MyData = data.frame(
  method=rep(c("A","B","C","D","E"),times=3),
  time1=rnorm(30,10,3),
  time2=rnorm(30,8,2),
  lab=rep(rep(c(1,2,3),each=5),times=2),
  cat=rep(c(1,2),each=15)
)

p <- ggplot(data = MyData,
            aes(x=lab)) +
  geom_bar(aes(y=time1,fill=method),
           stat="identity",
           position="dodge",
           alpha=.7
  ) +
  geom_point(aes(y=time2,group=method),
           stat="identity",
           position="dodge",
           alpha=.8,
           size=3) +
  scale_fill_brewer(palette=3) +
  facet_grid(. ~ cat)
p

enter image description here

mats
  • 133
  • 1
  • 3
  • 10

1 Answers1

6

Use position = position_dodge(width = .9) in geom_point.

lukeA
  • 53,097
  • 5
  • 97
  • 100