添加“月"作为图的x轴标签 [英] Adding "months" as x-axis labels to a plot

查看:68
本文介绍了添加“月"作为图的x轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ggplot在R中绘制附加数据.

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

数据链接

这是我的剧本:

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))

问题:

我希望xaxis以月为单位(从1月到12月).但是数据是a年.我尝试了此命令,但给出了错误.

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")

错误:

$&-.data.frame ( * tmp * ,"date",value = c(15340,15371,15400 ,:替换有12行,数据有366

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.

推荐答案

这将使您了解日期:

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

然后ggplot之类的东西:

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")

这篇关于添加“月"作为图的x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