i am displaying time series data with ggplot2
but the tick-labels show some strange behaviour. probably i am doing something wrong but i could not find any help on the internet. here's an example:
#just sample data
time <- as.Date(seq(as.Date("2004/1/1"), as.Date("2009/12/1"), by = "1 month"))
data <- rnorm(nrow(test))+c(1:nrow(test))
test <- data.frame(time, data)
i plot with:
q1 <- ggplot(data=test) + geom_line(aes(x=time, y=data))
q1 <- q1 + scale_x_date(major="years", minor="3 months", format="%Y-%m", lim=c(as.Date("2004/1/1"),as.Date("2009/12/1")), name="")
q1
this produces the following graph:
but from my understanding the grid should end 2009/12/1 - right? thanks a lot for your help!
To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .
The axes labels and ticks can be removed in ggplot using the theme() method. This method is basically used to modify the non-data components of the made plot. It gives the plot a good graphical customized look. The theme() method is used to work with the labels, ticks, and text of the plot made.
geom_text() and geom_label() add labels for each row in the data, even if coordinates x, y are set to single values in the call to geom_label() or geom_text() . To add labels at specified points use annotate() with annotate(geom = "text", ...) or annotate(geom = "label", ...) .
The limits
parameter to scale_x_date
affects which data points are plotted, but does not directly change the axis tick labels nor the axis range. This behavior is well illustrated in the help page http://had.co.nz/ggplot2/scale_date.html (towards the bottom of the page.)
If you want to eliminate the empty areas to left and right of your data, use coord_cartesian
library(ggplot2)
x <- as.Date(seq(as.Date("2004/1/1"), as.Date("2009/12/1"), by = "1 month"))
y <- rnorm(length(x))+c(1:length(x))
test <- data.frame(time=x, data=y)
q2 <- ggplot(data=test) +
geom_line(aes(x=time, y=data)) +
scale_x_date(major="years", minor="3 months", format="%Y-%m", name="") +
coord_cartesian(xlim=c(as.Date("2004/1/1"),as.Date("2009/12/1")))
png("date_ticks_plot.png", height=600, width=600)
print(q2)
dev.off()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With