R语言 如何制作多变量条形图?

rn0zuynd  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(123)

R初学者:我有一个数据集“flying”,其中包含一些变量,询问某个行为有多粗鲁(“不,一点也不粗鲁”,“是的,有点粗鲁”或“是的,非常粗鲁”)。现在我被要求制作一个水平条形图,显示每个问题的回答百分比,但我不确定如何解决这个问题。
这就是它应该看起来的样子::


的数据
我已经做了一个新的框架,只包含绘图所需的变量

flying65 <- cleaned_flying %>%
  filter(gender == 'Male') %>%
  select(wake_passenger_walk, baby_on_plane, moving_to_unsold_seat, talking_to_seatmate, switch_for_family)

字符串
我应该如何继续下去?
编辑:我尝试使用likert包,因为它看起来像我需要的。

flying65 <- flying65 %>%
  mutate(moving_to_unsold_seat = ifelse(moving_to_unsold_seat == 'No, not rude at all', 'No, not at all rude', moving_to_unsold_seat))

flying65$wake_passenger_walk = factor(flying65$wake_passenger_walk,
                       levels = c("No, not at all rude", 
                                  "Yes, somewhat rude", "Yes, very rude"),
                       ordered = TRUE)
flying65$baby_on_plane = factor(flying65$baby_on_plane,
                       levels = c("No, not at all rude", 
                                  "Yes, somewhat rude", "Yes, very rude"),
                       ordered = TRUE)
flying65$moving_to_unsold_seat = factor(flying65$moving_to_unsold_seat,
                       levels = c("No, not at all rude", 
                                  "Yes, somewhat rude", "Yes, very rude"),
                       ordered = TRUE)
flying65$talking_to_seatmate = factor(flying65$talking_to_seatmate,
                       levels = c("No, not at all rude", 
                                  "Yes, somewhat rude", "Yes, very rude"),
                       ordered = TRUE)
flying65$switch_for_family = factor(flying65$switch_for_family,
                       levels = c("No, not at all rude", 
                                  "Yes, somewhat rude", "Yes, very rude"),
                       ordered = TRUE)

likert(flying65)


然而,我一直得到这个错误消息:“错误在likert(flying65):所有项目(列)必须有相同的水平数”
下面是dput(head(flying65))输出:

