修改代码以在我的图形上堆叠第三个条形图

问题描述 投票:0回答:1

我有以下数据框和代码..

rG_activity <- c("-0.409", "-0.409", NA, "0.268", "0.268", "0.227", "0.092", "-0.220")
rG_activity_error_intervals <- c("(-0.556, -0.263)", "(-0.556, -0.263)", NA, "(0.222, 0.602)", "(0.282, 0.752)", "(0.229, 0.726)", "(-0.259, 0.220)", "(-0.222, 0.252)")
rG_task_persistence <- c("-0.409", "-0.409", "-0.209", "-0.262", "-0.225", "-0.292", "-0.062", "-0.292")
rG_task_persistence_error_intervals <- c("(-0.556, -0.263)", "(-0.556, -0.263)", "(-0.556, -0.262)", "(-0.679, -0.222)", "(-0.560, -0.222)", "(-0.296, -0.086)", "(-0.272, 0.227)", "(-0.507, -0.072)")
rE_activity <- c("-0.098", "-0.098", NA, "0.256", "-0.225", "-0.052", "-0.022", "0.058")
rE_activity_error_intervals <- c("(-0.261, 0.064)", "(-0.261, 0.064)", NA,"(-0.022, 0.222)", "(-0.298, 0.028)", "(-0.228, 0.220)", "(-0.266, 0.220)", "(-0.225, 0.222)")
rE_task_persistence <- c("-0.098", "-0.098", "-0.098", "0.092", "-0.002", "0.202", "0.027", "0.205")
rE_task_persistence_error_intervals <- c("(-0.261, 0.064)", "(-0.261, 0.064)", "(-0.262, 0.062)", "(-0.065, 0.252)", "(-0.272, 0.270)", "(-0.065, 0.272)", "(-0.222, 0.206)", "(-0.072, 0.280)")
age <- c("intercept", "slope", "36","30", "24", "18", "12", "6")
df <- data.frame(age, rG_activity, rG_activity_error_intervals, rG_task_persistence, rG_task_persistence_error_intervals, rE_activity, rE_activity_error_intervals, rE_task_persistence, rE_task_persistence_error_intervals)

library(data.table)
setDT(df)

df_tidy <- melt(df , measure.vars = list(values=c("rG_activity","rG_task_persistence","rE_activity","rE_task_persistence"),
                              intervals=c("rG_activity_error_intervals","rG_task_persistence_error_intervals","rE_activity_error_intervals","rE_task_persistence_error_intervals")))
df_tidy[ , values:=as.numeric(values)]
df_tidy[ , c("lci", "uci") := tstrsplit(gsub("[()]","",intervals),split=",",type.convert = TRUE)]
df_tidy[ , condition := c("rG_activity", "rG_persistence", "rE_activity", "rE_persistence")[variable]]
df_tidy[ , c("what","type") := tstrsplit(condition,split="_")]

ggplot(df_tidy) + 
  aes(x=age, y=values, ymin=lci, ymax=uci,fill=what) + 
  geom_col(position = "dodge") + 
  geom_errorbar(position=position_dodge(width=0.9),width=0.25) + 
  facet_wrap(~type, ncol=1) + 
  scale_fill_manual(values=c("darkgrey", "lightgrey"))+
  geom_text(size=2.75,aes(label=values,y=if_else(values > 0, uci, lci) + .1 * if_else(values > 0, 1, -1)),
            position=position_dodge(width=0.9))+
  scale_x_discrete(limits = c("6", "12", "18", "24", "30", "36", "slope", "intercept"),
                   labels = c("6"="Age 6", "12"="Age 12", "18"="Age 18", "24"="Age 24", "30"="Age 30", "36"="Age 36", "slope", "intercept"))

产生这个数字...

我正在尝试在上面的两个条形图之上为一个新变量(我们称之为“情绪”)堆叠另一个条形图。下面是包含情绪变量数据的新 df。

