【发布时间】:2019-04-11 07:13:58
【问题描述】:
我有一个像这样的数据框:
df<- data.frame(month= rep(c("Jan", "Feb", "Mar", "Apr", "May"), 3),
year= c(seq(2001:2003,5), rep(2002, 5), rep(2003, 5)),
clim_var= c(rep("precip_mm", 5), rep("tmin",5), rep("tmax", 5)),
anomaly= sample(-20:20, 15, replace = TRUE))
df<-df[-c(3,10),]
library("zoo")
df$date<- as.yearmon(paste(df$year, df$month), format= "%Y %b")
您会注意到有些日期可能会丢失,但大部分是各种气候变量的时间序列数据。我希望每个气候变量都是一个方面。 y 轴将为clim_var 列的每个级别绘制anomaly。这样我应该得到一个多因素的图,每个图都与此类似:
我试过这段代码(修改from)
library(ggplot2)
gg<- ggplot(df, aes(x= seq_along(date), y = anomaly)) +
geom_bar(stat = 'identity', aes(fill = anomaly>0), position = 'dodge', col =
'transparent') +
theme_bw() + scale_fill_discrete(guide = 'none') +
labs(x = '', y = 'anomaly')
gg + facet_grid(clim_var~.)
gg+ scale_x_datetime(labels = date_format("%b %Y"))
问题似乎是在绘制日期。就好像它没有被识别为日期,所以每个clim_var 的数据占绘图区域的 1/3,x 轴是连续值而不是日期。我希望输出具有包含月份和年份的轴标签......
在我的真实数据集中有很多年的数据,所以最好只为一月指定标签,然后将其他月份作为不带标签的刻度线。对此的任何见解将不胜感激。
编辑:
更正的数据框使得每个clim-var 都有多年的数据
precip_mm<- data.frame(clim_var= rep("precip_mm",36), month= rep(c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ), 3),
year= c(rep(2001,12),rep(2002,12), rep(2003, 12)),
anomaly= sample(-20:20, 36, replace = TRUE))
tmin<- data.frame(clim_var= rep("tmin",36), month= rep(c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ), 3),
year= c(rep(2001,12),rep(2002,12), rep(2003, 12)),
anomaly= sample(-20:20, 36, replace = TRUE))
tmax<- data.frame(clim_var= rep("tmax",36), month= rep(c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ), 3),
year= c(rep(2001,12),rep(2002,12), rep(2003, 12)),
anomaly= sample(-20:20, 36, replace = TRUE))
df<- rbind(precip_mm, tmin)
df<-rbind(df, tmax)
df<-df[-c(3,10, 50, 100),]
library("zoo")
df$date<- as.yearmon(paste(df$year, df$month), format= "%Y %b")
cmets 中建议的调整
library(ggplot2)
gg<- ggplot(df, aes(x= date, y = anomaly)) +
geom_bar(stat = 'identity', aes(fill = anomaly>0), position = 'dodge', col =
'transparent') +
theme_bw() + scale_fill_discrete(guide = 'none') +
labs(x = '', y = 'anomaly')
gg + facet_grid(clim_var~.)
gg+ scale_x_yearmon()
输出不面向每个clim_var,但 x 轴已正确标记。
编辑2:
labels_month <- format(seq(from = min(df$date), to =
max(df$date), by = "1 months"), "%Y-%b")
labels_month[rep(c(FALSE, TRUE), c(1, 11))] <- ""
labels_month<- as.Date(labels_month, format= "%Y-%b")
x_breaks <- seq(min(df$date), max(df$date), by = "1 months")
p1 <- ggplot(df, aes(x = factor(date), y = df)) +
geom_col(aes(fill = anomoly > 0),
position = "dodge",
col = "transparent") +
theme_bw(base_size = 12) +
scale_fill_discrete(guide = "none") +
labs(x = "", y = "") +
scale_x_date(expand = c(0.015, 0.015),
labels = labels_month,
breaks = x_breaks) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
facet_grid(climvar ~ .,
labeller = label_parsed,
switch = "y",
scales = 'free_y')
p1
【问题讨论】:
-
我不确定我是否理解
seq_along()。您是否想摆脱缺少日期的空格?如果是这样,您可以将yearmon转换为绘图因子。如果没有,请使用x = date并使用+ scale_x_yearmon()(这是来自 zoo 的函数)。 -
我已经编辑了我的帖子。阅读您的评论后,我意识到我发布的数据中有错误。除此之外,我已经尝试过您对新数据的建议,但没有得到多方面的输出。知道如何纠正吗?
-
我认为您的错误是您确实在分面后将
gg分配给了一个对象,因此您将比例添加到原始图形而不是分面图形。你会想要,例如,gg + facet_grid(clim_var ~ .) + scale_x_yearmon()