简体   繁体   中英

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM