我正在尝试使用包含县级疫苗接种数据的数据集制作闪避条形图(ggplot)。我希望我的 x 轴是 2 个县,并且我想根据特定的列名称(基于列中的种族)进行填充。我不确定应该将什么作为 y 轴。我想我需要旋转数据集并添加带有竞赛的列,然后使用它来填充数据。但不知道该怎么做。
我尝试旋转数据,但无法将第一行设为列名称。
您正在寻找的操作称为“重塑”或“旋转”。
tidyr
软件包提供 pivot_longer()
和 pivot_wider()
那个目的。以下是如何pivot_longer()
您的示例
数据并创建条形图。
# Creating parts of your dataset with random numbers.
# (As some of the commenters pointed out, it would be better
# if you could include your data in your question to make your
# question in a reprodcuible fashion.)
library(tidyverse)
ms_1 <- tibble(
COUNTY_NAME = c("Alleghany", "Montgomery", "Total"),
COUNT_TOTAL = sample(50:500, 3),
COUNT_ETH_NHL = sample(50:500, 3),
COUNT_ETH_HL = sample(50:500, 3),
COUNT_ETH_UNKNOWN = sample(50:500, 3),
COUNT_RACE_AIAN = sample(50:500, 3),
COUNT_RACE_ASIAN = sample(50:500, 3),
COUNT_RACE_BLACK = sample(50:500, 3),
COUNT_RACE_WHITE = sample(50:500, 3),
)
# Pivot, filter, select data and plot bars.
ms_1 |>
# Remove the following line if you want to include Total in the plot
filter(COUNTY_NAME != "Total") |>
select(COUNTY_NAME, contains("_RACE_")) |>
pivot_longer(-COUNTY_NAME, names_to = "race", values_to = "count") |>
mutate(race = str_remove(race, "COUNT_RACE_")) |>
ggplot(aes(COUNTY_NAME, count, group = race, fill = race)) +
geom_bar(stat = "identity", position = "dodge")