geom_errorbar disappears when using scale_x_reverse

21 views
Skip to first unread message

JBell

unread,
Oct 28, 2014, 7:44:36 AM10/28/14
to ggp...@googlegroups.com

I am trying to produce a simple scatter plot but with the x scale reversed (as this will be a better way of showing that y (shared species) declines with increasing x (environmental parameter)). The plot works fine with both axes ascending...

envsum$chemo.index <- c(0.56254404, 2.73595615, 2.47705543, 0.01796475)

envsum$sp_shared <- c(66.6, 100.0,  83.9,  62.8)

gShareSP  <- ggplot(envsum, aes(chemo.index, sp_shared))

gShareSP  <- gShareSP + geom_point()

gShareSP  <- gShareSP +  geom_errorbarh(xmax=ch.ind+seCh, xmin=ch.ind-seCh)

gShareSP  <- gShareSP + geom_smooth(method="lm", se=F, colour="black")

gShareSP  <- gShareSP + theme_bw()

gShareSP

but when I add the line

gShareSP  <- gShareSP + scale_x_reverse()

errorbars disappear (except one which moves to a random point on the plot)

I didn't think scale_x_reverse should alter the positions of the error bars and I can't find the fix to stop it happening

Thanks in advance for any insight

JBell

unread,
Oct 28, 2014, 12:59:32 PM10/28/14
to ggp...@googlegroups.com
Edit for df - sorry for confusion

envsum <- data.frame(chemo.index = c(0.56254404, 2.73595615,2.47705543, 0.01796475),
                                    sp_shared = c(66.6, 100.0, 83.9, 62.8),
                                    seCh = c(0.115674244, 0.182706874, 0.231441854, 0.004465524))

On Tuesday, October 28, 2014 11:44:36 AM UTC, JBell wrote:

I am trying to produce a simple scatter plot but with the x scale reversed (as this will be a better way of showing that y (shared species) declines with increasing x (environmental parameter)). The plot works fine with both axes ascending...

envsum$chemo.index <- c(0.56254404, 2.73595615, 2.47705543, 0.01796475)

envsum$sp_shared <- c(66.6, 100.0,  83.9,  62.8)

gShareSP  <- ggplot(envsum, aes(chemo.index, sp_shared))

gShareSP  <- gShareSP + geom_point()


gShareSP  <- gShareSP +  geom_errorbarh(xmax=envsum$chemo.index+seCh, xmin=envsum$chemo.index-seCh)


gShareSP  <- gShareSP + geom_smooth(method="lm", se=F, colour="black")

gShareSP  <- gShareSP + theme_bw()

gShareSP

Vivek Patil

unread,
Oct 28, 2014, 1:22:36 PM10/28/14
to JBell, ggplot2
The geom_errorbarh entry should have been within aes. This should work.

library(ggplot2)
envsum <- data.frame(chemo.index = c(0.56254404, 2.73595615,2.47705543, 0.01796475),
                     sp_shared = c(66.6, 100.0, 83.9, 62.8),
                     seCh = c(0.115674244, 0.182706874, 0.231441854, 0.004465524))

gShareSP  <- ggplot(envsum, aes(chemo.index, sp_shared))
gShareSP  <- gShareSP + geom_point()
gShareSP  <- gShareSP +  geom_errorbarh(aes(xmax=envsum$chemo.index+envsum$seCh, xmin=envsum$chemo.index-envsum$seCh))
gShareSP  <- gShareSP + geom_smooth(method="lm", se=F, colour="black")
gShareSP  <- gShareSP + theme_bw()
gShareSP

gShareSP  <- gShareSP + scale_x_reverse()
gShareSP


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

JBell

unread,
Oct 30, 2014, 5:03:08 AM10/30/14
to ggp...@googlegroups.com
Thank you Vivek, that works perfectly


On Tuesday, October 28, 2014 11:44:36 AM UTC, JBell wrote:
Reply all
Reply to author
Forward
0 new messages