Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Y axis at 0 using ggplot and facet_wrap [duplicate]

I'm using the following code:

x_breaks <- seq(as.Date("2010/1/1"), as.Date("2015/4/1"), "months")
x_labels <- as.character(x_breaks, format="%b%y")

thousand_formatter<-function(x){
  return(as.integer(x))
}

vLines <- data.frame('Date'=as.Date('2014/1/1'))
vLines <- rbind(vLines,vLines)
vLines$Date[1] <- as.Date('2013/3/4')
vLines$Date[2] <- as.Date('2014/6/2')

vLines$grp <- c('Complex Cases (except CD cases)','CD cases')

p <- ggplot(toPlot[1:261,], aes( Date, value)) + theme_bw() +ylab('Transactions') + xlab('') +
  scale_x_date(breaks=x_breaks, labels=x_labels)
p <- p + geom_line(aes(colour = variable, fill= variable),size=1.5) +
  theme(axis.text.y=element_text(hjust=0, angle=0), 
        axis.text.x = element_text(hjust=1, angle=45),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x=element_line(color='grey90',linetype='dashed'),
        panel.grid.major.y=element_line(color='grey90',linetype='dashed'),
        plot.title=element_text(size=20),
        axis.text=element_text(size=10),
        legend.key=element_blank(),
        legend.title=element_blank()) +
  scale_y_continuous(label=thousand_formatter) +
  ggtitle('Some Title')+
  scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6")) +
  facet_wrap(~OrderType,nrow=3,scales="free") +
  geom_vline(data=vLines,aes(xintercept=as.numeric(Date),colour=grp,linetype=grp),size=1) 
#   scale_y_continuous(expand = c(0, 0))
p
ggsave('SomeTitle.png', scale=2)

to get the following plot:

enter image description here

The output of dput(head(toPlot)) is:

structure(list(Date = structure(c(15065, 15095, 15218, 15309, 
15371, 15400), class = "Date"), OrderType = structure(c(1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Delivery", "eFiling", "Filing", 
"ProcessServing", "Research"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Orders", "Revenue"), class = "factor"), 
    value = c(1, 1, 1, 2, 1, 3)), .Names = c("Date", "OrderType", 
"variable", "value"), row.names = c(NA, 6L), class = "data.frame")

If we notice carefully, we can see that the graphs at position (2,1), (2,2) and (3,1) don't start at zero.

I would like the Y axis to start at zero, any pointers would be greatly appreciated.

This question may have a possible duplicate at Force the origin to start at 0 in ggplot2 (R) but its solution didn't fix my issue. In other words, I tried using scale_y_continuous(expand = c(0, 0)) but it didn't fix my issue. At no point am I using the two scale_y_continuous() statements, one is always commented.

My dataset may be found here.

like image 375
Patthebug Avatar asked Apr 29 '15 22:04

Patthebug


1 Answers

You can use scale_y_continuous with the key word limits like that :

scale_y_continuous(limits=c(0,max(toPlot$value)))
like image 65
maeVeyable Avatar answered Oct 24 '22 09:10

maeVeyable