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

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

How can I create a grouped bar plot visualisation in R using the given dataset

问题

如何在R中为以下数据集创建一个分组条形图

我正在使用这个数据集:

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

我想要找到每年的最高、最低和平均出发延误时间。以下是用于此目的的脚本和数据输出(附在下面):

  1. Summary_statistics <- full_trains %>%
  2. group_by(year) %>%
  3. summarise(min_ave_time = min(journey_time_avg),
  4. max_ave_time = max(journey_time_avg),
  5. mean_ave_time = mean(journey_time_avg)) %>%
  6. ungroup()

有人可以帮我创建一个类似于Excel中的数据输出的可视化吗?您可以参考以下图像:

英文:

How to create a grouped bar plot for the following dataset in R

I'm using this dataset:

  1. 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)

  1. Summary_statistics &lt;- full_trains %&gt;%
  2. group_by(year) %&gt;%
  3. summarise(min_ave_time = min(journey_time_avg),
  4. max_ave_time = max(journey_time_avg),
  5. mean_ave_time = mean(journey_time_avg)) %&gt;%
  6. 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.

  1. library(Rnvd3)
  2. library(dplyr)
  3. library(tidyr)
  4. full_trains <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv")
  5. Summary_statistics <- full_trains %>%
  6. group_by(year) %>%
  7. summarise(min_ave_time = min(journey_time_avg),
  8. max_ave_time = max(journey_time_avg),
  9. mean_ave_time = mean(journey_time_avg)) %>%
  10. ungroup()
  11. dat <- Summary_statistics %>%
  12. pivot_longer(-year, names_to = "variable", values_to = "average_time")
  13. multiBarChart(
  14. data = dat,
  15. average_time ~ year,
  16. by = "variable",
  17. height = "500px"
  18. )

使用提供的数据集,在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.

  1. library(Rnvd3)
  2. library(dplyr)
  3. library(tidyr)
  4. full_trains &lt;- readr::read_csv(&quot;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv&quot;)
  5. Summary_statistics &lt;- full_trains %&gt;%
  6. group_by(year) %&gt;%
  7. summarise(min_ave_time = min(journey_time_avg),
  8. max_ave_time = max(journey_time_avg),
  9. mean_ave_time = mean(journey_time_avg)) %&gt;%
  10. ungroup()
  11. dat &lt;- Summary_statistics %&gt;%
  12. pivot_longer(-year, names_to = &quot;variable&quot;, values_to = &quot;average_time&quot;)
  13. multiBarChart(
  14. data = dat,
  15. average_time ~ year,
  16. by = &quot;variable&quot;,
  17. height = &quot;500px&quot;
  18. )

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

答案2

得分: 0

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

英文:

Does this answer your question?

  1. library(tidyverse)
  2. library(ggplot2)
  3. full_trains &lt;- readr::read_csv(&quot;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/full_trains.csv&quot;)
  4. full_trains %&gt;%
  5. ggplot(aes(x = journey_time_avg)) +
  6. geom_histogram() +
  7. 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:

  1. library(dplyr)
  2. Summary_statistics %&gt;%
  3. pivot_longer(-year, names_to = &quot;variable&quot;, values_to = &quot;average_time&quot;) %&gt;%
  4. ggplot(aes(x = year, y = average_time, fill = variable)) +
  5. geom_bar(stat = &quot;identity&quot;, position = position_dodge())

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

答案4

得分: 0

以下是翻译好的部分:

使用ggplot2,您可以模仿您想要的Excel版本;调整参数值以满足您想要的设计。

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. Summary_statistics <-
  5. full_trains %>
  6. group_by(year) %>
  7. summarise(min_ave_time = min(journey_time_avg),
  8. max_ave_time = max(journey_time_avg),
  9. mean_ave_time = mean(journey_time_avg)) %>
  10. ungroup() %>
  11. pivot_longer(-year, names_to = "var", values_to = "avg_time")
  12. ggplot(Summary_statistics, aes(year, avg_time, fill = var)) +
  13. geom_col(position = position_dodge2(width = 0.8)) +
  14. geom_text(aes(label = round(avg_time, 1)),
  15. vjust = -0.3,
  16. position = position_dodge2(width = 0.9)) +
  17. labs(x = NULL,
  18. y = NULL,
  19. fill = NULL) +
  20. theme_minimal() +
  21. theme(legend.position = "bottom",
  22. panel.grid.minor.y = element_blank(),
  23. panel.grid.minor.x = element_blank(),
  24. 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.

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. Summary_statistics &lt;-
  5. full_trains |&gt;
  6. group_by(year) |&gt;
  7. summarise(min_ave_time = min(journey_time_avg),
  8. max_ave_time = max(journey_time_avg),
  9. mean_ave_time = mean(journey_time_avg)) |&gt;
  10. ungroup() |&gt;
  11. pivot_longer(-year, names_to = &quot;var&quot;, values_to = &quot;avg_time&quot;)
  12. ggplot(Summary_statistics, aes(year, avg_time, fill = var)) +
  13. geom_col(position = position_dodge2(width = 0.8)) +
  14. geom_text(aes(label = round(avg_time, 1)),
  15. vjust = -0.3,
  16. position = position_dodge2(width = 0.9)) +
  17. labs(x = NULL,
  18. y = NULL,
  19. fill = NULL) +
  20. theme_minimal() +
  21. theme(legend.position = &quot;bottom&quot;,
  22. panel.grid.minor.y = element_blank(),
  23. panel.grid.minor.x = element_blank(),
  24. 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:

确定