如何在堆积条形图中绘制条形百分比?

时间:2020-02-11 13:29:31

标签: r ggplot2

我一直在制作条形图。我想在堆积的条形图中显示不同变量的百分比。我到目前为止已经绘制了情节,但我无法在酒吧获得百分比。 到目前为止,我所看到的所有示例始终都有某种y变量用于构建百分比,但事实并非如此。 我认为我的问题与以下事实有关:我必须将数据转换为字符才能完成绘图,这样我才能获得百分比。

有人有什么想法吗,我该怎么办?

这是我的数据:

 dput(wissen2[,1:8]) structure(list(Offenburg_W1 = c(2, 2, 3, 4, 3, 2, 4, 2, 3, 2, 3, 3, 3, 3, 3, 4, 2, 1, 3, 2, 1, 2, 4, 4, 2, 3, 3, 3, 3, 3, 4, 4, 4, 1, 4, 4, 2, 2, 2, 4, 4, 2, 2, 3, 4, 1, 2, 3, 4, 4, 3, 4, 3, 2, 4, 4, 3, 2, 2, 4, 4, 3, 4, 2, 2, 2, 2, 2, 3, 2, 2, 3, 4, 2, 2, 2, 2, 3, 2, 4, 2, 2, 2, 2, 1, 4, 2, 3, 2, 2, 3, 3, 3, 3, 2, 3, 4, 3, 3, 4, 4, 3, 3, 4, 3, 4, 4, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 3, 3, 4, 2, 3, 2, 3, 3, 3), Offenburg_W2 = c(3, 3, 2, 4, 3, 3, 4, 4, 2, 2, 3, 3, 3, 3, 3, 4, 2, 4, 4, 4, 4, 2, 4, 4, 2, 4, 3, 2, 4, 4, 4, 3, 2, 1, 4, 1, 2, 2, 2, 3, 3, 2, 2, 4, 2, 2, 2, 3, 4, 4, 1, 3, 3, 3, 3, 4, 4, 2, 4, 4, 3, 4, 4, 2, 2, 3, 3, 2, 1, 2, 3, 1, 4, 2, 4, 3, 1, 3, 3, 2, 4, 3, 1, 3, 4, 4, 1, 3, 2, 3, 3, 3, 3, 3, 2, 3, 2, 3, 3, 3, 4, 1, 3, 4, 1, 4, 4, 3, 3, 4, 2, 2, 3, 4, 4, 2, 2, 4, 2, 3, 3, 4, 3, 3, 3, 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, 3, 3, 3, 2, 3, 3, 2, 3, 3, 4, 4, 3, 1, 2, 4, 2, 4, 3, 4, 4, 4, 1, 1, 4, 4, 3, 1, 4, 3, 4, 4, 3, 3, 2, 4, 4, 2, 4, 3, 2, 4, 2, 3, 4, 1, 3, 4, 4, 4, 4, 4, 3, 4, 4, 3, 3, 4, 2, 4, 4, 4, 4, 3, 3, 4, 2, 4, 3, 3, 4, 4, 3, 4, 4, 3, 4, 4, 4, 2, 4, 4, 4, 4, 1, 4, 4, 2, 3, 2, 3, 4, 4, 3, 4, 4, 4, 2, 2, 4, 3, 3, 3, 4, 4, 3, 4, 4, 2, 1, 4, 4)), class = "data.frame", row.names = c(NA, -128L))

代码如下:

wissen2 <- as_tibble(wissen2) %>%
  transmute_all(as.character)

wissen2 <- wissen2 %>%
 pivot_longer(cols = OffenburgW1:TuebingenW2,
              names_to = "Befragungszeitpunkt",
              values_to = "Antwort")


wissen2$Befragungszeitpunkt = forcats::fct_inorder(wissen2$Befragungszeitpunkt) #Reorder factor levels by sorting along another variable
wissen2$Befragungszeitpunkt = forcats::fct_rev(wissen2$Befragungszeitpunkt) #Reverse order of factor levels

wissen2_plot <- ggplot(wissen2, aes(x = Befragungszeitpunkt)) +
  geom_bar(aes(fill = Antwort), position = position_fill(reverse = T)) + 
  #scale_y_continuous(labels = scales::percent) +
  labs(x = "Befragungszeitpunkt", y= "Häufigkeit") +
  ggtitle("Schätzung der Höhe des Anteils der Bevölkerung mit Migrationshintergrund", subtitle = "in unterschiedlichen Städten (vor und nach der Befragung)") +
  theme(plot.title = element_text(hjust = 0.8)) +
  scale_fill_manual(values = c("#999933", "#E69F00", "#999999", "#CC6600"),
                    breaks=c(1, 2, 3, 4),
                    labels=c("Platz 1", "Platz 2", "Platz 3", "Platz 4")) +
  coord_flip() +
  theme(legend.position = "bottom") +
  guides(fill = "legend") +
  theme(axis.title.x = element_text(size = 10),
        axis.title.y = element_text(size = 10))  + 
  scale_x_discrete(labels = c ("OffenburgW1" = "Offenburg Welle 1", "OffenburgW2" = "Offenburg Welle 2", "HeilbronnW1" = "Heilbronn Welle 1", "HeilbronnW2" = "Heilbronn Welle 2", "MannheimW1" = "Mannheim Welle 1", "MannheimW2" = "Mannheim Welle 2", "TübingenW1" =  "Tübingen Welle 1", "TuebingenW2" = "Tübingen Welle 2")) 


wissen2_plot

0 个答案:

没有答案