column plot that I would like to colour according to season我想在计数和日期的多面图中为季节(春季/秋季)指定颜色。 我有这种格式的数据:
Date = c("06/13/2011", "10/26/2011", "05/28/2012", "11/11/2012", "05/25/2013", "10/31/2013", "06/09/2014", "11/03/2014", "05/14/2015", "10/24/2015", "05/03/2016", "10/13/2016", "05/24/2017", "10/16/2017", "06/09/2018", "10/05/2018")
Date = as.Date(Date, format = "%m/%d/%Y")
y0_c = c(159375000, 29403750000, 0, 3665625000, 0, 25790000000, 0, 18636250000, 0, 13421875000, 0, 26098125000, 0, 1440625000, 82500000, 23101250000)
y1_c = c(1794375000, 313125000, 12065625000, 3148125000, 2883750000, 372500000, 2872500000, 790000000, 7604375000, 3496875000, 4042500000, 830000000, 20070000000, 10648750000, 771250000, 328750000)
y2_c = c(317500000, 42500000, 33750000, 62500000, 176875000, 331875000, 166250000, 54375000, 260000000, 116250000, 1068125000, 290000000, 438125000, 160000000, 2686250000, 1072500000)
y3_c = c(5625000, 5000000, 5625000, 1875000, 2500000, 4375000, 18125000, 7500000, 11875000, 22500000, 51250000, 37500000, 16250000, 31250000, 60000000, 38125000)
df = data.frame(Date, y0_c, y1_c, y2_c, y3_c)
df_melt = melt(df, id.vars = 'Date')
ggplot(dfc_melt, aes(x = Date, y = value)) + geom_col() + facet_wrap(~ variable, scales = 'free_y', ncol = 1)
这给了我如下图所示的情节。
我想根据季节为条形着色,并尝试使用以下方法:
Season = c("spring", "Summer", "Spring"......)
,但这会产生警告消息:
度量变量之间的属性并不相同;他们会
...我有点迷茫!
如果能提供帮助,请先感谢
答案 0 :(得分:0)
我知道您的意思以及问题所在。对我来说,您似乎在努力制作新的季节列。即使我错了,锻炼一下也很有趣。
对我来说,关键是要检查数据框中的日期是哪个季节。加载两个必需的软件包 tidyverse 和 reshape2 后,定义了春季,夏季,秋季和冬季的开始日期,然后是进行上述检查的功能。然后使用此功能创建一个新的季节列。
此功能利用了 tidyverse 中包含的软件包 lubridate 。
融化后,此数据帧随 Date 和 season 的id.vars一起传输。 ggplot然后使用 geom_col 制作条形图,因为我相信这是您想要的内容。新的数据框应该为您提供一个良好的平台来创建其他版本的绘图。
由于您的原始数据仅包含春季或秋季的日期,因此我将一个日期更改为一个夏季日期以进行检查
library(tidyverse)
library(reshape2)
Date = c("06/13/2011", "10/26/2011", "07/28/2012", "11/11/2012", "05/25/2013",
"10/31/2013", "06/09/2014", "11/03/2014", "05/14/2015", "10/24/2015", "05/03/2016",
"10/13/2016", "05/24/2017", "10/16/2017", "06/09/2018", "10/05/2018")
Date = as.Date(Date, format = "%m/%d/%Y")
y0_c = c(159375000, 29403750000, 0, 3665625000, 0, 25790000000, 0, 18636250000, 0,
13421875000, 0, 26098125000, 0, 1440625000, 82500000, 23101250000)
y1_c = c(1794375000, 313125000, 12065625000, 3148125000, 2883750000, 372500000,
2872500000, 790000000, 7604375000, 3496875000, 4042500000, 830000000, 20070000000,
10648750000, 771250000, 328750000)
y2_c = c(317500000, 42500000, 33750000, 62500000, 176875000, 331875000, 166250000,
54375000, 260000000, 116250000, 1068125000, 290000000, 438125000, 160000000,
2686250000, 1072500000)
y3_c = c(5625000, 5000000, 5625000, 1875000, 2500000, 4375000, 18125000, 7500000,
11875000, 22500000, 51250000, 37500000, 16250000, 31250000, 60000000, 38125000)
winterStart <- "12-21"
springStart <- "03-23"
summerStart <- "06-21"
autumnStart <- "09-21"
season <- function(d) ifelse(d > ymd(paste(year(d), springStart, sep='-' )) &
d < ymd(paste(year(d),summerStart, sep='-')), 'spring',
ifelse(d > ymd(paste(year(d), summerStart, sep='-' )) &
d < ymd(paste(year(d),autumnStart, sep='-')), 'summer',
ifelse(d > ymd(paste(year(d), autumnStart, sep='-' )) &
d < ymd(paste(year(d),winterStart, sep='-')), 'autumn','winter')))
df = data.frame(Date, y0_c, y1_c, y2_c, y3_c)
df$season <- season(df[,1])
df.melt <- melt(df, id.vars = c("Date", 'season'))
df.melt <- melt(df, id.vars = c("Date", 'season'))
ggplot(df.melt, aes(Date, value, fill=season))+geom_col()
希望这是您正在寻找的东西。