如何使用r中的面包过滤数据并绘制列图?

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

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

我正在寻找实现以下目标的数据集--确定来自美国、英国和印度的前25名主要演员。

我编写的代码如下:

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

net_flix %>% 
    separate_rows(country, sep = ",")%>% 
    filter(country == "India"| country == "United States"| country == "United Kingdom")%>%
    separate_rows(cast, sep = ",")%>%
    count(cast)%>%
    slice_max(n, n = 25)%>%
    ggplot(aes(y = fct_reorder(cast, n), x = n))+
    geom_col()

结果输出如下:

enter image description here

预期的输出如下,(仅图上部分)

enter image description here

在审查了建议的问题后,进行了尝试 https://stackoverflow.com/questions/55864054/filtering-the-data-using-pickerinput-and-plotting-based-on-the-filtered-data-i

r ggplot2 facet-wrap column-chart
1个回答
1
投票

试试这个。问题与 facet_wrap 是,要按国家来琢磨,就必须按投和国家来算。另外。为了得到每个面的条形图,我使用了tidytext::reorder_within和tidytext::scale_x_reordered。

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

net_flix %>% 
  separate_rows(country, sep = ",")%>% 
  filter(country == "India"| country == "United States"| country == "United Kingdom")%>%
  separate_rows(cast, sep = ",")%>%
  # Count by country and cast
  count(country, cast)%>%
  slice_max(n, n = 25)%>%
  ggplot(aes(y = tidytext::reorder_within(cast, n, country), x = n))+
  geom_col() +
  tidytext::scale_y_reordered() +
  facet_wrap(~country, scales = "free")

这样我就得到了这个图

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.