簡體   English   中英

如何在同一方面將多個 ggplot 圖與不同的數據框結合起來?

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

所以,我有兩個數據框,它們產生兩個具有相同方面的 ggplots 我想組合

第一個 dataframe 產生以下 ggplot

數據框1

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"))

dataframe 的 ggplot 1


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

#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"))

dataframe 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")

所以我試圖將這兩個圖結合在一個方面(來自 dataframe 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 軸組的元數據,而不是類的子類別。

這是您如何使用facet_grid()代替 plot 數據的方法:

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 軸范圍設置為c(-100, 1000)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM