Linked Questions

Popular Questions

I'm trying to display estimates and confidence intervals from different regression models. (I recreated a similar dataset in the post.)

How can I use position_dodge, so that the points and lines have the same distance for different numbers of groups. (Notice the points and lines are farther apart in the first plot in the example.)

For now I can manually calculate the width argument of position dodge, by constant * nlevels(x) but I'd like to have a solution that does not require this.

I tried with different values of preserve, but I'm not quite sure what the parameter does. Also I did not find out what the width of points and lines is, if they have one at all, in geom_bar the width is more clear. (Compare this question: What is the width argument in position_dodge? )

require(ggplot2)

set.seed(42)

N_x     <- 3
N_group <- 4

testdata <- data.frame(
  a = rep(LETTERS[1:N_x], each=N_group),
  b = rep(letters[1:N_group], N_x),
  c = c(rep(1, N_group), exp(rnorm(N_group*(N_x-1))))
)

testdata$ci_width <- c(rep(NA, N_group), exp(rt(N_group*(N_x-1), 10)))
testdata$c_lower <- testdata$c - testdata$ci_width
testdata$c_upper <- testdata$c + testdata$ci_width

testdata <- testdata[order(testdata$b), ]

testdata

ggplot(testdata[1:6, ], aes(x=a, y=c, ymin=c_lower, ymax=c_upper, group=b, shape=b)) +
  geom_point(position = position_dodge(.125), size=3) +
  geom_linerange(position = position_dodge(.125)) + 
  geom_hline(yintercept = 1)

ggplot(testdata, aes(x=a, y=c, ymin=c_lower, ymax=c_upper, group=b, shape=b)) +
  geom_point(position = position_dodge(.125), size=3) +
  geom_linerange(position = position_dodge(.125)) + 
  geom_hline(yintercept = 1)

Related Questions