简体   繁体   中英

Plot multiple nominal variables

I need to combine awareness measures for multiple websites into a single barplot. Awareness for each website is measured by a separate nominal variable (Yes/No). I would like to create a plot with one bar per website indicating what percentage of people know the respective website. My first try was creating a vector with number of "Yes" Values divided by "Total Rows" for each website. But with 69 websites there should be a more intelligent way.

The following solution on stack overflow kind of fits what I want to do. How do I plot a number of categorical variables on a graph in R?

But if possible I would like to only display the yes values. Also I would like everything in one graph rather than multiple graphs.

My data frame has the following structure:

gender <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Male", "Female", "Missing Value"))
age <- sample(18:55, 5, replace=TRUE)
web1 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web2 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web3 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web4 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
web5 <- factor(sample(1:2, 5, replace=TRUE), levels = c(1,2,99), labels = c("Yes", "No", "Missing Value"))
df <- data.frame(gender, age, web1, web2, web3, web4, web5)
df

We can use tidyverse to create a tbl of the information you want and then graph it.

library(tidyverse)

df2 <- gather(df, "website", "aware", 3:7, factor_key = T) %>%
  group_by(website, aware) %>%
  summarize(n = n()) %>%
  ungroup() %>%
  filter(aware == "Yes") %>%
  complete(website, fill = list(n = 0))

ggplot(data = df2) +
  geom_bar(aes(website, n), stat = "identity")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM