提问人:Sri Sreshtan 提问时间:7/25/2020 更新时间:7/25/2020 访问量:561
如何使用 facet_wrap 拆分列并绘制图形?
How to split columns and plot a graph using facet_wrap?
问:
https://www.kaggle.com/shivamb/netflix-shows-and-movies-exploratory-analysis 包含数据集。(2.13兆字节)
我正在尝试从 netflix 数据集中拆分国家/地区列,并绘制一个表示来自三个国家/地区的电影的分面条形图。
可重现的代码如下:-
library(tidyverse)
library(scales)
library(lubridate)
netflix_tbl <- read.csv("netflix_titles_nov_2019.csv")
netflix_wrangled_tbl <- netflix_tbl%>%
mutate(date_added = dmy(date_added),
date = day(date_added), month = month(date_added), year = year(date_added),
count = readr::parse_number(as.character(duration)),
show_type = stringr::str_remove(duration, as.character(count)))
netflix_wrangled_tbl %>%
filter(type == "Movie") %>%
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 = 24)%>%
ggplot(aes(y = tidytext::reorder_within(cast, n, country), x = n))+
geom_col() +
tidytext::scale_y_reordered() +
facet_wrap(~country, scales = "free")
结果输出为:
预期输出为:-
我可以知道我哪里出了问题以及如何实现预期的输出吗?谢谢。
答:
2赞
Duck
7/25/2020
#1
尝试用以下命令修改代码的最后一个片段:
netflix_wrangled_tbl %>%
filter(type == "Movie") %>%
separate_rows(country, sep = ",")%>%
filter(country == "India" | country == "United States"| country == "United Kingdom")%>%
separate_rows(cast, sep = ",")%>%
filter(cast!="") %>%
# Count by country and cast
count(country, cast)%>%
group_by(country) %>% arrange(desc(n)) %>%
group_by(country) %>%
slice(seq_len(24)) %>%
ggplot(aes(y = tidytext::reorder_within(cast, n, country), x = n))+
geom_col() +
tidytext::scale_y_reordered() +
facet_wrap(~country, scales = "free")
评论
0赞
Sri Sreshtan
7/26/2020
代码运行良好。只是想澄清您的输出是否正确或预期输出中提到的输出是否正确?
1赞
Duck
7/26/2020
@SriSreshtan 有很多空白,这就是你的代码不起作用的原因,关于紫色图,这取决于你想显示多少行。此外,该情节来自python而不是.cast
R
0赞
Sri Sreshtan
7/26/2020
同意。那为什么输出会有差异呢?
1赞
Duck
7/26/2020
它可能是您选择的顶行数。此外,数据太旧,因此请确保您拥有与这些 python 笔记本中相同的数据。
评论