【问题标题】:several plots (scatter plot of a specific variable vs other variables) in RR中的几个图(特定变量与其他变量的散点图)
【发布时间】:2017-06-13 22:32:29
【问题描述】:

如何修改此代码以将所有绘图放在一起并在一页上(循环功能)?在我的真实数据集中,我希望有因变量和 10 个自变量之间的散点图。因变量和每个IV之间的散点图。

plot(rock$area, rock$peri)
plot(rock$area, rock$shape)
plot(rock$area, rock$perm)

【问题讨论】:

  • 三个地块前,输入par(mfrow=c(1,3))
  • 最好不要使用函数名plot作为变量名。

标签: r ggplot2


【解决方案1】:

由于您使用ggplot2 标记了您的问题,我假设您对ggplot2 解决方案感兴趣。

rock <- data.frame(area  = sample(1:100, 10, replace = TRUE),
                   peri  = sample(1:100, 10, replace = TRUE),
                   shape = sample(1:100, 10, replace = TRUE), 
                   perm  = sample(1:100, 10, replace = TRUE))

现在我们可以整理数据(一列用于 y 变量名称,另一列用于 y 变量值)并使用 facets 为每个 y 变量创建单独的图。

library(tidyr)
library(ggplot2)
rock %>% 
  gather(yvar, val, -area) %>% 
  ggplot(aes(area, val)) + 
    geom_point() + 
    facet_grid(yvar ~ .)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多