使用ggplot和facet_wrap从0开始的Y轴 [英] Starting Y axis at 0 using ggplot and facet_wrap

查看:62
本文介绍了使用ggplot和facet_wrap从0开始的Y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

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)

获得以下图解:

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")

如果我们仔细观察,可以发现位置(2,1),(2,2)和(3,1)的图形不是从零开始的.

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

我希望Y轴从零开始,不胜感激任何指针.

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.

推荐答案

您可以将 scale_y_continuous 与关键字 limits 这样使用:

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

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

这篇关于使用ggplot和facet_wrap从0开始的Y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