【问题标题】:How do I plot different variables from the same column while leaving out the NAs?如何在省略 NA 的同时从同一列绘制不同的变量?
【发布时间】:2021-12-07 20:13:08
【问题描述】:

如何在省略 NA 的同时绘制同一列中的不同变量?

我正在研究 EA Sports 国际足联球员统计数据集

我写的代码是:

plotageleague <- df %>% 
  group_by(League) %>% 
  summarise(age = mean(Age))

options(repr.plot.width = 12, repr.plot.height = 8)

ggplot()+
  geom_histogram(df, mapping = aes(Age, fill = League))+
  geom_vline(plotageleague, mapping = aes(xintercept = age), color = "red", size = 1.5)+
  geom_text(plotageleague, mapping = aes(x = age+3, y = 65, label = round(age,digits = 2)))+
  facet_wrap(League~.)+
  theme_minimal()+
  theme(legend.position = "bottom")+
  labs(y = "Frequency", title = "", caption = "")

联赛列包含 12 个不同的足球联赛,每个联赛作为不同的变量和 NA,成为第 13 个图。我不想要那个。我只想要分配了球员的联赛的统计数据(我只将球员分配到实际存在的 30 多个联赛中的 12 个联赛,所以其余球员在“联赛”列中有 NA)

【问题讨论】:

  • filter(!is.na(League)) 会有所帮助。

标签: r ggplot2 plot group-by


【解决方案1】:

此答案使用 tidy 从 df 的 League 列中删除 na。您似乎也在创建另一个数据框 plotageleague。我不确定您是否希望将 na 包括在内。按照您的设置方式,它们就是这样。

library(tidyverse)
df %>% 
   drop_na(League) %>% 
   ggplot(.)+
   geom_histogram(mapping = aes(Age, fill = League))

【讨论】:

  • 感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将有助于未来的读者更好地了解正在发生的事情,尤其是那些刚接触该语言并难以理解概念的社区成员。
猜你喜欢
  • 2021-03-09
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多