1

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.

2
  • is T$date a POSIXct or Date class? If not, try with T$date <- as.Date(T$date) Commented Oct 24, 2014 at 14:58
  • I do not know how to check this. It probably is a character string as I imported it form excel. So I tried to define the date as you suggested. Unfortunately this still gives me the same error. Commented Oct 27, 2014 at 9:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Katharina Very welcome, if you believe my reply is the good answer you should accept the answer. It is how this site works. For more information visit: stackoverflow.com/help/someone-answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.