您如何使用ggplot创建此量表?

问题描述 投票:2回答:1

我对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)

我所得到的是量规和标题的黄色背景。我对如何使量表显示回答“是”的受访者所占的比例感到困惑。有人可以帮忙吗?预先谢谢!

r ggplot2
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.