绘制多个组的嵌套分类值的折线图 (ggplot2)

Posted

技术标签:

【中文标题】绘制多个组的嵌套分类值的折线图 (ggplot2)【英文标题】:Line chart plotting nested categorical values for multiple groups (ggplot2) 【发布时间】:2021-11-21 18:38:41 【问题描述】:

我有关于不同酒店品牌在客户满意度调查中得分的数据。调查问题分为几类,并非所有类别都有相同数量的问题。请看以下数据:

hotels = data.frame(category = rep(c("room","room","service","service","overall rating"),each = 3),
                   subcategory = rep(c("comfort","cleanliness","professionalism","promptness","overall rating"),each = 3),
                   brand = rep(c("hotel 1","hotel 2","hotel 3"),times = 5),
                   score = c(6,10,4,7,9,2,6,9,5,9,7,3,6,8,3))

我需要将数据绘制为折线图,以可视化每个品牌在每个问题/子类别上的得分,并标记更广泛的类别。我的第一次尝试是这样的:

# factor variables so they appear in correct order when plotted
hotels$category = factor(hotels$category, levels = c("room","service","overall rating"))
hotels$subcategory = factor(hotels$subcategory, levels =c("comfort","cleanliness","professionalism","promptness","overall rating"))

# plot
library(dplyr)
library(ggplot2)

p = hotels %>%
  ggplot(aes(x=subcategory, y=score, group=brand, color=brand)) +
  geom_line() +
  geom_point()

p

Here is the plot. 如果我不需要子类别 类别标签,这会很好,但我需要。接下来,我尝试了分面:

p = hotels %>%
  ggplot(aes(x=subcategory, y=score, group=brand, color=brand)) +
  geom_line() +
  geom_point() +
  facet_grid(~category,
             scales = "free_x",
             space = "free_x",
             switch = "x") +
  theme(panel.spacing = unit(0, units = "cm"),
                             strip.placement = "outside")

p

This was the result. 如您所见,刻面之间的线被打断了。如何创建一个图表,其中所有点都像第一个示例中一样连接,但类别和子类别的标签与第二个示例中的一样?如果不是很明显,我是 ggplot2 的新手,所以我很感激你可能有的任何解决方案。

附:这些解决方案解决了类似的问题,但并不是我所需要的:

Drawing line between points across facets: X 轴是连续的,而我的是分类的;每个方面都出现相同的值,而我的每个方面都包含不同的子类别 Multirow axis labels with nested grouping variables:可视化是条形图,而我需要折线图 Plot charts with nested categories axes:可视化是点图,而我需要折线图(尝试此解决方案产生的结果与我的刻面尝试相同)

编辑:以下 teunbrand 的回答正是我所需要的。我稍微调整了代码以将类别按正确的顺序排列(考虑粘贴的变量不起作用)。最终代码如下所示:

# data
hotels = data.frame(category = rep(c("room","room","service","service","overall rating"),
                                   each = 3),
                    subcategory = rep(c("comfort","cleanliness","professionalism",
                                        "promptness","overall rating"),each = 3),
                    brand = rep(c("hotel 1","hotel 2","hotel 3"),times = 5),
                    score = c(6,10,4,7,9,2,6,9,5,9,7,3,6,8,3))

# add pasted variable directly to data set
hotels$paste = paste0(hotels$subcategory, "&", hotels$category)

# plot
library(dplyr)
library(ggplot2)
library(ggh4x)
library(forcats)
p = hotels %>%
# use mutate function from forcats to reorder categories
  mutate(paste = fct_relevel(paste, 
                            "comfort&room", "cleanliness&room", "professionalism&service", 
                            "promptness&service", "overall rating&overall rating")) %>%
# x is reordered pasted variable
  ggplot(aes(x=paste, 
             y=score, group=brand, color=brand)) +
  geom_line() +
  geom_point() +
  guides(x = ggh4x::guide_axis_nested(delim = "&"))

p

And the final plot looks like this.

感谢您的帮助!

【问题讨论】:

这些可能会让你继续前进:Multi-row x-axis labels in ggplot line chart; Axis labels on two lines with nested x variables (year below months) 这是一个使用interaction的快速'n脏版本:ggplot(aes(x=interaction(subcategory, category, sep = "\n"), y=score, group=brand, color=brand)) + ... 【参考方案1】:

这是ggh4x::guide_axis_nested() 的一个选项。您可以组合超类别和子类别的标签,指南将它们分成不同的行。免责声明:我是该函数的作者。

library(ggplot2)

hotels = data.frame(category = rep(c("room","room","service","service","overall rating"),each = 3),
                    subcategory = rep(c("comfort","cleanliness","professionalism","promptness","overall rating"),each = 3),
                    brand = rep(c("hotel 1","hotel 2","hotel 3"),times = 5),
                    score = c(6,10,4,7,9,2,6,9,5,9,7,3,6,8,3))

hotels$category = factor(hotels$category, levels = c("room","service","overall rating"))
hotels$subcategory = factor(hotels$subcategory, levels =c("comfort","cleanliness","professionalism","promptness","overall rating"))

# plot
library(dplyr)
library(ggplot2)

hotels %>%
  ggplot(aes(x=paste0(subcategory, "&", category), 
             y=score, group=brand, color=brand)) +
  geom_line() +
  geom_point() +
  guides(x = ggh4x::guide_axis_nested(delim = "&"))

由reprex package (v2.0.1) 于 2021 年 9 月 29 日创建

【讨论】:

以上是关于绘制多个组的嵌套分类值的折线图 (ggplot2)的主要内容,如果未能解决你的问题,请参考以下文章

iOS 动画绘制线条颜色渐变的折线图

数据可视化—绘制简单的折线图

需要帮助使用 ggplot 绘制带有五条线的折线图

为R中的每个客户端绘制不同颜色的折线图[重复]

在ggplot2中绘制两个具有相同y变量但不同x变量的箱线图

Canvas---绘制折线图