Home > OS >  How to plot geom_point alone plus geom_point with position_dodge
How to plot geom_point alone plus geom_point with position_dodge

Time:03-02

I struggling on how I can plot my real values, present in the real_values vector, next to the estimates values. My problem here is that the estimates values have a range (via the geom_errorbar), and for the real values I would like to plot just the point, in black, on the left side of each of the 10 estimates.

Here's an example of what I tried:

est_values = rnorm(20)
real_values = rnorm(10)
dat_ex =  data.frame(
          xvalues = 1:10,
          values = est_values,
          method = c(rep("A",10),rep("B",10)),
          ic_0.025 = c(est_values - rnorm(20,1,0.1)),
          ic_0.975 =  c(est_values   rnorm(20,1,0.1)))

ggplot(dat_ex)  
  #geom_point(aes(x = 1:10, y= real_values), size = 2)  
  geom_point(aes(x = xvalues, y= values, group = method, colour = method),  position=position_dodge(.9), size = 3)  
  geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin =  ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2)

enter image description here

CodePudding user response:

ggplot generally works best with data in data frames. So we put your real_values in a data frame and plot them in a separate layer, and "nudge" them to the left, as requested:

ggplot(dat_ex)  
  geom_point(aes(x = xvalues, y= values, group = method, colour = method),  position=position_dodge(.9), size = 3)  
  geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin =  ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2)  
  geom_point(
    data = data.frame(values = real_values, xvalues = dat_ex$xvalues),
    aes(x = xvalues, y = values), 
    position = position_nudge(x = -.4),
    color = "black")

enter image description here

A nicer method might be to put them all in the same data frame. This can simplify the code and will automatically put them in the legend.

library(dplyr)
dat_ex = data.frame(
  xvalues = 1:10,
  values = real_values,
  method = "real"
) %>%
  bind_rows(dat_ex) %>%
  mutate(method = factor(method, levels = c("real", "A", "B")))

ggplot(dat_ex, aes(x = xvalues, y = values, color = method))  
  geom_point(position=position_dodge(.9), size = 3)  
  geom_errorbar(aes(ymin =  ic_0.025, ymax = ic_0.975, group = method),
                size = 1.3, position=position_dodge(.9), width = .2)  
  scale_color_manual(values = c("real" = "black", "A" = "orange", "B" = "blue"))

enter image description here

CodePudding user response:

I would add real_values to your data as another level of method, so they will be dodged along with "A" and "B" (and included in the legend):

library(ggplot2)

dat_ex <- rbind(
  dat_ex,
  data.frame(
    xvalues = 1:10, 
    values = real_values, 
    method = "Real",
    ic_0.025 = NA_real_,
    ic_0.975 = NA_real_
  )
)

# arrange so "Real" is on the left
dat_ex$method <- factor(dat_ex$method, levels = c("Real", "A", "B"))

ggplot(dat_ex)  
  geom_point(aes(x = xvalues, y= values, group = method, colour = method),  position=position_dodge(.9), size = 3)  
  geom_errorbar(aes(x = xvalues, y= values, group = method, colour = method,ymin =  ic_0.025, ymax = ic_0.975), size = 1.3,position=position_dodge(.9), width = .2)  
  scale_colour_manual(values = c("black", "forestgreen", "royalblue"))

  • Related