Saya memiliki dataset seperti di bawah ini.

   sex       hpvvac orxh06   orxh11   orxh16   orxh18   orxh26   orxh31   orxh33   orxh35  
   <fct>  <dbl+lbl> <fct>    <fct>    <fct>    <fct>    <fct>    <fct>    <fct>    <fct>   
 1 female   0 [no]  NA       NA       NA       NA       NA       NA       NA       NA      
 2 male     0 [no]  negative negative positive negative negative negative negative negative
 3 male     1 [yes] negative negative negative negative negative negative negative negative
 4 female   0 [no]  negative negative negative negative negative negative negative negative
 5 female  NA       negative negative negative negative negative negative negative negative
 6 female  NA       negative negative negative negative negative negative negative negative
 7 male     1 [yes] negative negative positive negative negative negative negative negative
 8 male     0 [no]  positive negative negative negative negative negative negative negative
 9 male     0 [no]  negative negative negative negative negative negative negative negative
10 female   0 [no]  negative negative negative negative negative negative negative positive
structure(list(sex = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L), .Label = c("male", 
"female"), class = c("labelled", "factor"), label = "sex"), hpvvac = structure(c(0, 
0, 1, 0, NA, NA, 1, 0, 0, 0, 1, 0, 0, NA, 0, 0, 0, NA, 0, 0, 
0), labels = c(no = 0, yes = 1), class = "haven_labelled"), orxh06 = structure(c(NA, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv06"), orxh11 = structure(c(NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv11"), orxh16 = structure(c(NA, 1L, 2L, 
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv16"), orxh18 = structure(c(NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv18"), orxh26 = structure(c(NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv26"), orxh31 = structure(c(NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv31"), orxh33 = structure(c(NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv33"), orxh35 = structure(c(NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("positive", "negative"), class = c("labelled", 
"factor"), label = "hpv35")), row.names = c(NA, -21L), class = c("tbl_df", 
"tbl", "data.frame"))

Saya ingin menggunakan ggplot2 untuk menghasilkan histogram frekuensi variabel dengan awalan "orxh" (variabel biner). Bagaimana saya bisa meletakkan semua "orxh" dalam satu plot? Terima kasih.

1
YYM17 1 Maret 2020, 03:20

1 menjawab

Jawaban Terbaik

Inilah salah satu cara untuk Anda. Anda mengonversi data ke bingkai data format panjang dengan pivot_longer(), dan menggunakan ggplot2. Saya membuat dua grafik untuk Anda. Saya harap keduanya bekerja dengan baik.

library(tidyverse)

pivot_longer(mydf, cols = contains("orxh"), names_to = "variable", values_to = "value") %>% 
ggplot() +
geom_bar(aes(x = variable, y = ..count.., fill = value))

enter image description here

pivot_longer(mydf, cols = contains("orxh"), names_to = "variable", values_to = "value") %>% 
ggplot() +
geom_bar(aes(x = variable, y = ..count.., fill = value),
         position = "dodge2") 

enter image description here

3
jazzurro 1 Maret 2020, 00:40