Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting both horizontal and vertical point ranges simultaneously in ggplot

Tags:

r

ggplot2

Is there a way to plot both horizontal and vertical point ranges together on the same plot in ggplot. I understand that geom_pointrange(...) plots vertical point ranges, and that horizontal point ranges can be generated with coord_flip(...), but I'm interested in putting both together on the same plot.

set.seed(1)
df <- data.frame(x=sample(1:10,10),y=sample(1:10,10), x.range=1, y.range=2)
library(ggplot2)
ggplot(df) +
  geom_pointrange(aes(x=x, y=y, ymin=y=y.range, ymax=y+y.range))

I'm looking for something like this:

ggplot(df) +
  geom_pointrange(aes(x=x, y=y, 
                      ymin=y-y.range, ymax=y+y.range, 
                      xmin=x-x.range, xmax=x+x.range))

Which of course produces the same output as above because the xmin and xmax arguments are ignored. Evidently, there is (was) a function geom_hpointrange(...) in ggExtra, but this package has been pulled as far as I can tell.

like image 485
jlhoward Avatar asked Dec 16 '13 22:12

jlhoward


2 Answers

Is geom_errorbarh what you are looking for?

ggplot(data = df, aes(x = x, y = y)) +  
  geom_pointrange(aes(ymin = y - y.range, ymax = y + y.range)) +
  geom_errorbarh(aes(xmax = x + x.range, xmin = x - x.range, height = 0))

enter image description here

like image 76
Henrik Avatar answered Sep 20 '22 00:09

Henrik


you can also call geompoint_range twice

ggplot(df, aes(x=x, y=y)) +
  geom_pointrange(aes(ymin=y-y.range, ymax=y+y.range)) + 
  geom_pointrange(aes(xmin=x-x.range, xmax=x+x.range))

enter image description here

like image 37
chang02_23 Avatar answered Sep 21 '22 00:09

chang02_23