R 多面标准化条形图,条形图颜色以正值或负值区分

R 多面标准化条形图,条形图颜色以正值或负值区分,r,ggplot2,plot,R,Ggplot2,Plot,我有这样一个数据框: 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,

我有这样一个数据框:

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")
这件事似乎在策划日期。这就好像它没有被识别为日期,因此每个
clim_var
的数据占据了绘图区域的1/3,x轴是连续值,而不是日期。我希望输出有轴标签,包括月份和年份这样

在我的真实数据集中,有很多年的数据,所以最好只为1月份指定标签,然后将其他月份作为没有标签的记号标记。对此有任何见解都将不胜感激

编辑:

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
修正数据框架,使每个
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")

如果您有大量的facet,并且它可以像这样解决您的缩放问题,那么使用
facet\u wrap
可能会更好:

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_wrap(clim_var~., scales= "free_x")


我更喜欢这种方法,因为它为我的日期轴提供了很大的灵活性。

这就是你想要的吗

图书馆(动物园)
图书馆(GG2)
种子集(123)

确切地说,我不确定我是否理解了
seq\u along()
。你是不是在试着去掉那些没有日期的空格?如果是这样,您可以将
yearmon
转换为打印因子。如果没有,请使用
x=date
+scale\u x\u yearmon()
(这是动物园的一个函数)。我已经编辑了我的帖子。读了你的评论后,我意识到我发布的数据中有一个错误。除此之外,我尝试了你对新数据的建议,但没有得到多方面的结果。你知道如何纠正吗?我想你的错误是,你在刻面后给一个对象分配了
gg
,所以你将比例添加到原始图形中,而不是刻面图形中。例如,您可能希望,
gg+facet\u grid(clim\u var~)+scale\u x\u yearmon()
注意,降水量和温度的单位是不同的,因此此图不会产生太大影响sense@Tung你是对的。应使用
free
选项,而不是
free\u x
。我只是在解决被问到的共享比例问题。这是正确的,它们是不同的单元。此外,对于长期数据,我更喜欢水平面进行比较。更适合于出版物和幻灯片中的>20年数据。@Danielle我不能对你的帖子发表评论,因为我的声誉不到50岁,但你忘了为
facet\u wrap添加
scales=“free”
选项。我已经根据你的建议添加了一个编辑,但是我想知道你是否可以帮助我找出为什么我不能每12个月对轴进行编号。
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
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_wrap(clim_var~., scales= "free_x")
df$date<- as.Date(paste(df$year, df$month, "1", sep="-"), format="%Y-%b-%d")

library(ggplot2)
library(scales)
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_wrap(clim_var~., scales= "free")+ scale_x_date(breaks = date_breaks("2 months"),labels = date_format("%m/%y"))