Start Y axis at 0 using ggplot and facet_wrap

I am 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 graph:

enter image description here

Conclusion dput(head(toPlot))

:

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 charts at position (2,1), (2,2) and (3,1) do not start at zero.

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

This question may have a possible duplicate in Force source to start at 0 in ggplot2 (R) , but its solution did not fix my problem. In other words, I tried to use it scale_y_continuous(expand = c(0, 0))

, but it didn't fix my problem. Under no circumstances do I use two operators scale_y_continuous()

, one is always commented.

My dataset can be found here .

+3


source to share


1 answer


You can use scale_y_continuous

with a keyword limits

like this:



scale_y_continuous(limits=c(0,max(toPlot$value)))

      

+4


source







All Articles