R中的季节性ggplot? [英] seasonal ggplot in R?

查看:60
本文介绍了R中的季节性ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看11月至4月的数据,并希望从11月至4月开始绘制一个图.下面是我的示例代码,可以筛选出感兴趣的月份.

I am looking at data from Nov to April and would like to have a plot starting from Nov to April. Below is my sample code to screen out month of interests.

library(tidyverse)
mydata = data.frame(seq(as.Date("2010-01-01"), to=as.Date("2011-12-31"),by="days"), A = runif(730,10,50))
colnames(mydata) = c("Date", "A")
DF = mydata %>%
     mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
     filter(Month == 11 | Month == 12 | Month == 01 | Month == 02 | Month == 03 | Month == 04)

我试图对从11月开始的数据进行重新排序,然后是12月,然后是01、02、03和04.我使用了代码 factor(Month,levels = c(11,12,01,02,03,04))以及上面的代码,但是没有用.我想要一个从11月开始到4月结束的剧情.以下代码为我附上了情节

I tried to re-order the data starting at month 11 followed by month 12 and then month 01,02,03,and,04. I used the code factor(Month, levels = c(11,12,01,02,03,04)) along with the code above but it didn't work. I wanted a plot that starts at month Nov and ends on April. The following code gave me attached plot

ggplot(data = DF, aes(Month,A))+
  geom_bar(stat = "identity")+ facet_wrap(~Year, ncol = 2)

现在,该地块从1月开始一直到12月-我不想要这个.我希望剧情从11月开始,一直到4月.我尝试使用 scale_x_date(labels = date_format(%b",date_breaks ="month",name ="Month")来标记该图,该方法无效.任何帮助

Right now, the plot is starting at January all the way to December- I dont want this. I want the plot starting at November, and all the way to April. I tried to label the plot using scale_x_date(labels = date_format("%b", date_breaks = "month", name = "Month") which didn't work. Any help would

推荐答案

我在应用factor()之前将Month转换为字符,并且可以正常工作.

I converted Month to character before applying factor() and it worked.

DF = mydata %>%
  mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
  filter(Month %in% c(11, 12, 1, 2, 3, 4)) %>%
  mutate(Month = sprintf("%02d", Month)) %>%
  mutate(Month = factor(Month, levels = c("11","12","01","02","03","04")))

ggplot(data = DF, aes(Month,A))+
  geom_bar(stat = "identity")+ facet_wrap(~Year, ncol = 2)

输出:

这篇关于R中的季节性ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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