YYM17
YYM17

Reputation: 353

How to use ggplot2 to plot several character variables in one plot?

I have a dataset like below.

   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"))

I want to use ggplot2 to produce a frequency histogram of the variables with prefix "orxh" (tehy're binary variables). How can I put all the "orxh" in single one plot? Thank you.

Upvotes: 1

Views: 191

Answers (1)

jazzurro
jazzurro

Reputation: 23574

Here is one way for you. You convert your data to a long-format data frame with pivot_longer(), and use ggplot2. I created two graphs for you. I hope either works well.

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

Upvotes: 3

Related Questions