【问题标题】:Multiple scatter plots in a single figure from multiple data frame in R using ggplot?使用ggplot从R中的多个数据框中的单个图中的多个散点图?
【发布时间】:2019-05-30 16:08:31
【问题描述】:

我想生成一个图形,使用来自两个数据帧的数据(即,将 Data1 的 A 列与 Data2 的 A 列回归)显示该单个图形上的所有散点图。图中的每个图都应显示 R 平方和 p 值。我更想知道如何在从多个数据帧中获取数据时使用ggplotfact_wrap 函数。 我尝试了几种方法,但都没有成功。

library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))

#method-1: using plot functions
par(mfrow=c(3,1))
plot(Data1$A, Data2$A)
abline(lm(Data1$A ~ Data2$A))
plot(Data1$B, Data2$B)
abline(lm(Data1$B ~ Data2$B))
plot(Data1$C, Data2$C)
abline(lm(Data1$C ~ Data2$C))
dev.off()

#method-2: using ggplot
ggplot()+
  geom_point(aes(Data1$A,Data2$A))

我想要一个像下面这样的图

【问题讨论】:

  • Don't use $ inside your aes。将您的数据框绑定在一起并使用facet_wrap——查看构面文档
  • 两个数据框中的列名是相同的 - 所以将它们组合起来会产生问题??
  • @GiovanaStein,这有助于但并没有完全解决我的问题 - 我正在努力添加趋势线以及在每个图的顶部指示 p 值和 r 平方值
  • 您可以使用适当的名称将它们逐列绑定(cbinddplyr::bind_cols)。或根据标识符加入

标签: r ggplot2 annotations regression scatter-plot


【解决方案1】:

最困难的部分是整理您的数据。完成后,情节就很简单了。

    library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))


data <- Data1 %>% 
  #add columns to indicate the source and the observation number
  mutate(source = "Data1",
         obs = row_number()) %>% 
  #bind to Data2 with the same new columns
  bind_rows(Data2 %>% mutate(source = "Data2", obs = row_number())) %>% 
  #tidy the data so we've got a column for Data1 and Data2 and an indicator for the series (A, B, C)
  gather(A, B, C, key = series, value = value) %>% 
  spread(key = source, value = value)

#create a separate data frame for annotations, finding the "top left" corner of each series
annotations <- data %>% 
  group_by(series) %>% 
  summarise(x = min(Data1),
            y = max(Data2)) %>% 
  mutate(label = c("P = 0.6", "P = 0.5", "P = 0.9"))

#plot the data, faceting by series
data %>% 
  ggplot(aes(Data1, Data2))+
  geom_point() + 
  geom_smooth(method = "lm", se = FALSE) + 
  facet_grid(series~., scales = "free") +
  #add the annotations with adjustments to the horiz & vert placement
  geom_text(data = annotations, aes(x = x, y = y, label = label, hjust = 0, vjust = 1), 
           color = "red", fontface = "italic")

【讨论】:

  • @Jordo88,@camille,我尝试使用grob &lt;- grobTree(textGrob(c(P=0.6, P=0.5,P=0.9), x=0.1, y=0.95, hjust=0, gp=gpar(col="red", fontsize=13, fontface="italic"))) 后跟annotation_custom(grob) 向图中的每个绘图添加文本。但是,它对所有图使用第一个值 (P=0.6)。
  • 谢谢@Jordo82,请看下面的结果。有没有办法让文本出现在每个图的左上角?您关于文本添加的代码很好,但是,它取决于具有相似数字的统一数据的规模,但是,如果数据中存在较大的可变性,例如在我的真实情况下(参见 Y 轴图)- 这可能不是一个理想的方式。
  • @Hydrologist,经过编辑以使注释文本的位置成为系列的功能
【解决方案2】:

您可以制作一个绘图列表,然后使用 grid.arrange() 函数。

sc_plots = list()

sc_plots$sc1 = ggplot() + ...
sc_plots$sc2 = ggplot() + ...

grid.arrange(sc_plots$sc1, sc_plots$sc2,
 ncol = 3)

【讨论】:

    【解决方案3】:

    @Jordo82,这是我尝试在数字上插入文本时得到的结果。有没有一种方法可以释放 Y 轴,使添加的文本不依赖于 y 比例,而是出现在每个图的左上角。我使用 annotate_custom 的原因是它不依赖于 y 比例,但缺点是我只会使用标签中的第一个文本。我的真实价值观彼此如此不同 - 请参阅附图的 Y 比例。

    我在编辑放置坐标时使用了你的代码

     annotate("text", -1.5, 800, label = c("P = 0.6", "P = 0.5", "P = 0.9", "P = 0.9"), 
                 color = "red", fontface = "italic")
    

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 2014-04-18
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多