Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding "months" as x-axis labels to a plot

I'm trying to plot the attached data in R using ggplot.

link of the data

Here's my script:

library(ggplot2)

dat<-read.csv("dat.csv",header=TRUE,sep=",")
dat<-data.frame(dat)
dat$min<-dat$zam-sd(dat$zam)
dat$max<-dat$zam+sd(dat$zam)
ggplot(dat,aes(dd,zam))
+geom_ribbon(aes(ymin=min,ymax=max),fill="skyblue")
+geom_line(color="steelblue4",lwd=1)
+theme(panel.background=element_rect(fill="white"), axis.line=element_line(colour="black"), panel.border = element_rect(colour = "black", fill=NA, size=5))

Question:

I would like the xaxis to be in months (January to December). But the data is for a leap year. I tried this command by gave an error.

dat$date <- seq(as.Date("2012/1/1"), as.Date("2012/12/31"), "month")

Error:

Error in $<-.data.frame(*tmp*, "date", value = c(15340, 15371, 15400, : replacement has 12 rows, data has 366

Can anyone suggest a simple method to do this.

like image 209
ichabod Avatar asked Feb 22 '26 19:02

ichabod


1 Answers

This will get you to dates:

dat$date <- as.Date(strptime(paste("2012", dat$dd), format="%Y %j"))

Then ggplot something like:

ggplot(dat, aes(date, zam) + 
geom_ribbon(aes(ymin = min, ymax = max),fill = "skyblue") + 
geom_line(color = "steelblue4", lwd = 1) + 
scale_x_date(date_labels = "%B", date_breaks = "1 month")
like image 91
neilfws Avatar answered Feb 25 '26 14:02

neilfws