rG_activity <- c("-0.409", "-0.409", NA, "0.268", "0.268", "0.227", "0.092", "-0.220")
rG_activity_error_intervals <- c("(-0.556, -0.263)", "(-0.556, -0.263)", NA, "(0.222, 0.602)", "(0.282, 0.752)", "(0.229, 0.726)", "(-0.259, 0.220)", "(-0.222, 0.252)")
rG_task_persistence <- c("-0.409", "-0.409", "-0.209", "-0.262", "-0.225", "-0.292", "-0.062", "-0.292")
rG_task_persistence_error_intervals <- c("(-0.556, -0.263)", "(-0.556, -0.263)", "(-0.556, -0.262)", "(-0.679, -0.222)", "(-0.560, -0.222)", "(-0.296, -0.086)", "(-0.272, 0.227)", "(-0.507, -0.072)")
rE_activity <- c("-0.098", "-0.098", NA, "0.256", "-0.225", "-0.052", "-0.022", "0.058")
rE_activity_error_intervals <- c("(-0.261, 0.064)", "(-0.261, 0.064)", NA,"(-0.022, 0.222)", "(-0.298, 0.028)", "(-0.228, 0.220)", "(-0.266, 0.220)", "(-0.225, 0.222)")
rE_task_persistence <- c("-0.098", "-0.098", "-0.098", "0.092", "-0.002", "0.202", "0.027", "0.205")
rE_task_persistence_error_intervals <- c("(-0.261, 0.064)", "(-0.261, 0.064)", "(-0.262, 0.062)", "(-0.065, 0.252)", "(-0.272, 0.270)", "(-0.065, 0.272)", "(-0.222, 0.206)", "(-0.072, 0.280)")
age <- c("intercept", "slope", "36", "30", "24", "18", "12", "6")

#new data
rG_emotionality <- c("-0.409", "-0.409", NA, "0.268", "0.268", "0.227", "0.092", "-0.220")
rG_emotionality_error_intervals <- c("(-0.556, -0.263)", "(-0.556, -0.263)", NA, "(0.222, 0.602)", "(0.282, 0.752)", "(0.229, 0.726)", "(-0.259, 0.220)", "(-0.222, 0.252)")
rE_emotionality <- c("-0.098", "-0.098", NA, "0.256", "-0.225", "-0.052", "-0.022", "0.058")
rE_emotionality_error_intervals <- c("(-0.261, 0.064)", "(-0.261, 0.064)", NA,"(-0.022, 0.222)", "(-0.298, 0.028)", "(-0.228, 0.220)", "(-0.266, 0.220)", "(-0.225, 0.222)")

#create new df
df <- data.frame(age, rG_activity, rG_activity_error_intervals, rG_task_persistence, rG_task_persistence_error_intervals, rE_activity, rE_activity_error_intervals, rE_task_persistence, rE_task_persistence_error_intervals, rG_emotionality, rG_emotionality_error_intervals, rE_emotionality, rE_emotionality_error_intervals)

我想更改我的初始代码,使其生成一个图形,其中包含堆叠在前两个上的第三个“情感”条形图。

r ggplot2 bar-chart
1个回答
0
投票

在不知道你想要的情节是什么样子的情况下(这不是问题的重点),我建议使用 ggpubr 包中的

ggarrange

install.packages('ggpubr')
p1 <- ggplot(df_tidy) + 
  aes(x=age, y=values, ymin=lci, ymax=uci,fill=what) + 
  geom_col(position = "dodge") + 
  geom_errorbar(position=position_dodge(width=0.9),width=0.25) + 
  facet_wrap(~type, ncol=1) + 
  scale_fill_manual(values=c("darkgrey", "lightgrey"))+
  geom_text(size=2.75,aes(label=values,y=if_else(values > 0, uci, lci) + .1 * if_else(values > 0, 1, -1)),
            position=position_dodge(width=0.9))+
  scale_x_discrete(limits = c("6", "12", "18", "24", "30", "36", "slope", "intercept"),
                   labels = c("6"="Age 6", "12"="Age 12", "18"="Age 18", "24"="Age 24", "30"="Age 30", "36"="Age 36", "slope", "intercept"))


p2 <- ggplot(df2) + 
  aes(x=age, y=rG_emotionality) + 
  geom_col(position = "dodge")  
  
ggpubr::ggarrange(plotlist = list(p2,p1),ncol = 1)
© www.soinside.com 2019 - 2024. All rights reserved.