温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - How can i draw a barplot with 3 variables?
bar-chart ggplot2 r

r - 如何绘制具有3个变量的条形图?

发布于 2020-04-22 16:33:35

我在制作barplot时遇到了一些麻烦。我想制作一个具有3个序数变量的比例尺(比例尺:是,不,我不知道(每个))我需要x轴并排显示这些条形(是1,是2,是3,否1,否2 ... 等等)。它们的y轴应显示频率或百分比。每个变量都属于面板中的不同波形,我想通过小节显示更改。

到目前为止,我为每个变量绘制了一个图(请参见代码)

我需要的是结合这3个图,我只是还不知道该怎么做。我已经尝试过facet_wrap / facet_grid,但是我无法用这种方法解决我的问题。我也收到错误消息:
"Don't know how to automatically pick scale for object of type haven_labelled. Defaulting to continuous."
因此无法显示x轴上的标签。有人可以帮帮我吗?谢谢,英格丽。

这是我的数据:

dput(veraenderung[1:4, ])
structure(list(vor = structure(c(2, 3, 3, 1), label = "Erwartung, dass sich durch die Teilnahme an der FoBi Veränderungen im Berufsallt", labels = c(ja = 1, 
nein = 2, `weiß nicht` = 3), class = "haven_labelled"), nach = structure(c(2, 
3, 1, 1), label = "Erwarten Sie, dass Ihre Teilnahme an dieser FoBi zu Veränderungen in Ihrem Beruf", labels = c(ja = 1, 
nein = 2, `weiß nicht` = 3), class = "haven_labelled"), sechs_monate_spaeter = structure(c(2, 
2, 1, 3), label = "Hat sich durch Ihre Teilnahme an der Fortbildung zur interkulturellen Kompetenz", labels = c(ja = 1, 
nein = 2, `weiß nicht` = 9), class = "haven_labelled"), Welle123 = c(1, 
1, 1, 1)), na.action = structure(c(`4` = 4L, `7` = 7L, `8` = 8L, 
`9` = 9L, `10` = 10L, `11` = 11L, `12` = 12L, `13` = 13L, `14` = 14L, 
`15` = 15L, `16` = 16L, `17` = 17L, `19` = 19L, `20` = 20L, `24` = 24L, 
`26` = 26L, `27` = 27L, `29` = 29L, `30` = 30L, `31` = 31L, `33` = 33L, 
`34` = 34L, `35` = 35L, `36` = 36L, `37` = 37L, `38` = 38L, `39` = 39L, 
`41` = 41L, `43` = 43L, `44` = 44L, `46` = 46L, `47` = 47L, `48` = 48L, 
`49` = 49L, `50` = 50L, `52` = 52L, `54` = 54L, `55` = 55L, `58` = 58L, 
`59` = 59L, `60` = 60L, `63` = 63L, `64` = 64L, `66` = 66L, `68` = 68L, 
`71` = 71L, `72` = 72L, `73` = 73L, `74` = 74L, `75` = 75L, `78` = 78L, 
`80` = 80L, `81` = 81L, `82` = 82L, `83` = 83L, `84` = 84L, `86` = 86L, 
`87` = 87L, `91` = 91L, `92` = 92L, `94` = 94L, `97` = 97L, `99` = 99L, 
`101` = 101L, `102` = 102L, `105` = 105L, `106` = 106L, `107` = 107L, 
`108` = 108L, `109` = 109L, `112` = 112L, `113` = 113L, `114` = 114L, 
`116` = 116L, `117` = 117L, `119` = 119L, `121` = 121L, `122` = 122L, 
`123` = 123L, `124` = 124L, `127` = 127L, `128` = 128L, `130` = 130L, 
`132` = 132L, `134` = 134L, `135` = 135L, `136` = 136L, `138` = 138L, 
`139` = 139L, `140` = 140L, `141` = 141L, `142` = 142L, `144` = 144L, 
`146` = 146L, `147` = 147L, `148` = 148L, `149` = 149L, `151` = 151L, 
`152` = 152L, `153` = 153L, `156` = 156L, `157` = 157L, `159` = 159L, 
`164` = 164L, `165` = 165L, `166` = 166L, `168` = 168L, `169` = 169L, 
`170` = 170L, `172` = 172L, `173` = 173L, `174` = 174L, `176` = 176L, 
`177` = 177L, `178` = 178L, `179` = 179L, `180` = 180L, `181` = 181L, 
`183` = 183L, `184` = 184L, `185` = 185L, `190` = 190L, `191` = 191L, 
`192` = 192L, `194` = 194L, `195` = 195L, `196` = 196L, `197` = 197L, 
`202` = 202L, `205` = 205L, `206` = 206L, `208` = 208L, `209` = 209L, 
`210` = 210L, `211` = 211L, `212` = 212L, `213` = 213L, `215` = 215L, 
`216` = 216L, `217` = 217L, `218` = 218L, `221` = 221L, `223` = 223L, 
`225` = 225L, `226` = 226L, `227` = 227L, `228` = 228L, `229` = 229L, 
`230` = 230L, `231` = 231L, `232` = 232L, `233` = 233L, `234` = 234L, 
`235` = 235L, `236` = 236L, `237` = 237L, `238` = 238L, `239` = 239L, 
`240` = 240L, `241` = 241L, `242` = 242L, `243` = 243L, `244` = 244L, 
`245` = 245L, `246` = 246L, `247` = 247L, `248` = 248L, `249` = 249L
), class = "omit"), row.names = c(NA, 4L), class = "data.frame")

这是代码:

    library(tidyverse)
veraenderung <- ikoe %>%
      select(v13, wn06, xn2, Welle123) %>%
      rename(vor = v13,
             nach = wn06,
             sechs_monate_spaeter = xn2) %>%
      na.omit(veraenderung) %>%
      as.data.frame()

    ggplot(veraenderung, aes(x = vor)) +
      geom_bar()

    ggplot(veraenderung, aes(x = nach)) +
      geom_bar()

    ggplot(veraenderung, aes(x = sechs_monate_spaeter)) +
      geom_bar()

查看更多

提问者
Ingrid
被浏览
17
Tjebo 2020-02-07 00:16

您的haven对象对于整洁操作有点挑战。请参阅下文,我建议使此对象更“干净”(删除标签,将值更改为字符类)。然后进行长时间绘制。

library(tidyverse)

names(veraenderung) <- c('vor','nach','sechs','welle') #remove labels in names
veraenderung <- as_tibble(veraenderung) %>% transmute_all(as.character) #change values to character class

veraenderung <- veraenderung %>% pivot_longer(cols = everything(), names_to = 'key', values_to = 'value')

ggplot(veraenderung, aes(key)) +
  geom_bar(aes(fill = value), position = position_dodge(preserve = 'single'))
#try without preserve or position_dodge and see what happens

reprex软件包(v0.3.0)创建于2020-02-06