如何在同一方面下将多个 ggplot 图与不同的数据框结合起来?

How can I combine multiple ggplot graphs with different dataframes under the same facet?

所以,我有两个数据帧,它们生成两个 ggplots,它们具有我想要组合的相同方面

第一个数据帧产生以下 ggplot

Dataframe1

library(ggh4x)
library(ggnomics)
library(ggplot2)
library(data.table)

#dataframe
drug <- c("DrugA","DrugB1","DrugB2","DrugB3","DrugC1","DrugC2","DrugC3","DrugC4")
PR  <- c(18,430,156,0,60,66,113,250)
GR <- c(16,425,154,0,56,64,111,248)
PS  <- c(28,530,256,3,70,76,213,350)
GS <- c(26,525,254,5,66,74,211,348)
group<-c("n=88","n=1910","n=820","n=8","n=252","n=280","n=648","n=1186")
class<-c("Class A","Class B","Class B","Class B","Class C","Class C","Class C","Class C")
df <-data.frame(drug,group, class,PR,GR,PS,GS)

#make wide to long df
df.long <- melt(setDT(df), id.vars = c("drug","group","class"), variable.name = "type")

#Order of variables
df.long$type <- factor(df.long$type, levels=c("PR","GR","PS","GS"))
df.long$class <- factor(df.long$class, levels= c("Class B", "Class A", "Class C"))
df.long$group <- factor(df.long$group, levels= c("n=1910","n=820","n=8","n=88","n=252","n=280","n=648","n=1186"))
df.long$drug <- factor(df.long$drug, levels= c("DrugB1","DrugB2","DrugB3","DrugA","DrugC1","DrugC2","DrugC3","DrugC4"))

数据框 1 的 Ggplot


ggplot(df.long, aes(fill = type, x = drug, y = value)) + 
  geom_bar(aes(fill = type), stat = "identity", position = "dodge", colour="white") + 
  geom_text(aes(label = value), position = position_dodge(width = 1.2), vjust = -0.5)+ 
  scale_fill_manual(values = c("#fa9fb5","#dd1c77","#bcbddc","756bb1")) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 600)) +
  theme(title = element_text(size = 18), 
        legend.text = element_text(size = 12), 
        axis.text.x = element_text(size = 9), 
        axis.text.y =element_text(size = 15),
        plot.title = element_text(hjust = 0.5)) +
  ggh4x::facet_nested(.~class + group, scales = "free_x", space= "free_x")

这是第二个数据帧

#dataframe 2
drug <- c("DrugA","DrugB1","DrugB2","DrugB3","DrugC1","DrugC2","DrugC3","DrugC4")
Sens  <- c(0.99,0.97,NA,0.88,0.92,0.97,0.98,0.99)
Spec <- c(1,0.99,1,0.99,0.99,0.99,0.99,1)
class<-c("Class A","Class B","Class B","Class B","Class C","Class C","Class C","Class C")
df2 <-data.frame(drug,class,Sens,Spec)

#wide to long df2
df2.long <- melt(setDT(df2), id.vars = c("drug","class"), variable.name = "type")

#additional variables
df2.long$UpperCI <- c(0.99,0.99,NA,0.98,0.98,0.99,0.99,0.99,1,1,1,1,1,1,1,1)
df2.long$LowerCI  <- c(0.97,0.98,NA,0.61,0.83,0.88,0.93,0.97,0.99,0.99,0.99,0.99,0.98,0.99,0.99,0.99)

#order of variables
df2.long$class <- factor(df2.long$class, levels= c("Class B", "Class A", "Class C"))

数据框 2 的 Ggplot

ggplot(df2.long, aes(x=drug, y=value, group=type, color=type)) + 
  geom_line() +
  geom_point()+
  geom_errorbar(aes(ymin=LowerCI, ymax=UpperCI), width=.2,
                position=position_dodge(0.05)) +
  scale_y_continuous(labels=scales::percent)+
  labs(x="drug", y = "Percentage")+
  theme_classic() +
  scale_color_manual(values=c('#999999','#E69F00')) + 
  theme(legend.text=element_text(size=12), 
        axis.text.x=element_text(size=9), 
        axis.text.y =element_text(size=15),
        panel.background = element_rect(fill = "whitesmoke"))+
  facet_wrap(facets = vars(class),scales = "free_x")

