Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to plot geom_point alone plus geom_point with position_dodge

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading