我正在尝试使用facet_grid绘制长达八年(2004-2011)系列的月降水量数据,以在单个图中绘制每年的月度值。但是,很多时候,绘图时会出现问题(可能是我的错)。您可以看到我得到的 x 轴从 2004 年开始到 2011 年结束的每个图,但我需要每个年份/方面的图从 1 月到 12 月运行。我怎样才能摆脱这个错误?
\n\n任何帮助将不胜感激。
\n\n
在这里您可以获得 dput(pcp.mensual) http://ubuntuone.com/0EZvmwXGnSkqNVwXZOnx8E的输出,以下是我使用的代码(一段较长的脚本)。
\n\nlibrary(ggplot2)\nlibrary(plyr)\nlibrary(lubridate)\nlibrary(scales)\n\n#---------------------------------------------------------------------\n\nsystem("awk \'{ print $1 \\";\\" $2 \\";\\" $3 \\";\\" $5 \\";\\" $10 \\";\\" $11 \\";\\" $13 }\' visbel.cor > kk.dat",intern=T)\n\n# Lectura de datos\ndatos=read.csv("kk.dat",sep=";",header=T,na.strings="-99.900")\n\n# Inclusi\xc3\xb3n de una columna con formato temporal \n# (dato original en formato %y/%m/%d %H:%M:%S)\ndatos=within(datos, datetime <- as.POSIXct(paste(FECHA,H_SOLAR),format = "%y/%m/%d %H:%M:%S"))\n\n# Creaci\xc3\xb3n del objeto datetime\n#datetime <- as.POSIXct(paste(datos$FECHA,datos$H_SOLAR),format = "%y/%m/%d %H:%M:%S")\n\n# Eliminaci\xc3\xb3n de valores NA en Precipitaci\xc3\xb3n para barplot\ndatos$Precipitacion[is.na(datos$Precipitacion)]=0\n\n#---------------------------------------------------------------------\n\npcp.diaria=aggregate(cbind(Precip.diaria=Precipitacion) ~ FECHA, datos, sum)\npcp.diaria=within(pcp.diaria, date <- as.POSIXct(paste(pcp.diaria$FECHA),format = "%y/%m/%d"))\n\n# Agregaci\xc3\xb3n mensual\npcp.diaria$mes <- floor_date(pcp.diaria$date, "month")\npcp.mensual=ddply(pcp.diaria, "mes", summarise, x = sum(Precip.diaria,na.rm=TRUE))\npcp.mensual$ano=year(pcp.mensual$mes)\ncolnames(pcp.mensual) <- c("mes", "Precip.mensual","ano")\n\n#--------------------------------------------------------------------- \n\nggplot(data=pcp.mensual,aes(x=mes, y=Precip.mensual)) +\n facet_grid(ano ~. ) +\n geom_bar(colour="blue",stat="identity",fill="blue") + \nRun Code Online (Sandbox Code Playgroud)\n
您可以使用该months()函数从 POSIXct 对象中提取月份信息。您可以将其作为您的x而不是mes. 要按照您想要的顺序排列月份,而不是按字母顺序排列,您可以创建一个有序因子:
pcp.mensual$month <- months(pcp.mensual$mes)
pcp.mensual$month <- factor(pcp.mensual$month, levels = unique(pcp.mensual$month))
ggplot(data=pcp.mensual,aes(x=pcp.mensual$month, y=Precip.mensual)) +
facet_grid(ano ~. ) +
geom_bar(colour="blue",stat="identity",fill="blue")
Run Code Online (Sandbox Code Playgroud)
