英文:
How can I create a grouped bar plot visualisation in R using the given dataset
问题
如何在R中为以下数据集创建一个分组条形图
我正在使用这个数据集:
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()
有人可以帮我创建一个类似于Excel中的数据输出的可视化吗?您可以参考以下图像:
英文:
How to create a grouped bar plot for the following dataset in R
I'm using this dataset:
full_trains <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv")
I wanted to find the Wthe highest, lowest, and average departure delay time for each year. Here's the script for that and data output (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()
Can someone please help me create a visualisation for this data output similar to ?
答案1
得分: 1
I'm a big fan of the Rnvd3 package for such charts because of the interactivity it offers. In particular, the grouped/stacked effect is really funny.
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"
)
英文:
I'm a big fan of the Rnvd3 package for such charts because of the interactivity it offers. In particular the grouped/stacked effect is really funny.
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"
)
答案2
得分: 0
这个问题是否解答了您的疑问?
英文:
Does this answer your question?
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)
答案3
得分: 0
你所提到的是一个“分组条形图”,而不是“直方图”。在R中完成这个操作,首先需要将表格转换为长格式(使用pivot_longer()
在tidyverse中或其他选项,如reshape2::melt()
)。然后,将其输入到一个ggplot对象中,使用geom_bar()
,您需要设置stat = "identity"
和position = position_dodge()
以获得您想要的精确类型的分组条形图:
英文:
What you are referring to is a "grouped bar plot" and not "histogram". To do it in R you first need to transform your table into long format (using pivot_longer()
in tidyverse or other options like reshape2::melt()
. Then feed this into a ggplot item with geom_bar()
, you have to set stat = "identity"
and position = position_dodge()
to get the exact type of grouped bar plot that you want:
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())
答案4
得分: 0
以下是翻译好的部分:
使用ggplot2,您可以模仿您想要的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
英文:
With ggplot2 you can mimic your desired excel version; adjust argument values to suit your desired design.
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())
<!-- -->
<sup>Created on 2023-05-22 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论