structure(list(wake_passenger_walk = structure(c(2L, 2L, 2L, 
3L, 1L, 2L), levels = c("No, not at all rude", "Yes, somewhat rude", 
"Yes, very rude"), class = c("ordered", "factor")), baby_on_plane = structure(c(2L, 
2L, 2L, 3L, 1L, 2L), levels = c("No, not at all rude", "Yes, somewhat rude", 
"Yes, very rude"), class = c("ordered", "factor")), moving_to_unsold_seat = structure(c(1L, 
1L, 1L, 2L, 1L, 1L), levels = c("No, not at all rude", "Yes, somewhat rude", 
"Yes, very rude"), class = c("ordered", "factor")), talking_to_seatmate = structure(c(1L, 
1L, 1L, 1L, 2L, 1L), levels = c("No, not at all rude", "Yes, somewhat rude", 
"Yes, very rude"), class = c("ordered", "factor")), switch_for_family = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), levels = c("No, not at all rude", "Yes, somewhat rude", 
"Yes, very rude"), class = c("ordered", "factor"))), row.names = c(NA, 
-6L), .internal.selfref = <pointer: (nil)>, na.action = structure(c(`1` = 1L, 
`2` = 2L, `9` = 9L, `12` = 12L, `13` = 13L, `14` = 14L, `15` = 15L, 
`17` = 17L, `20` = 20L, `23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L, 
`27` = 27L, `31` = 31L, `33` = 33L, `39` = 39L, `40` = 40L, `42` = 42L, 
`44` = 44L, `46` = 46L, `51` = 51L, `54` = 54L, `57` = 57L, `64` = 64L, 
`71` = 71L, `72` = 72L, `73` = 73L, `76` = 76L, `81` = 81L, `85` = 85L, 
`86` = 86L, `88` = 88L, `89` = 89L, `91` = 91L, `92` = 92L, `93` = 93L, 
`103` = 103L, `104` = 104L, `105` = 105L, `106` = 106L, `108` = 108L, 
`110` = 110L, `112` = 112L, `115` = 115L, `116` = 116L, `119` = 119L, 
`120` = 120L, `121` = 121L, `122` = 122L, `130` = 130L, `131` = 131L, 
`132` = 132L, `134` = 134L, `135` = 135L, `136` = 136L, `137` = 137L, 
`144` = 144L, `146` = 146L, `148` = 148L, `151` = 151L, `157` = 157L, 
`159` = 159L, `160` = 160L, `169` = 169L, `172` = 172L, `173` = 173L, 
`177` = 177L, `179` = 179L, `182` = 182L, `184` = 184L, `185` = 185L, 
`186` = 186L, `189` = 189L, `190` = 190L, `191` = 191L, `193` = 193L, 
`195` = 195L, `196` = 196L, `201` = 201L, `202` = 202L, `205` = 205L, 
`208` = 208L, `213` = 213L, `214` = 214L, `216` = 216L, `220` = 220L, 
`222` = 222L, `227` = 227L, `228` = 228L, `229` = 229L, `232` = 232L, 
`235` = 235L, `237` = 237L, `239` = 239L, `242` = 242L, `245` = 245L, 
`247` = 247L, `248` = 248L, `252` = 252L, `253` = 253L, `254` = 254L, 
`258` = 258L, `259` = 259L, `265` = 265L, `266` = 266L, `274` = 274L, 
`278` = 278L, `279` = 279L, `282` = 282L, `283` = 283L, `288` = 288L, 
`297` = 297L, `298` = 298L, `299` = 299L, `300` = 300L, `303` = 303L, 
`308` = 308L, `310` = 310L, `311` = 311L, `315` = 315L, `321` = 321L, 
`323` = 323L, `326` = 326L, `327` = 327L, `329` = 329L, `330` = 330L, 
`341` = 341L, `342` = 342L, `343` = 343L, `344` = 344L, `346` = 346L, 
`349` = 349L, `352` = 352L, `354` = 354L, `358` = 358L, `359` = 359L, 
`363` = 363L, `367` = 367L, `370` = 370L, `371` = 371L, `378` = 378L, 
`382` = 382L, `385` = 385L, `393` = 393L, `394` = 394L, `396` = 396L, 
`398` = 398L, `403` = 403L, `404` = 404L, `406` = 406L, `407` = 407L, 
`408` = 408L, `415` = 415L, `417` = 417L, `420` = 420L, `423` = 423L, 
`427` = 427L, `429` = 429L, `436` = 436L, `438` = 438L, `439` = 439L, 
`440` = 440L, `443` = 443L, `444` = 444L, `446` = 446L, `449` = 449L, 
`450` = 450L, `452` = 452L, `453` = 453L, `454` = 454L, `472` = 472L, 
`476` = 476L, `477` = 477L, `478` = 478L, `479` = 479L, `481` = 481L, 
`482` = 482L, `487` = 487L, `491` = 491L, `494` = 494L, `500` = 500L, 
`501` = 501L, `504` = 504L, `507` = 507L, `510` = 510L, `511` = 511L, 
`515` = 515L, `518` = 518L, `519` = 519L, `522` = 522L, `525` = 525L, 
`526` = 526L, `527` = 527L, `528` = 528L, `529` = 529L, `532` = 532L, 
`536` = 536L, `538` = 538L, `539` = 539L, `545` = 545L, `546` = 546L, 
`553` = 553L, `554` = 554L, `566` = 566L, `568` = 568L, `569` = 569L, 
`570` = 570L, `582` = 582L, `584` = 584L, `590` = 590L, `592` = 592L, 
`593` = 593L, `594` = 594L, `598` = 598L, `599` = 599L, `602` = 602L, 
`606` = 606L, `607` = 607L, `608` = 608L, `611` = 611L, `613` = 613L, 
`616` = 616L, `622` = 622L, `629` = 629L, `633` = 633L, `637` = 637L, 
`641` = 641L, `643` = 643L, `647` = 647L, `651` = 651L, `652` = 652L, 
`657` = 657L, `661` = 661L, `662` = 662L, `668` = 668L, `669` = 669L, 
`673` = 673L, `676` = 676L, `677` = 677L, `680` = 680L, `685` = 685L, 
`686` = 686L, `687` = 687L, `688` = 688L, `691` = 691L, `694` = 694L, 
`695` = 695L, `697` = 697L, `699` = 699L, `703` = 703L, `705` = 705L, 
`707` = 707L, `711` = 711L, `715` = 715L, `719` = 719L, `723` = 723L, 
`726` = 726L, `728` = 728L, `732` = 732L, `737` = 737L, `738` = 738L, 
`739` = 739L, `740` = 740L, `742` = 742L, `747` = 747L, `749` = 749L, 
`756` = 756L, `763` = 763L, `769` = 769L, `771` = 771L, `776` = 776L, 
`779` = 779L, `781` = 781L, `783` = 783L, `784` = 784L, `787` = 787L, 
`790` = 790L, `792` = 792L, `794` = 794L, `799` = 799L, `801` = 801L, 
`804` = 804L, `805` = 805L, `808` = 808L, `809` = 809L, `816` = 816L, 
`824` = 824L, `825` = 825L, `830` = 830L, `833` = 833L, `835` = 835L, 
`836` = 836L, `839` = 839L, `843` = 843L, `852` = 852L, `853` = 853L, 
`854` = 854L, `860` = 860L, `861` = 861L, `862` = 862L, `864` = 864L, 
`876` = 876L, `877` = 877L, `879` = 879L, `881` = 881L, `885` = 885L, 
`886` = 886L, `887` = 887L, `888` = 888L, `903` = 903L, `904` = 904L, 
`911` = 911L, `913` = 913L, `918` = 918L, `919` = 919L, `921` = 921L, 
`922` = 922L, `923` = 923L, `926` = 926L, `928` = 928L, `930` = 930L, 
`931` = 931L, `933` = 933L, `944` = 944L, `945` = 945L, `948` = 948L, 
`950` = 950L, `954` = 954L, `961` = 961L, `963` = 963L, `964` = 964L, 
`965` = 965L, `968` = 968L, `970` = 970L, `971` = 971L, `974` = 974L, 
`976` = 976L, `977` = 977L, `980` = 980L, `982` = 982L, `987` = 987L, 
`990` = 990L, `991` = 991L, `992` = 992L, `1002` = 1002L, `1003` = 1003L, 
`1006` = 1006L, `1008` = 1008L, `1009` = 1009L, `1010` = 1010L, 
`1014` = 1014L, `1019` = 1019L, `1021` = 1021L, `1022` = 1022L, 
`1027` = 1027L, `1030` = 1030L, `1031` = 1031L, `1032` = 1032L, 
`1034` = 1034L, `1037` = 1037L, `1038` = 1038L, `1039` = 1039L
), class = "omit"), class = c("tbl_df", "tbl", "data.frame"))

cnwbcb6i

cnwbcb6i1#

使用likert()时的问题是它不适用于tibble s。但您可以通过转换为data.frame()来解决这个问题:
注意事项:您提到的问题和错误消息与likert使用nlevels = length(levels(items[, 1]))计算级别数的事实有关(参见?likert)。在数据.frame的情况下,这很好,因为items[, 1]将返回矢量。然而,在tibble的情况下,items[, 1]仍然是tibble,因此导致nlevels = 0。不幸的是,这不是唯一的问题。
注2:此问题已在likert的开发版本中修复,该版本使用as.data.frame()tibble转换为 Dataframe 。

library(likert)

likert(
  data.frame(flying65)
) |> 
  plot()

字符串


的数据

相关问题