所以我试图在一个方面(来自数据框 1 的那个)下合并两个图,到目前为止我已经完成了以下工作

ggplot(df.long)+
  aes(x=drug, y=value,fill = type)+
  geom_bar(, stat = "identity", position = "dodge", colour="white") + 
  geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.5, size=2) + 
  scale_fill_manual(breaks=c("PR","GR","PS","GS"),
                    values=c("#dd1c77","#756bb1","#fa9fb5","#e7e1ef","black","black")) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100),sec.axis=sec_axis(~./10, labels = function(b) { paste0(b, "%")},name="Percentage")) + #remove space between x axis labels and bottom of chart
  theme(legend.text=element_text(size=12), 
        legend.position = 'bottom',
        axis.text.x=element_text(size=9), 
        axis.text.y =element_text(size=15),
        panel.background = element_rect(fill = "whitesmoke"), #color of plot background
        panel.border = element_blank(), #remove border panels of each facet
        strip.background = element_rect(colour = NA)) +  #remove border of strip
  labs(y = "Number of isolates", fill = "")+
  geom_errorbar(data=df2.long,aes(x=drug, y=value*1000,ymin=LowerCI*1000, ymax=UpperCI*1000,color=type), width=.2,
                position=position_dodge(0.05))+
  geom_point(data=df2.long,aes(x=drug,y=value*1000,color=type),show.legend = F)+
  geom_line(data=df2.long, aes(x=drug, y=value*1000, group=type, color=type)) +
  scale_color_manual(values=c('#999999','#E69F00'))

但我坚持从 plot1 添加方面。我希望任何人都可以提供帮助:)

对于这种特定情况,我认为嵌套方面不是合适的解决方案,因为 n = ... 似乎是 x-axis 组的元数据,而不是 类 的子类别.

以下是如何使用 facet_grid() 绘制数据:

ggplot(df.long, aes(drug, value, fill = type)) +
  geom_col(position = "dodge") +
  geom_text(aes(label = value), 
            position = position_dodge(0.9),
            vjust = -0.5, size = 2) +
  geom_errorbar(data = df2.long,
                aes(y = value * 1000, color = type,
                    ymin = LowerCI * 1000, ymax = UpperCI * 1000),
                position = position_dodge(0.05), width = 0.2) +
  geom_point(data = df2.long,
             aes(y = value * 1000, color = type), 
             show.legend = FALSE) +
  geom_line(data = df2.long,
            aes(y = value * 1000, group = type, color = type)) +
  scale_fill_manual(breaks = c("PR", "GR", "PS", "GS"),
                    values=c("#dd1c77","#756bb1","#fa9fb5","#e7e1ef","black","black")) +
  scale_color_manual(values=c('#999999','#E69F00')) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 1100),
                     sec.axis = sec_axis(~ ./10,
                                         labels = function(b) {
                                           paste0(b, "%")
                                         }, name = "Percentage")) +
  scale_x_discrete(
    labels = levels(interaction(df.long$drug, df.long$group, sep = "\n"))
  ) +
  facet_grid(~ class, scales = "free_x", space = "free_x") +
  theme(legend.text=element_text(size=12), 
        legend.position = 'bottom',
        axis.text.x=element_text(size=9), 
        axis.text.y =element_text(size=15),
        panel.background = element_rect(fill = "whitesmoke"), #color of plot background
        panel.border = element_blank(), #remove border panels of each facet
        strip.background = element_rect(colour = NA)) 

如果您坚持要包括 n = ... 标签,也许更好的方法是将它们添加为文本,即添加以下内容:

  stat_summary(fun = sum,
               aes(group = drug, y = stage(value, after_stat = -50),
                   label = after_stat(paste0("n = ", y))),
               geom = "text") +

并将 y-axis 限制设置为 c(-100, 1000) 例如。