在R中,如何使用ggplot2用2个分类值绘制三个不同的值?
我有一个原始数据集在R中,如何使用ggplot2用2个分类值绘制三个不同的值?,r,ggplot2,R,Ggplot2,我有一个原始数据集 gender neg_sentiment neu_sentiment pos_sentiment <fct> <int> <int> <int> 1 F 24216 10070 14734 2 M 372863 162281 239366 您可以使用facet
gender neg_sentiment neu_sentiment pos_sentiment
<fct> <int> <int> <int>
1 F 24216 10070 14734
2 M 372863 162281 239366
您可以使用
facet\u wrap
library(tidyverse)
df %>%
pivot_longer(-gender) %>%
ggplot(aes(name, value, fill = name)) +
geom_col() +
facet_wrap(~ gender)
或者经过一些更精细的调整/抛光
df %>%
pivot_longer(-gender) %>%
mutate(name = factor(
name,
levels = c("pos_sentiment", "neu_sentiment", "neg_sentiment"),
labels = c("positive", "neutral", "negative"))) %>%
ggplot(aes(name, value, fill = name)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ gender, strip.position = "bottom") +
labs(x = "") +
scale_fill_manual(
values = c("positive" = "darkblue", "neutral" = "blue", "negative" = "darkred")) +
theme_minimal()
样本数据
df您可以使用facet\u wrap从宽到长重塑形状,然后(gg)绘制
library(tidyverse)
df %>%
pivot_longer(-gender) %>%
ggplot(aes(name, value, fill = name)) +
geom_col() +
facet_wrap(~ gender)
或者经过一些更精细的调整/抛光
df %>%
pivot_longer(-gender) %>%
mutate(name = factor(
name,
levels = c("pos_sentiment", "neu_sentiment", "neg_sentiment"),
labels = c("positive", "neutral", "negative"))) %>%
ggplot(aes(name, value, fill = name)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ gender, strip.position = "bottom") +
labs(x = "") +
scale_fill_manual(
values = c("positive" = "darkblue", "neutral" = "blue", "negative" = "darkred")) +
theme_minimal()
样本数据
df