r - Removing gap between ggplot y-axis and first x-value -
i need remove gap preceding 1950 on x axis.
i have attempted utilize scale_x_continuous
results in:
error: discrete value supplied continuous scale
i believe must have date format of data.
example data:
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
the code used create plot:
plot <- ggplot(d2,aes(x = date, y = y)) + geom_area(data=subset(d2, y<=0), fill="blue") + geom_area(data=subset(d2, y>=0), fill="red") + scale_y_continuous(name = "mei")+ xlab("year")+ theme(axis.text.y = element_text(size=24), axis.text.x = element_text(size=24), axis.title.y = element_text(size=24), axis.title.x = element_text(size=24), panel.background = element_blank(), panel.grid.major = element_line(colour = "grey"), strip.background = element_blank(), panel.border = element_rect(colour = "black", fill = na), axis.line = element_line(colour = "black"))
any help appreciated!!!
you can utilize expand
argument of scale_x_datetime
remove space between axis , start of data. (you can utilize in scale_y(x)_continuous, , others...)
# info - tweaked 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 , tag y beingness 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))
r date ggplot2 axis-labels
No comments:
Post a Comment