在 gganimate 中旋转多个 Y 变量
Rotating through multiple Y Variables in gganimate
我目前正在尝试使用 gganimate 为绘图制作动画,但我正在努力弄清楚如何轮换多个 y 变量。以下数据是从 Twitter 抓取中收集的,这使我能够根据最近民主党辩论后的推文计算 "sentiment score"。这里的目标是创建一个动画图,以简化所有 10 个情绪分数并为每个候选人调整 ggplot。这可以用 gganimate 实现吗?
structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
这是我目前编写的脚本:
library ("ggplot2")
library("dplyr")
library("tidyverse")
library("plotly")
library("viridis")
library("gganimate")
#Read in CSV Files
sentiment_score <- read_csv('C:\Users\tdago\Documents\R\Sentiment_Scores.csv')
sentiment_score_hashtag <- read_csv('C:\Users\tdago\Documents\R\Sentiment_Scores_hashtag.csv')
#Tidy Data
sentiment_score <- sentiment_score %>%
rename(candidate = X1)
sentiment_score_hashtag <-sentiment_score_hashtag %>%
rename(candidate = X1)
#Create Charts for Comparison
ggplot(data=sentiment_score,aes(x = candidate, y=anger))+
geom_bar(aes(fill=candidate),stat = "identity")+
theme(legend.position="none")+
xlab("Presidential Candidates")+ylab("Scores")+ggtitle("Anger") +
labs(x = "", y = "{sentiment"}) +
ease_aes('linear')
注意:sentiment_score 对象是此特定图表中唯一使用的对象。 sentiment_score_hashtag 是一个类似的数据框,其中包含基于不同搜索的情绪分数。
我认为您不能使用 gganimate 轮换 Y 变量。更容易将数据从 wide 格式转换为 long 格式(请参阅 this question 以获取实现此目的的完整方法列表)。我将采用整洁的方式,使用 tidyr::pivot_longer
:
> sentiment_score %>%
+ pivot_longer(-candidate, names_to = 'sentiment')
# A tibble: 70 x 3
candidate sentiment value
<chr> <chr> <dbl>
1 warren anger 162
2 warren anticipation 570
3 warren disgust 94
4 warren fear 245
5 warren joy 574
6 warren sadness 237
7 warren surprise 104
8 warren trust 741
9 warren negative 540
10 warren positive 989
# … with 60 more rows
>
通过这种方式,您可以轻松地将情绪用作 gganimate
中的状态变量,并遵循很好的 gganimate getting started manual.
以下是可能性的示例:
library ("ggplot2")
library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library("tidyverse")
# library("plotly")
# library("viridis")
library("gganimate")
#Tidy Data
sentiment_score <- structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
#Create Charts for Comparison
candidates_plot <- sentiment_score %>%
pivot_longer(-candidate, names_to = 'sentiment') %>%
ggplot(aes(x = candidate, y=value))+
geom_bar(aes(fill=candidate, group = sentiment),stat = "identity")+
scale_y_continuous(expand = c(0,0), limits = c(0,1250)) +
theme(legend.position="none")#+
# xlab("Presidential Candidates")+ylab("Scores")+ggtitle("{sentiment}") +
# labs(x = "Presidential Candidates", y = "{sentiment}")
anim <- candidates_plot +
transition_states(
sentiment, 2, 2
) +
enter_fade() + enter_drift(y_mod = -500) +
exit_shrink() + exit_drift(y_mod = -500) +
labs(
title = '{closest_state}',
x = "Presidential Candidates", y = "{closest_state}"
)
animate(
anim, width = 500, height = 300, res = 90
)
由 reprex package (v0.3.0)
于 2019-11-25 创建
我目前正在尝试使用 gganimate 为绘图制作动画,但我正在努力弄清楚如何轮换多个 y 变量。以下数据是从 Twitter 抓取中收集的,这使我能够根据最近民主党辩论后的推文计算 "sentiment score"。这里的目标是创建一个动画图,以简化所有 10 个情绪分数并为每个候选人调整 ggplot。这可以用 gganimate 实现吗?
structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
这是我目前编写的脚本:
library ("ggplot2")
library("dplyr")
library("tidyverse")
library("plotly")
library("viridis")
library("gganimate")
#Read in CSV Files
sentiment_score <- read_csv('C:\Users\tdago\Documents\R\Sentiment_Scores.csv')
sentiment_score_hashtag <- read_csv('C:\Users\tdago\Documents\R\Sentiment_Scores_hashtag.csv')
#Tidy Data
sentiment_score <- sentiment_score %>%
rename(candidate = X1)
sentiment_score_hashtag <-sentiment_score_hashtag %>%
rename(candidate = X1)
#Create Charts for Comparison
ggplot(data=sentiment_score,aes(x = candidate, y=anger))+
geom_bar(aes(fill=candidate),stat = "identity")+
theme(legend.position="none")+
xlab("Presidential Candidates")+ylab("Scores")+ggtitle("Anger") +
labs(x = "", y = "{sentiment"}) +
ease_aes('linear')
注意:sentiment_score 对象是此特定图表中唯一使用的对象。 sentiment_score_hashtag 是一个类似的数据框,其中包含基于不同搜索的情绪分数。
我认为您不能使用 gganimate 轮换 Y 变量。更容易将数据从 wide 格式转换为 long 格式(请参阅 this question 以获取实现此目的的完整方法列表)。我将采用整洁的方式,使用 tidyr::pivot_longer
:
> sentiment_score %>%
+ pivot_longer(-candidate, names_to = 'sentiment')
# A tibble: 70 x 3
candidate sentiment value
<chr> <chr> <dbl>
1 warren anger 162
2 warren anticipation 570
3 warren disgust 94
4 warren fear 245
5 warren joy 574
6 warren sadness 237
7 warren surprise 104
8 warren trust 741
9 warren negative 540
10 warren positive 989
# … with 60 more rows
>
通过这种方式,您可以轻松地将情绪用作 gganimate
中的状态变量,并遵循很好的 gganimate getting started manual.
以下是可能性的示例:
library ("ggplot2")
library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library("tidyverse")
# library("plotly")
# library("viridis")
library("gganimate")
#Tidy Data
sentiment_score <- structure(
list(
candidate = c("warren", "booker", "yang", "harris", "biden", "sanders", "buttigieg"),
anger = c(162, 216, 193, 74, 451, 290, 114),
anticipation = c(570, 492, 401, 205, 360, 419, 499),
disgust = c(94, 75, 52, 61, 202, 81, 69),
fear = c(245, 241, 119, 117, 271, 251, 102),
joy = c(574, 525, 279, 181, 214, 319, 183),
sadness = c(237, 161, 138, 106, 406, 157, 251),
surprise = c(104, 191, 176, 106, 255, 343, 123),
trust = c(741, 749, 460, 325, 593, 574, 410),
negative = c(540, 317, 253, 205, 715, 360, 469),
positive = c(989, 1202, 857, 510, 751, 790, 701)
),
class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L),
spec = structure(
list(
cols = list(
candidate = structure(list(), class = c("collector_character", "collector")),
anger = structure(list(), class = c("collector_double", "collector")),
anticipation = structure(list(), class = c("collector_double", "collector")),
disgust = structure(list(), class = c("collector_double", "collector")),
fear = structure(list(), class = c("collector_double", "collector")),
joy = structure(list(), class = c("collector_double", "collector")),
sadness = structure(list(), class = c("collector_double", "collector")),
surprise = structure(list(), class = c("collector_double", "collector")),
trust = structure(list(), class = c("collector_double", "collector")),
negative = structure(list(), class = c("collector_double", "collector")),
positive = structure(list(), class = c("collector_double", "collector"))),
default = structure(list(), class = c("collector_guess", "collector")), skip = 1
),
class = "col_spec")
)
#Create Charts for Comparison
candidates_plot <- sentiment_score %>%
pivot_longer(-candidate, names_to = 'sentiment') %>%
ggplot(aes(x = candidate, y=value))+
geom_bar(aes(fill=candidate, group = sentiment),stat = "identity")+
scale_y_continuous(expand = c(0,0), limits = c(0,1250)) +
theme(legend.position="none")#+
# xlab("Presidential Candidates")+ylab("Scores")+ggtitle("{sentiment}") +
# labs(x = "Presidential Candidates", y = "{sentiment}")
anim <- candidates_plot +
transition_states(
sentiment, 2, 2
) +
enter_fade() + enter_drift(y_mod = -500) +
exit_shrink() + exit_drift(y_mod = -500) +
labs(
title = '{closest_state}',
x = "Presidential Candidates", y = "{closest_state}"
)
animate(
anim, width = 500, height = 300, res = 90
)
由 reprex package (v0.3.0)
于 2019-11-25 创建