Removing gap between ggplot y-axis and first x-value

南笙酒味 提交于 2019-12-02 02:23:58

You can use the expand argument of scale_x_datetime to remove the space between the axis and the start of the data. (you can also use this in scale_y(x)_continuous, and others...)

# your data - tweaked to include pos / neg y values
d2 <- read.table(text="x      y                date
1    -631152000 -1.018 '1950-01-01 01:32:40'
2    -628473600 -1.143 '1950-02-01 01:32:40'
3    -626054400 -1.290 '1950-03-01 01:32:40'
4    -623376000 -1.061 '1950-04-01 01:32:40'
5    -620784000 -1.416 '1950-05-01 01:32:40'
6    -618105600 1.372 '1950-06-01 01:32:40'
7    -615513600 1.334 '1950-07-01 01:32:40'
8    -612835200 1.050 '1950-08-01 01:32:40'
9    -610156800 0.578 '1950-09-01 01:32:40'
10   -607564800 0.395 '1950-10-01 01:32:40'", header=TRUE)

# create date class and tag for y being pos / negative
d2$date <- as.POSIXct(d2$date)
d2$tag <- d2$y < 0

library(ggplot2)  

ggplot(d2,aes(x = date, y = y, fill=tag)) + 
                      geom_area() + 
                      scale_y_continuous(name = "MEI")+
                      scale_x_datetime(expand=c(0,0))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!