r - 仅在R中将ggplot上的x轴显示为月

标签 r ggplot2

我在这里有一个非常简单的问题。我有一个2009-2012年的数据集。我想用多个方面绘制数据。我创建了如下多面图。

R码

ggplot(al02428400,aes(x=date,y=as.numeric(Discharge)))+geom_line()+ylab("Discharge(cfs)")+facet_wrap(~Year,scales=("free_x"))+theme_bw()


上面的R代码的输出如下:



在X轴上,我只想显示月份。默认情况下,它显示月份和年份。有什么办法可以摆脱一年的困扰?

完全可复制的代码如下:

library(ggplot2)

url <- "http://nwis.waterdata.usgs.gov/usa/nwis/uv/?cb_00060=on&cb_00065=on&format=rdb&period=&begin_date=2009-01-01&end_date=2012-12-31&site_no=02428400"
download.file(url,destfile="Data load for stations/data/alabamariver-at-monroeville-2009.txt")

al02428400 <- read.table("Data load for stations/data/alabamariver-at-monroeville-2009.txt",header=T,skip=1,sep="\t")
head(al02428400)

sapply(al02428400,class)
al02428400 <- al02428400[-1,]

names(al02428400)<- c("Agency","SiteNo","Datetime", "TZ","Discharge","Status","Gageheight","gstatus")
al02428400$date <- strptime(al02428400$Datetime, format="%Y-%m-%d %H:%M")

al02428400$Discharge <- as.numeric(as.character(al02428400$Discharge))
al02428400$Year <- as.numeric(format(al02428400$date, "%Y"))
ggplot(al02428400,aes(x=date,y=as.numeric(Discharge)))+geom_line()+ylab("Discharge(cfs)")+facet_wrap(~Year,scales=("free_x"))+theme_bw()


谢谢。

最佳答案

由于x值为日期,因此可以使用scale_x_date()更改标签格式。需要库scales才能更好地设置中断和标签的格式。

library(scales)
+scale_x_datetime(labels = date_format("%b"))

关于r - 仅在R中将ggplot上的x轴显示为月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596811/

相关文章:

R中的相对频率直方图,ggplot

r - 操纵 Y 轴限制不起作用(ggplot2 百分比条形图)

r - 使用管道运算符将数据转换为 data.frame

r - "Reversed"在 ggplot2 中使用 fct_infreq()

r - 有没有办法让 ggplot2 中的position_jitterdodge在使用两个数据集时按两个分类变量进行排序?

r - facet_wrap log_transforming 一列中的轴

R rstats 如何替换字符串中某个位置的单个字符

r - 有没有办法使用变量来获取 R 中 XTS 数据帧的最后 X 个观察值?

r - 如何在不创建摘要数据框的情况下在 ggplot2 中标记堆积条形图?

r - 将第二个标题添加到基于极坐标的 ggplot2 图