RStudio 一个数值变量对 n 个图中的 n 个数值变量
RStudio one numeric variable against n numeric variables in n plots
我是 eviews 用户,eviews 非常基本地绘制散点图矩阵。
在下图中,我有 13 个不同的组数据,Eviews 在一张带有回归线的图中的 12 个图中绘制了一组数据与 12 组数据的对比。
如何使用 Rstudio 实现相同的图形?
这是一个关于如何在 ggplot 中绘制所请求的图的示例:
首先是一些数据:
z <- matrix(rnorm(1000), ncol= 10)
这里的基本思想是将宽矩阵转换为长格式,其中与所有其他变量进行比较的变量被复制的次数与其他变量的复制次数一样多。这些其他变量中的每一个在键列中都有一个特定的标签。 ggplot 喜欢这种格式的数据
library(tidyverse)
z %>%
as.tibble() %>% #convert matrix to tibble or data.frame
gather(key, value, 2:10) %>% #convert to long format specifying variable columns 2:10
mutate(key = factor(key, levels = paste0("V", 1:10))) %>% #specify levels so the facets go in the correct order to avoid V10 being before V2
ggplot() +
geom_point(aes(value, V1))+ #plot points
geom_smooth(aes(value, V1), method = "lm", se = F)+ #plot lm fit without se
facet_wrap(~key) #facet by key
我是 eviews 用户,eviews 非常基本地绘制散点图矩阵。
在下图中,我有 13 个不同的组数据,Eviews 在一张带有回归线的图中的 12 个图中绘制了一组数据与 12 组数据的对比。
如何使用 Rstudio 实现相同的图形?
这是一个关于如何在 ggplot 中绘制所请求的图的示例:
首先是一些数据:
z <- matrix(rnorm(1000), ncol= 10)
这里的基本思想是将宽矩阵转换为长格式,其中与所有其他变量进行比较的变量被复制的次数与其他变量的复制次数一样多。这些其他变量中的每一个在键列中都有一个特定的标签。 ggplot 喜欢这种格式的数据
library(tidyverse)
z %>%
as.tibble() %>% #convert matrix to tibble or data.frame
gather(key, value, 2:10) %>% #convert to long format specifying variable columns 2:10
mutate(key = factor(key, levels = paste0("V", 1:10))) %>% #specify levels so the facets go in the correct order to avoid V10 being before V2
ggplot() +
geom_point(aes(value, V1))+ #plot points
geom_smooth(aes(value, V1), method = "lm", se = F)+ #plot lm fit without se
facet_wrap(~key) #facet by key