【问题标题】:Line chart plotting nested categorical values for multiple groups (ggplot2)绘制多个组的嵌套分类值的折线图 (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 的新手,所以我很感激你可能有的任何解决方案。

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

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

感谢您的帮助!

【问题讨论】:

标签: r ggplot2 linechart axis-labels facet


【解决方案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 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2022-01-19
    相关资源
    最近更新 更多