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

Line chart plotting nested categorical values for multiple groups (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))

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

# 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 的新手,所以我很感激你可能有的任何解决方案。

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

编辑: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.

感谢您的帮助!

这是 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-09-29 创建