"Inf" for Discrete Scale

610 views
Skip to first unread message

Brandon Hurr

unread,
Aug 29, 2012, 6:51:01 AM8/29/12
to ggplot2
I'm creating a rather wide plot of means with SEM error bars. When I do this, there are many different levels of the discrete x variable that I want to make it easier to reference the "control" values. In order to do this, I want to create a rectangle that goes across the screen that represents the mean-se and mean+se of each control (there are two in my original dataset). I cannot figure out how to do this with a discrete scale. Complicating this is the fact that I am reordering my variables on the fly based upon the mean, so, "a" and "d" might not be the ends of the scale. 

When I use Inf for the xmin/max in geom_rect() I get complaints about discrete/continuous combinations. Is there an "Inf" for a discrete scale? Is there a way to do this? 

#Hopefully reproducible example
require(ggplot2)

data<-data.frame(name=c("a", "b", "c", "d"), mean=c(5, 9, 7, 11), se=c(1.2, 0.9, 3, 2))

ggplot()+

#layer creating a rectangle across the plot
#geom_rect(data=data[data$name=="b",], aes(xmin=-Inf, xmax=Inf, ymin=mean-se, ymax=mean+se), fill='white', alpha=0.2)+
#Using the above line I get the following:
Error: Discrete value supplied to continuous scale
geom_pointrange(data=data, aes(x=reorder(name, mean), y=mean, ymin=mean-se, ymax=mean+se))+
labs(y="b*")+
opts(axis.text.x = theme_text(angle = 90))

Brandon Hurr

unread,
Aug 29, 2012, 12:33:33 PM8/29/12
to ggplot2
I found a hack for this thanks to this post by Dennis Murphy (++):
https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/uz-RqfggOqM

If I create a layer that uses a discrete scale before I use numeric input for geom_rect it uses the levels as numeric and creates the band I'm looking for. I just need to compute the length of the levels and fake a layer. 

Is there a less hacky way? Can we have an Inf -Inf for Discrete scales?


require(ggplot2)

data<-data.frame(name=c("a", "b", "c", "d"), mean=c(5, 9, 7, 11), se=c(1.2, 0.9, 3, 2))
xmax.data<-length(levels(data$name))+1


ggplot()+
#layer creating a rectangle across the plot
geom_point(data=data, aes(x=reorder(name, mean), y=mean(mean)), color=NA)+
geom_rect(data=data[data$name=="b",], aes(xmin=0, xmax=xmax.data, ymin=mean-se, ymax=mean+se), fill='white', alpha=0.5)+

geom_pointrange(data=data, aes(x=reorder(name, mean), y=mean, ymin=mean-se, ymax=mean+se))+
#Using the above line I get the following: Error: Discrete value supplied to continuous scale
labs(y="b*")+
opts(axis.text.x = theme_text(angle = 90))

tjulou

unread,
Feb 5, 2018, 4:52:13 PM2/5/18
to ggplot2
In case anybody hits this thread by googling, the key point seems to be to set xmin and xmax (i.e. outside aes()) while mapping ymin and ymax (i.e. inside aes()) if necessary…
works with ggplot2_2.2.1 (and probably earlier).

data<-data.frame(name=c("a", "b", "c", "d"), mean=c(5, 9, 7, 11), se=c(1.2, 0.9, 3, 2))
ggplot
()+
 
#layer creating a rectangle across the plot

  geom_rect
(data=data[data$name=="b",], xmin=-Inf, xmax=Inf, aes(ymin=mean-se, ymax=mean+se), fill='white', alpha=0.8)+

  geom_pointrange
(data=data, aes(x=reorder(name, mean), y=mean, ymin=mean-se, ymax=mean+se))+

  labs
(y="b*")
Reply all
Reply to author
Forward
0 new messages