使用提供的数据集,在R中如何创建一个分组条形图可视化。

huangapple go评论74阅读模式
英文:

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 &lt;- readr::read_csv(&quot;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv&quot;)

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 &lt;- full_trains %&gt;%
  group_by(year) %&gt;%
  summarise(min_ave_time = min(journey_time_avg),
            max_ave_time = max(journey_time_avg),
            mean_ave_time = mean(journey_time_avg)) %&gt;%
            ungroup()

Can someone please help me create a visualisation for this data output similar to 使用提供的数据集,在R中如何创建一个分组条形图可视化。?

答案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"
)

使用提供的数据集,在R中如何创建一个分组条形图可视化。

英文:

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 &lt;- readr::read_csv(&quot;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv&quot;)

Summary_statistics &lt;- full_trains %&gt;%
  group_by(year) %&gt;%
  summarise(min_ave_time = min(journey_time_avg),
            max_ave_time = max(journey_time_avg),
            mean_ave_time = mean(journey_time_avg)) %&gt;%
  ungroup()

dat &lt;- Summary_statistics %&gt;% 
  pivot_longer(-year, names_to = &quot;variable&quot;, values_to = &quot;average_time&quot;) 


multiBarChart(
  data = dat, 
  average_time ~ year,
  by = &quot;variable&quot;,
  height = &quot;500px&quot;
)

使用提供的数据集,在R中如何创建一个分组条形图可视化。

答案2

得分: 0

这个问题是否解答了您的疑问?

英文:

Does this answer your question?

library(tidyverse)
library(ggplot2)

full_trains &lt;- readr::read_csv(&quot;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv&quot;)

full_trains %&gt;% 
  ggplot(aes(x = journey_time_avg)) + 
    geom_histogram() + 
    facet_wrap(~year)

使用提供的数据集,在R中如何创建一个分组条形图可视化。

答案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 = &quot;identity&quot; and position = position_dodge() to get the exact type of grouped bar plot that you want:

library(dplyr)

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

output:
使用提供的数据集,在R中如何创建一个分组条形图可视化。

答案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())

使用提供的数据集,在R中如何创建一个分组条形图可视化。

创建于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 &lt;- 
  full_trains  |&gt; 
  group_by(year)  |&gt; 
  summarise(min_ave_time = min(journey_time_avg),
            max_ave_time = max(journey_time_avg),
            mean_ave_time = mean(journey_time_avg)) |&gt; 
  ungroup() |&gt; 
  pivot_longer(-year, names_to = &quot;var&quot;, values_to = &quot;avg_time&quot;) 


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 = &quot;bottom&quot;,
        panel.grid.minor.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank())

使用提供的数据集,在R中如何创建一个分组条形图可视化。<!-- -->

<sup>Created on 2023-05-22 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月22日 14:21:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定