我正在为学校做一个项目,我在从数据集中提取信息并绘制它时遇到了麻烦。我正在使用数据集“movies”和包“ggplot2movies”。我的任务是筛选1990年至2005年的六种电影类型(动作,喜剧,浪漫,戏剧,纪录片和动画),并绘制每年生产的电影数量。每种类型都应该有“一条曲线”,所有的曲线都必须标绘在同一个图中。它必须包含适当的图例并具有不同的颜色。
我已经有了一些代码,但它没有按预期运行。我显然是新来的,可以使用帮助。代码可以在这里找到:
library(ggplot2movies)
data(movies)
filtered_movies <- movies %>% filter(year >= 1990 & year <= 2005, Action ==1, Comedy == 1, Animation == 1, Drama == 1, Documentary ==1, Romance == 1)
Genresix = tibble(Genre = c("Action", "Animation", "Comedy", "Drama",
"Documentary", "Romance"),
Movies1990 = c(filtered_movies$Action,
filtered_movies$Animation,
filtered_movies$Comedy,
filtered_movies$Drama,
filtered_movies$Documentary,
filtered_movies$Romance
))
ggplot(data = movies, mapping = aes(x = Genre, y = years)) +
geom_line(size = 5)+ geom_line(colour = "red")
plot <- ggplot(filtered_movies, aes(x = year, color = genre)) +
geom_line(stat = "count", aes(group = genre)) +
labs(title = "Number of Movies by Genre (1990-2005)",
x = "Year",
y = "Number of Movies") +
theme_minimal() +
scale_color_discrete(name = "Genre")
# Display the plot
print(plot)
我知道该怎么做,但具体如何将我的想法翻译成R,我不确定。你可以自己仔细阅读“电影”数据集。体裁“短”不应该包括在作业中。
我试图使用R从数据集“电影”中绘制六种电影类型(动作,喜剧,浪漫,纪录片,戏剧和动画)。图表必须包含适当的图例和不同的颜色。每种类型都应该绘制一条曲线,所有曲线必须在同一图形中。我试过使用ggplot,但是缺少了一些东西。我期待着每一个流派的情节按年(1990年至2005年)。它指出:
Error in `tibble()`:
! Tibble columns must have compatible sizes.
• Size 6: Existing data.
• Size 0: Column `Movies1990`.
ℹ Only values of size one are recycled.
Backtrace:
1. tibble::tibble(...)
我是一个新手,需要一些帮助。代码可以在下面找到:
library(ggplot2movies)
data(movies)
filtered_movies <- movies %>% filter(year >= 1990 & year <= 2005, Action ==1, Comedy == 1, Animation == 1, Drama == 1, Documentary ==1, Romance == 1)
Genresix = tibble(Genre = c("Action", "Animation", "Comedy", "Drama",
"Documentary", "Romance"),
Movies1990 = c(filtered_movies$Action,
filtered_movies$Animation,
filtered_movies$Comedy,
filtered_movies$Drama,
filtered_movies$Documentary,
filtered_movies$Romance
))
ggplot(data = movies, mapping = aes(x = Genre, y = years)) +
geom_line(size = 5)+ geom_line(colour = "red")
plot <- ggplot(filtered_movies, aes(x = year, color = genre)) +
geom_line(stat = "count", aes(group = genre)) +
labs(title = "Number of Movies by Genre (1990-2005)",
x = "Year",
y = "Number of Movies") +
theme_minimal() +
scale_color_discrete(name = "Genre")
# Display the plot
print(plot)
1条答案
按热度按时间axr492tv1#
您的代码存在多个问题。首先,您的
filter
语句将只保留六个类型列中每个列都有“1”的电影,显然没有符合此条件的电影。第二,有更简单的方法来堆叠多个数据框列,即你可以使用tidy::pivot_longer
来重塑你的数据,然后filter
用于属于六种类型之一的电影。这样做之后,绘图代码工作正常: