使用 ggplot2 在 R 中为离散变量创建小提琴图、宇宙飞船图或类似图
Create violin plots, spaceship charts or similar for discrete variables in R using ggplot2
我正尝试在下面的 link 中创建一系列类似这样的图表。这些图表显示了每个英国公务员等级的人口,以及每个政府部门的图表。这样可以轻松比较图表以显示它们的结构。例如,我可以很快看出 DfID 非常偏重于高级,而 MOJ 则偏重于底层。
https://www.instituteforgovernment.org.uk/charts/grade-composition-and-change-department
我想在 R 中执行此操作,并且一直在使用 ggplot 试用一些解决方案。到目前为止,我已经尝试了以下方法:
- 小提琴图(不适用于垂直轴上的离散变量)
- 折线图背对背贴在一起,一正一负,重新创建形状(努力填充中间的 space)
- 人口金字塔(我想要一条平滑的线而不是条形图)
我在下面提供了一个示例,该示例将创建一对线条,显示特定球队按位置排列的平均 Fantasy Football 积分。然后,我想在所有英超联赛球队中执行此操作,其方式与上面 link 中公务员部门所做的类似。
library(tidyverse)
library(dplyr)
position <- c('Goalkeeper','Defender','Midfielder','Forward')
average_points <- c(100, 150, 185, 170)
football_df <- data.frame(position, average_points) %>%
dplyr::mutate(negative_average_points = average_points * -1) %>% # create a column that shows the negative to create the mirrored line
gather(key = key, value = average_points, -position, na.rm = TRUE) # turn into long format to create the line chart
ggplot(football_df,
aes(x = position, y = average_points, group = key)) +
geom_line() +
coord_flip()
这是我现在要去的路线。我很想做一些更像面积图的东西,但堆叠不允许负值。
采用这种方法仍然存在一些问题:
- 填充线下的区域,使其看起来更像面积图
- 这些仓位现在已经乱序了 - 我希望它保持 'goalkeeper, defender, midfielder, forward' 的顺序。我曾尝试使用因子来执行此操作,但数据的长格式不允许使用因子,因为每个位置出现两次。
我欢迎任何关于更好方法的想法,或者我如何开发折线图的想法,使其看起来更像上面 link 示例中的图表。谢谢!
要达到您想要的结果,您可以切换到 geom_area
,对于排序,您可以将 limits
设置为所需的顺序:
library(tidyverse)
position <- c('Goalkeeper','Defender','Midfielder','Forward')
average_points <- c(100, 150, 185, 170)
football_df <- data.frame(position, average_points) %>%
dplyr::mutate(negative_average_points = average_points * -1) %>% # create a column that shows the negative to create the mirrored line
gather(key = key, value = average_points, -position, na.rm = TRUE) # turn into long format to create the line chart
ggplot(football_df,
aes(x = position, y = average_points, group = key)) +
geom_area() +
scale_x_discrete(limits = position) +
coord_flip()
我正尝试在下面的 link 中创建一系列类似这样的图表。这些图表显示了每个英国公务员等级的人口,以及每个政府部门的图表。这样可以轻松比较图表以显示它们的结构。例如,我可以很快看出 DfID 非常偏重于高级,而 MOJ 则偏重于底层。
https://www.instituteforgovernment.org.uk/charts/grade-composition-and-change-department
我想在 R 中执行此操作,并且一直在使用 ggplot 试用一些解决方案。到目前为止,我已经尝试了以下方法:
- 小提琴图(不适用于垂直轴上的离散变量)
- 折线图背对背贴在一起,一正一负,重新创建形状(努力填充中间的 space)
- 人口金字塔(我想要一条平滑的线而不是条形图)
我在下面提供了一个示例,该示例将创建一对线条,显示特定球队按位置排列的平均 Fantasy Football 积分。然后,我想在所有英超联赛球队中执行此操作,其方式与上面 link 中公务员部门所做的类似。
library(tidyverse)
library(dplyr)
position <- c('Goalkeeper','Defender','Midfielder','Forward')
average_points <- c(100, 150, 185, 170)
football_df <- data.frame(position, average_points) %>%
dplyr::mutate(negative_average_points = average_points * -1) %>% # create a column that shows the negative to create the mirrored line
gather(key = key, value = average_points, -position, na.rm = TRUE) # turn into long format to create the line chart
ggplot(football_df,
aes(x = position, y = average_points, group = key)) +
geom_line() +
coord_flip()
这是我现在要去的路线。我很想做一些更像面积图的东西,但堆叠不允许负值。
采用这种方法仍然存在一些问题:
- 填充线下的区域,使其看起来更像面积图
- 这些仓位现在已经乱序了 - 我希望它保持 'goalkeeper, defender, midfielder, forward' 的顺序。我曾尝试使用因子来执行此操作,但数据的长格式不允许使用因子,因为每个位置出现两次。
我欢迎任何关于更好方法的想法,或者我如何开发折线图的想法,使其看起来更像上面 link 示例中的图表。谢谢!
要达到您想要的结果,您可以切换到 geom_area
,对于排序,您可以将 limits
设置为所需的顺序:
library(tidyverse)
position <- c('Goalkeeper','Defender','Midfielder','Forward')
average_points <- c(100, 150, 185, 170)
football_df <- data.frame(position, average_points) %>%
dplyr::mutate(negative_average_points = average_points * -1) %>% # create a column that shows the negative to create the mirrored line
gather(key = key, value = average_points, -position, na.rm = TRUE) # turn into long format to create the line chart
ggplot(football_df,
aes(x = position, y = average_points, group = key)) +
geom_area() +
scale_x_discrete(limits = position) +
coord_flip()