数据中每一列的ggplot

ggplot for each column in a data

我缺少 R 中的一些基础知识。

如何为数据框中的每一列绘制图表?

我试过分别为每一列绘制图表。我想知道有没有更简单的方法?

library(dplyr)
library(ggplot2)
data(economics)

#scatter plots
ggplot(economics,aes(x=pop,y=pce))+
  geom_point()
ggplot(economics,aes(x=pop,y=psavert))+
  geom_point()
ggplot(economics,aes(x=pop,y=uempmed))+
  geom_point()
ggplot(economics,aes(x=pop,y=unemploy))+
  geom_point()

#boxplots
ggplot(economics,aes(y=pce))+
  geom_boxplot()
ggplot(economics,aes(y=pop))+
  geom_boxplot()
ggplot(economics,aes(y=psavert))+
  geom_boxplot()
ggplot(economics,aes(y=uempmed))+
  geom_boxplot()
ggplot(economics,aes(y=unemploy))+
  geom_boxplot()

我要找的是 1 个箱形图 2*2 和 1 个 2*2 散点图和 ggplot2。我知道有一个我不明白如何实现的小平面网格。(我相信这可以通过 par(mfrow()) 和基本 R 图轻松实现。我在其他地方看到使用 using widening the data?我没有'不懂。

在这种情况下,解决方案几乎总是reshape the data from wide to long format

economics %>%
  select(-date) %>%
  tidyr::gather(variable, value, -pop) %>%
  ggplot(aes(x = pop, y = value)) +
  geom_point(size = 0.5) +
  facet_wrap(~ variable, scales = "free_y")

economics %>%
  tidyr::gather(variable, value, -date) %>%
  ggplot(aes(y = value)) +
  geom_boxplot() +
  facet_wrap(~ variable, scales = "free_y")