0

I am struggling with the following issue and would really appreciate your help.

I have the following data (this is just a random example for simplification)

x<- c('left', 'left', 'left', 'right', 'right', 'right')
y<- c (0.5, 0.3, 0.2, 0.5, 0.7, 0.2)
conf<- c(1.5, 1.7, 3, 2, 3, 1)
df <-data.frame(x,y,conf)

I want to do the following plot.

library(ggplot2)
ggplot(df, aes(x=x, y=y))+geom_point()+coord_flip()

This generates the following image see photo here. But each of my points (y) has a also certain confidence associated with them (variable conf), which I would like to plot as confidence values on another x axis (that would go on the right hand side in this case and the confidence values would be vertical lines). How do I add this second x axis on the right side of the plot, so that it is numerical and that it plots my values as confidence intervals?

Thanks a lot for your help.

Mirta
  • 19
  • 4
  • 1
    Why are you bothering with `coord_flip` when you could just map `aes(x = y, y = x)`? Your naming convention seems to introduce extra confusion. To be clear, the uncertainty is not in the name `y` (horizontal) dimension, but in the `x` (vertical) dimension? – Gregor Thomas May 11 '16 at 22:46
  • Having two y-axes is [deliberately difficult in ggplot](http://stackoverflow.com/q/3099219/903061). However, you could easily `facet_wrap(~ x, ncol = 1)`, map `y = 0` and use a standard `geom_errorbar` to indicate uncertainty. – Gregor Thomas May 11 '16 at 22:48
  • I tried make a simple example from my more complex data to be able to present it here and used wrong confusing names. By trying to simplify it I actually made it more complicated. @Gregor thanks the facet_wrap can work too. – Mirta May 12 '16 at 14:40

2 Answers2

4

using geom_errorbarh() you can do this:

library(ggplot2)
ggplot(df, aes(x=x, y=y))+geom_point()+coord_flip()+ 
    geom_errorbarh(aes( xmax = as.numeric(x)+conf/10, 
                       xmin = as.numeric(x)-conf/10, 
                       height=0))

enter image description here

But as said in other posts it may not be great to have several meaning on your x axis, so you could facet:

ggplot(df, aes(x=0, y=y))+geom_point()+ 
     geom_errorbarh(aes( xmax = conf, xmin = -conf, height=0))+
     coord_flip()+facet_wrap(~dir, nrow=2)

enter image description here

HubertL
  • 19,246
  • 3
  • 32
  • 51
1

Looking around a bit, several threads indicate that ggplot is designed to not support what you are looking for and it would generally be bad practice (see this comment and this thread).

There seems to be a 'hack' here, which might go a bit further than you want to.

On a side-note: May I ask why you decided to label your y-axis with "x" and your x-axis with "y"?

Community
  • 1
  • 1
matt_jay
  • 1,241
  • 1
  • 15
  • 33
  • the x is x and y is y, i just used cord_flip. (with my actual data i flip the plot as it is better for representation, but doing it with this invented data was pointless and confusing). – Mirta May 12 '16 at 14:45