【问题标题】:How do I combine scatterplots to form a scatterplot matrix with common X axis for varied Y axis?如何组合散点图以形成具有公共 X 轴的散点图矩阵以用于不同的 Y 轴?
【发布时间】:2021-01-09 05:12:57
【问题描述】:

我的情节是这样的

这是我尝试过的。我制作了单独的散点图并将它们与grid.arrange 组合在一起。

data(methylmercurydata) 
p1 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=logTHg)) + geom_point()
p2 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=OM)) + geom_point()
p3 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=FeRB)) + geom_point()
grid.arrange(p1,p2,p3)

【问题讨论】:

  • 那么您到底希望它是什么样子?如果您包含reproducible example,则更容易为您提供帮助。 methylmercurydata 来自哪个包?如果您使用ggplot2,您可能想要重新塑造您的数据,然后进行分面

标签: r ggplot2 scatter-plot scatter-matrix


【解决方案1】:

最简单的方法可能是在创建更长的表后使用dplyr::facet_wrap()

类似:

library(tidyverse)

methylmercurydata %>%
    pivot_longer(cols = c(logTHg, OM, FeRB), names_to = 'metric') %>%
    ggplot() +
    geom_point(aes(MeHG, value)) +
    facet_wrap(~metric)

编辑:r2evans 提出了一个很好的观点;如果您需要跨 y 的单独比例,您可以在 facet_wrap 调用中使用 scales = 'free_y'。同样,scales = 'free_x' 将提供不同的 x 轴,scales = 'free' 将为每个轴提供不同的比例。重新创建上述绘图时要考虑的另一件事是指定 ncol 参数,在本例中为 ncol = 1

【讨论】:

  • 由于 y 轴有点不同,最好使用facet_wrap(~ metric, scales = "free_y")
  • @r2evans 好点。我编辑了我的回复以包含 scales 参数以及 ncol 参数。
【解决方案2】:

我不清楚你所说的“散点图矩阵”是什么意思,但如果你想制作一个相关矩阵,你可以使用 ggforce(例如每个https://ihaddadenfodil.com/post/it-s-a-bird-it-s-a-plane-it-s-a-ggforce-function/):

library(tidyverse)
library(ggforce)
library(palmerpenguins)

ggplot(penguins, aes(col = sex)) +
  geom_autopoint(na.rm = TRUE) +
  facet_matrix(rows = vars(bill_length_mm:body_mass_g),
               switch = "x")

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2019-07-07
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多