R中的季节性ggplot?
seasonal ggplot in R?
我正在查看从 11 月到 4 月的数据,并希望绘制从 11 月到 4 月的数据。下面是我筛选兴趣月份的示例代码。
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 月结束的情节。下面的代码给了我附加的情节
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity")+ facet_wrap(~Year, ncol = 2)
现在,剧情从一月开始一直到十二月——我不想这样。我希望情节从十一月开始,一直到四月。我尝试使用 scale_x_date(labels = date_format("%b", date_breaks = "month", name = "Month")
标记绘图,但没有用。任何帮助都会
我在应用 factor() 之前将 Month 转换为字符并且它起作用了。
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)
输出:
user2332849 答案很接近,但确实引入了错误。酒吧的顺序不正确。例如,对于 2010 年,它显示的是年初数据之前的 11 月和 12 月数据。为了以正确的顺序绘制年份将需要调整,以便日历从第 11 个月开始到第 4 个月。
#Convert month to Factor and set desired order
DF$Month<- factor(DF$Month, levels=c(11, 12, 1, 2, 3, 4))
#Adjust the year to match the year of the beginning of series
#For example assign Jan, Feb, Mar and April to prior year
DF$Year<-ifelse(as.integer(as.character(DF$Month)) <6, DF$Year-1, DF$Year)
#plot
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity") +
facet_wrap(~Year, ncol = 3)
在下面的图中,2010 年前 4 个月变成了前一年的最后 4 个月。 2011 年的最后 2 个月已经为 2012 年的前 4 个月做好了准备。
我正在查看从 11 月到 4 月的数据,并希望绘制从 11 月到 4 月的数据。下面是我筛选兴趣月份的示例代码。
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 月结束的情节。下面的代码给了我附加的情节
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity")+ facet_wrap(~Year, ncol = 2)
现在,剧情从一月开始一直到十二月——我不想这样。我希望情节从十一月开始,一直到四月。我尝试使用 scale_x_date(labels = date_format("%b", date_breaks = "month", name = "Month")
标记绘图,但没有用。任何帮助都会
我在应用 factor() 之前将 Month 转换为字符并且它起作用了。
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)
输出:
user2332849 答案很接近,但确实引入了错误。酒吧的顺序不正确。例如,对于 2010 年,它显示的是年初数据之前的 11 月和 12 月数据。为了以正确的顺序绘制年份将需要调整,以便日历从第 11 个月开始到第 4 个月。
#Convert month to Factor and set desired order
DF$Month<- factor(DF$Month, levels=c(11, 12, 1, 2, 3, 4))
#Adjust the year to match the year of the beginning of series
#For example assign Jan, Feb, Mar and April to prior year
DF$Year<-ifelse(as.integer(as.character(DF$Month)) <6, DF$Year-1, DF$Year)
#plot
ggplot(data = DF, aes(Month,A))+
geom_bar(stat = "identity") +
facet_wrap(~Year, ncol = 3)
在下面的图中,2010 年前 4 个月变成了前一年的最后 4 个月。 2011 年的最后 2 个月已经为 2012 年的前 4 个月做好了准备。