How to plot several categorical variables in r

Hi,
am new to RStudio and I would like to learn how to plot several categorical variables.
Here is my data

id nst_drought_ew nst_flood_mtgn nst_imp_lu nst_off_farm nst_agric_pract nst_pdates_crv nst_aconst nst_fr_cons
1 1 1 3 1 3 3 1 4
2 3 3 3 3 3 3 3 3
3 2 2 2 4 3 3 3 4
4 2 3 4 3 4 4 1 4
5 1 1 4 1 3 4 1 4
6 3 3 4 3 3 3 3 2
7 1 2 3 2 3 4 2 2
8 1 1 3 3 3 4 3 3
9 1 2 4 4 4 3 3 3
10 1 1 3 1 3 3 3 1
11 2 2 4 4 2 3 2 4
12 4 3 4 3 3 4 1 4
13 1 3 3 2 3

Take a look at FAQ: What's a reproducible example (`reprex`) and how do I do one? to see how you can help us to answer your question.

But even without reprex, what exactly do you want to achieve? Do you have an example of the plot in mind? Or better link to the type of plot you want to get?

Thanks Yassin for the reply. My data shows responses for non structural measures where
I would like to produce a stacked bar graph for 8 variables each having 4 categories where 1 = Very low, 2 = Low, 3 = High and 4 = Very High

Since you are new on this forum, I'm going to give you a hand with your reprex this time, but you really need to read this FAQ: What's a reproducible example (reprex) and how do I do one?

Is this close to what you want to do?

# Sample Data
df <- data.frame(
    id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13),
    nst_drought_ew = c(1, 3, 2, 2, 1, 3, 1, 1, 1, 1, 2, 4, NA),
    nst_flood_mtgn = c(1, 3, 2, 3, 1, 3, 2, 1, 2, 1, 2, 3, 1),
    nst_imp_lu = c(3, 3, 2, 4, 4, 4, 3, 3, 4, 3, 4, 4, NA),
    nst_off_farm = c(1, 3, 4, 3, 1, 3, 2, 3, 4, 1, 4, 3, 3),
    nst_agric_pract = c(3, 3, 3, 4, 3, 3, 3, 3, 4, 3, 2, 3, NA),
    nst_pdates_crv = c(3, 3, 3, 4, 4, 3, 4, 4, 3, 3, 3, 4, 3),
    nst_aconst = c(1, 3, 3, 1, 1, 3, 2, 3, 3, 3, 2, 1, NA),
    nst_fr_cons = c(4, 3, 4, 4, 4, 2, 2, 3, 3, 1, 4, 4, NA)
)

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
    gather(variable, category, -id) %>%
    filter(!is.na(category)) %>% 
    count(variable, category) %>% 
    ggplot(aes(x = variable,
               y = n,
               fill = factor(category, labels = c("Very Low", "Low", "High", "Very High")))) +
    geom_col() +
    labs(x = "Variable",
         y = "Count",
         fill = "Category") +
    theme(axis.text.x = element_text(angle=30, hjust=1, vjust = 1))

Created on 2019-02-12 by the reprex package (v0.2.1)

1 Like

Thank you so so much andres for your support and effort put in. That's exactly what I want. I am also reading the information from the link you have posted.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.