【问题标题】:How do you create this gauge chart using ggplot?你如何使用ggplot创建这个仪表图?
【发布时间】:2020-05-23 18:07:02
【问题描述】:

我对 R 很陌生,我发现的类似问题的答案让我难以理解。

我有一个数据框 res 的调查回复。每个受访者对应一行,每个问题对应一列。我想将对特定问题 res$Q13 的回答可视化为仪表图,显示回答“是”的受访者比例。

与我的目标最接近的是:https://pomvlad.files.wordpress.com/2018/05/pomvlad-dials.png

我想要一个看起来像那个的仪表盘,但我显然不需要刻面层,我只需要一个仪表盘。我已将代码(来源:https://pomvlad.blog/2018/05/03/gauges-ggplot2/ 信用:https://pomvlad.blog/author/pomvlad/)缩减为我认为需要的部分,注释掉我认为不必要的行,并添加一些随机颜色以帮助我识别哪些代码行生成图表的哪些位:

ggplot(res, aes(fill = "violet", ymax = 100, ymin = 0, xmax = 2, xmin = 1)) +
  geom_rect(aes(ymax=1, ymin=0, xmax=2, xmin=1), fill = "#ece8bd") +
  geom_rect() + 
  coord_polar(theta = "y", start = -pi/2) + xlim(c(0, 2)) + ylim(c(0, 2)) +
  geom_text(aes(x = 0, y = 0, label = "title1", colour = "blue"), size = 6.5) +
  geom_text(aes(x = 1.5, y = 1.5, label = "title2"), size = 4.2) + 
  #facet_wrap(~title, ncol = 5) +
  theme_void() +
  #scale_fill_manual(values = c("red" = "#C9146C", "orange" = "#DA9112", "green" = "#129188")) +
  #scale_colour_manual(values = c("red" = "#C9146C", "orange" = "#DA9112", "green" = "#129188")) +
  theme(strip.background = element_blank(),
  strip.text.x = element_blank()) +
  guides(fill = FALSE) +
  guides(colour = FALSE)

我得到的只是仪表和标题的黄色背景。我对如何使仪表图显示回答“是”的受访者百分比感到困惑。任何人都可以帮忙吗?提前谢谢!

【问题讨论】:

  • 至少,您需要使用数据框。您的 df 只是一个向量。尝试从df = data.frame(value = c("Yes", "No", "Yes", "Yes", "")) 开始。然后,您需要在绘图中的某处使用该列名称value,在美学映射aes() 内。如果您查看示例中使用的 data 并将您的数据置于类似的结构中,您可能会有更好的运气。
  • 好的,谢谢,我已根据您的评论进行了编辑。知道我应该在哪里包含 res$Q13 吗?如果我这样做aes(y=res$Q13,...,我会得到错误:提供给连续刻度的离散值。
  • 最简单的解决方案是将您的数据格式转换为博客中的格式。创建一个具有单行的数据框,其中包含以下变量:“变量”、“百分比”、“组”、“标签”和“标题”。将此数据框传递到博客中的 ggplot 脚本,它就可以工作了。
  • 我想到了这一点,但我更想知道如何分析原始数据而不必创建额外的数据框。
  • 如果您使用一致的数据格式,ggplot 的工作非常简单。将数据转换为要绘制的漂亮摘要通常要简单得多。在你的绘图代码中包含像rag(round(nrow(res[res$Q13 == "Yes",])/nrow(res),2)) 这样的长转换是脆弱的——它很容易产生错误,难以调试,而且为新数据调整很烦人。

标签: r ggplot2


【解决方案1】:

知道了,谢谢你的帮助!

Q13.GaugeChart <- ggplot(res, aes(fill = rag(round(nrow(res[res$Q13 == "Yes",])/nrow(res),2)), ymax = nrow(res[res$Q13 == "Yes",])/nrow(res), ymin = 0, xmax = 2, xmin = 1)) +
  geom_rect(aes(ymax=1, ymin=0, xmax=2, xmin=1), fill = "#ece8bd") +
  geom_rect() + 
  coord_polar(theta = "y", start = -pi/2) + xlim(c(0, 2)) + ylim(c(0, 2)) +
  geom_text(aes(x = 0, y = 0, label = paste(round(100*nrow(res[res$Q13 == "Yes",])/nrow(res),0),"%", sep = ""), colour = rag(round(nrow(res[res$Q13 == "Yes",])/nrow(res),2)), size = 6.5)) +
  geom_text(aes(x = 1, y = 1.5, label = "TITLE"), size = 4.2) + 
  theme_void() +
  theme(legend.position = "none") +
  scale_fill_manual(values = c("red" = "#C9146C", "orange" = "#DA9112", "green" = "#129188")) +
  scale_colour_manual(values = c("red" = "#C9146C", "orange" = "#DA9112", "green" = "#129188")) +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank()) +
  guides(fill = FALSE) +
  guides(colour = FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2021-04-04
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多