如何使用给定的数据集在R中创建分组条形图可视化

jxct1oxe  于 2023-05-26  发布在  其他
关注(0)|答案(4)|浏览(164)

如何在R中为以下数据集创建分组条形图
我使用这个数据集:

full_trains <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv")

我想找出每年的最高、最低和平均起飞延误时间。下面是用于此操作的脚本和数据输出(attached)

Summary_statistics <- full_trains %>%
  group_by(year) %>%
  summarise(min_ave_time = min(journey_time_avg),
            max_ave_time = max(journey_time_avg),
            mean_ave_time = mean(journey_time_avg)) %>%
            ungroup()

有人能帮我创建一个类似于

的数据输出的可视化吗?

xfyts7mz

xfyts7mz1#

我是Rnvd 3软件包的忠实粉丝,因为它提供了交互性。特别是分组/堆叠效果真的很有趣。

library(Rnvd3)
library(dplyr)
library(tidyr)

full_trains <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv")

Summary_statistics <- full_trains %>%
  group_by(year) %>%
  summarise(min_ave_time = min(journey_time_avg),
            max_ave_time = max(journey_time_avg),
            mean_ave_time = mean(journey_time_avg)) %>%
  ungroup()

dat <- Summary_statistics %>% 
  pivot_longer(-year, names_to = "variable", values_to = "average_time") 

multiBarChart(
  data = dat, 
  average_time ~ year,
  by = "variable",
  height = "500px"
)

esyap4oy

esyap4oy2#

使用ggplot 2你可以模仿你想要的excel版本;调整参数值以适合所需的设计。

library(dplyr)
library(tidyr)
library(ggplot2)

Summary_statistics <- 
  full_trains  |> 
  group_by(year)  |> 
  summarise(min_ave_time = min(journey_time_avg),
            max_ave_time = max(journey_time_avg),
            mean_ave_time = mean(journey_time_avg)) |> 
  ungroup() |> 
  pivot_longer(-year, names_to = "var", values_to = "avg_time") 

ggplot(Summary_statistics, aes(year, avg_time, fill = var)) + 
  geom_col(position = position_dodge2(width = 0.8)) +
  geom_text(aes(label = round(avg_time, 1)), 
            vjust = -0.3, 
            position = position_dodge2(width = 0.9)) +
  labs(x = NULL,
       y = NULL,
       fill = NULL) +
  theme_minimal() +
  theme(legend.position = "bottom",
        panel.grid.minor.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank())

创建于2023-05-22使用reprex v2.0.2

xggvc2p6

xggvc2p63#

这回答了你的问题吗?

library(tidyverse)
library(ggplot2)

full_trains <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv")

full_trains %>% 
  ggplot(aes(x = journey_time_avg)) + 
    geom_histogram() + 
    facet_wrap(~year)

ymzxtsji

ymzxtsji4#

您所指的是“分组条形图”,而不是“直方图”。要在R中完成此操作,首先需要将表转换为长格式(在tidyverse中使用pivot_longer()或其他选项,如reshape2::melt()。然后将其馈送到带有geom_bar()的ggplot项中,您必须设置stat = "identity"position = position_dodge()以获得您想要的分组条形图的确切类型:

library(dplyr)

Summary_statistics %>% 
  pivot_longer(-year, names_to = "variable", values_to = "average_time") %>% 
  ggplot(aes(x = year, y = average_time, fill = variable)) + 
  geom_bar(stat = "identity", position = position_dodge())

输出:

相关问题