【问题标题】:How to plot all variables in dataset with ggplot and facetwrap如何使用 ggplot 和 facetwrap 绘制数据集中的所有变量
【发布时间】:2019-01-18 10:39:10
【问题描述】:

我正在尝试使用 ggplot 和 facet_wrap 一次性绘制所有变量。但是,我无法使代码正常工作。

我的数据集是各种分类变量和数值变量,我希望所有变量的 x 变量都相同。

我做了什么:

  data %>%
    keep(is.numeric) %>%
    gather() %>%
    ggplot(aes(value))+
    geom_point(~ key)
      facet_wrap(~ key)

我也试过

问题是什么都没有出现,或者缺少一个 y 变量...

我希望有人能帮助我从这个挑战中前进。

【问题讨论】:

  • 您可以使用dput添加示例数据吗?

标签: r ggplot2 facet-wrap


【解决方案1】:

这个?根据 OP 的要求,这里是使用gapminder 的代表。它可以根据需要进行调整。

gapminder::gapminder %>%
  gather("id","value",4:ncol(.)) %>%  
  ggplot(aes(continent,value,col=id))+geom_col()+facet_wrap(.~id)+
  theme_minimal()+
  theme(axis.text.x = element_text(angle=90))

原答案:

library(tidyverse)
    iris %>% 
    keep(is.numeric) %>% 
      gather() %>% 
      ggplot(aes(value,key))+geom_point()+facet_wrap(key~.)

示例 2:

iris %>% 
keep(is.numeric) %>% 
  gather() %>% 
  ggplot(aes(key,value))+geom_col()+facet_wrap(.~key)

【讨论】:

  • 我总共有 6 个变量:国家、大陆、年份、lifeExp、pop 和 gdpPercap(gapminder 数据集)。如果我现在想将“大陆”作为所有图表的 x 变量,那么我该怎么做?
猜你喜欢
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 2017-03-14
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
相关资源
最近更新 更多