[英]Adding “months” as x-axis labels to a plot
I'm trying to plot the attached data in R using ggplot. 我正在尝试使用ggplot在R中绘制附加数据。
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). 我希望xaxis可以用几个月(一月到十二月)。 But the data is for a leap year.
但是数据是a年。 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$<-.data.frame
(*tmp*
,“ date”,value = c(15340,15371,15400,:替换有12行,数据有366
Can anyone suggest a simple method to do this. 任何人都可以建议一个简单的方法来执行此操作。
Maybe this will do: 也许这样做:
dat$date <- as.Date(dat$dd,origin="2012-01-01")
ggplot(dat,aes(date,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"))+
scale_x_date(date_labels = "%b",date_breaks = "1 month")
This will get you to dates: 这将使您了解日期:
dat$date <- as.Date(strptime(paste("2012", dat$dd), format="%Y %j"))
Then ggplot something like: 然后ggplot类似:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.