Katharina
Katharina

Reputation: 17

x axis format in ggplot2

I am trying to do a simple temperature over time curve in ggplot2. I have an average temperature measurement every day over 8 months. When I plot this the x axis gets very crowded. How can I format the x axis so that only the 8 months in full name and the year (e.g. October - 2013) are used as tick-names?

This is my code so far:

T <- read.table("R_Q_all.csv", header=T, sep=";")
head(T)

      Q       date
1 2.440 2013-10-01
2 2.206 2013-10-02
3 2.172 2013-10-03
4 3.115 2013-10-04
5 3.136 2013-10-05
6 2.788 2013-10-06

library("ggplot2")
library("scales")
ggplot(data=na.omit(T), aes(x=date, y=Q))
+ geom_line()+
+ scale_x_date(breaks = "month", labels=date_format("%m-%Y"))+
+ xlab("date")+
+ ylab("temp"))

Fehler in aes(x = date, y = Q) + geom_line() : 
  nicht-numerisches Argument für binären Operator

which gives me the error that there is a non numeric argument for the binary operator.

Upvotes: 1

Views: 841

Answers (1)

Emer
Emer

Reputation: 3824

Your date column must be a date time variable using as.Date and make sure the + sign goes at the end of each sentence. Here goes my test code:

#T <- read.table("R_Q_all.csv", header=T, sep=";")

set.seed(20141028)
T <- data.frame(Q=rnorm(100,mean=2.6428,sd=0.4433),
            date=as.character(as.Date(Sys.time())+c(1:100)),
            stringsAsFactors=FALSE)

head(T)

T$date <- as.Date(T$date)

library("ggplot2")
library("scales")

ggplot(data=na.omit(T), aes(x=date, y=Q))+
geom_line()+scale_x_date(breaks = "month", labels=date_format("%m-%Y"))+
xlab("date")+ylab("temp")

I created a T data.frame that mimics the read.table output, then the string column called date was converted and your ggplot code worked fine.

Upvotes: 1

Related Questions