绘制柱状图时如何避免分类变量的重复?

问题描述 投票:0回答:1

https:/www.kaggle.comshivambnetflix-shows-and-movies-exploratory-analysisnotebook 包含数据集。文件大小 - 2.13 MB)。

我试图绘制Netflix提供的前20个类别的内容。

我尝试的代码如下。

library(tidyverse)
library(lubridate)    

net_flix <- read.csv("netflix_titles_nov_2019.csv")

net_flix %>% separate_rows(listed_in, sep = ",")%>%
    count(listed_in)%>%
    slice_max(n, n = 20)%>%
    ggplot(aes(y = fct_reorder(listed_in, n), x = n))+
    geom_col()

结果输出如下,[Netflix上的20大类节目]。

从图中可以看出,有很多类别如电视剧、喜剧、国际电视节目都出现在多个位置。

预期的输出结果如下。

enter image description here

r ggplot2 data-visualization repeat
1个回答
1
投票

欢迎来到SO,我想你的字符串用空格弄乱了,也尽量按照ggplot的惯例对图形进行语法上的处理

library(tidyverse)
library(lubridate)

net_flix <- read_csv("netflix_titles.csv")

net_flix %>%
  separate_rows(listed_in, sep = ",") %>%
  mutate(listed_in = listed_in %>% str_squish()) %>%
  count(listed_in) %>%
  top_n(20, wt = n) %>%
  ggplot(aes(x = fct_reorder(listed_in, n), y = n)) +
  geom_col() +
  coord_flip()
© www.soinside.com 2019 - 2024. All rights reserved.