条形颜色由正值或负值区分的多面归一化条形图 [英] Multiple facet normalized bar graph with bar color differentiated by positive or negative value
问题描述
我有一个这样的数据框:
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)),异常=样本(-20:20,15,替换= TRUE))df <-df [-c(3,10),]库("zoo")df $ date<-as.yearmon(paste(df $ year,df $ month),format =%Y%b")
您会注意到某些日期可能会丢失,但是在大多数情况下,这是各种气候变量的时间序列数据.我希望每个气候变量都是一个方面.y轴将为 clim_var
列的每个级别绘制异常
.这样,我应该得到一个多面的图,每个图都与此类似:
我已经尝试过此代码(修改自
在我的真实数据集中,有很多年的数据,因此最干净的做法是只为Jan指定标签,然后将其他月份保留为不带标签的刻度.任何对此的见解将不胜感激.
校正后的数据帧,以使每个 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)),异常=样本(-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)),异常=样本(-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)),异常=样本(-20:20,36,replace = TRUE))df <-rbind(precip_mm,tmin)df <-rbind(df,tmax)df <-df [-c(3,10,50,100),]库("zoo")df $ date<-as.yearmon(paste(df $ year,df $ month),format =%Y%b")
注释中建议的调整
库(ggplot2)gg<-ggplot(df,aes(x =日期,y =异常))+geom_bar(stat ='identity',aes(fill = anomaly> 0),position ='dodge',col ='透明')+theme_bw()+ scale_fill_discrete(guide ='none')+实验室(x ='',y ='异常')gg + facet_grid(clim_var〜.)gg + scale_x_yearmon()
输出并没有针对每个 clim_var
,但是x轴已正确标记.
labels_month<-format(seq(from = min(df $ date),to =max(df $ date),通过="1个月"),%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),位置=道奇",col =透明")+theme_bw(base_size = 12)+scale_fill_discrete(guide ="none")+labs(x =",y =")+scale_x_date(expand = c(0.015,0.015),标签= labels_month,休息时间= x_breaks)+主题(axis.text.x = element_text(angle = 90,vjust = 0.5))facet_grid(climvar〜.,labeller = label_parsed,开关="y",标度='free_y')1
这是您要寻找的吗?
library(zoo)库(ggplot2)set.seed(123)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),年= c(rep(2001,12),rep(2002,12),rep(2003,12)),异常=样本(-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),年= c(rep(2001,12),rep(2002,12),rep(2003,12)),异常=样本(-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),年= c(rep(2001,12),rep(2002,12),rep(2003,12)),异常=样本(-20:20,36,replace = TRUE))df<-rbind(precip_mm,tmin)df<-rbind(df,tmax)df<-df [-c(3,10,50,100),]df $ date<-as.yearmon(paste(df $ year,df $ month),format =%Y%b")#为T和P设置漂亮的标签df $ clim_var<-factor(df $ clim_var,级别= c("tmax","tmin","precip_mm"),标签= c(expression("T" [max]〜anomaly〜(degree * C)),表达式("T" [min]〜异常〜(度* C)),expression(降水异常(" *"mm)")))# 阴谋p1<-ggplot(df,aes(x = factor(date),y = anomaly))+geom_col(aes(fill =异常> 0),位置=道奇",col =透明")+theme_bw(base_size = 12)+scale_fill_discrete(guide ="none")+labs(x =",y =")+facet_grid(clim_var〜.,labeller = label_parsed,开关="y",标度='free_y')+主题(strip.placement ='outside',strip.background = element_blank(),axis.text.x = element_text(角度= 90,垂直= 0.5,垂直= 1))1
如果要修改x轴,请选中此 answer
<代码> df $ date2<-as.Date(paste(df $ year,df $ month,"01",sep =-"),format ="%Y-%b-%d)p2<-ggplot(df,aes(x = date2,y = anomaly))+geom_col(aes(fill =异常> 0),位置=道奇",col =透明")+theme_bw(base_size = 12)+scale_fill_discrete(guide ="none")+labs(x =",y =")+facet_grid(clim_var〜.,labeller = label_parsed,开关="y",标度='free_y')+主题(strip.placement ='outside',strip.background = element_blank())+scale_x_date(date_breaks ="12个月",date_labels =%b-%Y")2
I have a data frame like so:
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")
You will notice that some dates may be missing, but for the most part it is time series data for various climate variables. I would like each climate variable to be a facet. The y axis will plot anomaly
for each level of of the clim_var
column. Such that I should get a multifacted figure which each graph similar to this:
I have tried this code (modified 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"))
The issue seems to be plotting the date. It's as though it is not being recognized as a date so data for each clim_var
take up 1/3 of the plot area and the x-axis is continuous values rather than dates. I want the output to have axis label that include the month and year like so...
In my real dataset there are many years of data, so it may be most clean to specify labels for only Jan and then leave other months as tick marks without labels. Any insight to that would be appreciated.
Edit:
Corrected data frame such that each clim-var
has multiple years of data
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")
Adjustments suggested in comments
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()
Output does not facet each clim_var
, however x-axis is correctly labeled.
Edit2:
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
Is this what you are looking for?
library(zoo)
library(ggplot2)
set.seed(123)
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), ]
df$date <- as.yearmon(paste(df$year, df$month), format = "%Y %b")
# Setup nice labels for T and P
df$clim_var <- factor(df$clim_var,
levels = c("tmax", "tmin", "precip_mm"),
labels = c(expression("T"[max]~anomaly~(degree*C)),
expression("T"[min]~anomaly~(degree*C)),
expression("Precip anomaly ("*"mm)")))
# Plot
p1 <- ggplot(df, aes(x = factor(date), y = anomaly)) +
geom_col(aes(fill = anomaly > 0),
position = "dodge",
col = "transparent") +
theme_bw(base_size = 12) +
scale_fill_discrete(guide = "none") +
labs(x = "", y = "") +
facet_grid(clim_var ~ .,
labeller = label_parsed,
switch = "y",
scales = 'free_y') +
theme(strip.placement = 'outside',
strip.background = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
p1
If you want to modify the x-axis, check this answer
df$date2 <- as.Date(paste(df$year, df$month, "01", sep = "-"), format = "%Y-%b-%d")
p2 <- ggplot(df, aes(x = date2, y = anomaly)) +
geom_col(aes(fill = anomaly > 0),
position = "dodge",
col = "transparent") +
theme_bw(base_size = 12) +
scale_fill_discrete(guide = "none") +
labs(x = "", y = "") +
facet_grid(clim_var ~ .,
labeller = label_parsed,
switch = "y",
scales = 'free_y') +
theme(strip.placement = 'outside',
strip.background = element_blank()) +
scale_x_date(date_breaks = "12 months", date_labels = "%b-%Y")
p2
这篇关于条形颜色由正值或负值区分的多面归一化条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!