简体   繁体   中英

Making multiple bar graphs using gather in R

I have variables called Category, BottSold, and County. Basically I want to use the gather and ggplot functions to make multiple bar graphs at once. The bar graph would be County vs. BottSold, but each graph would only take into account a different value of category. Category has values like Vodka, Gin, Whiskey. Basically, the output would be a graph of the different counties vs Bottsold for Whiskey, one for Vodka, one for Gin, and so on. Here is what I've tried to do so far:

Iowa_Gather <- gather(Iowa_Liqour, Category, County, -BottSold) 
head(Iowa_Gather)

I think this is what you're describing.

library(tidyverse)
category <- c("Vodka", "Gin", "Whiskey", "Vodka", "Gin", "Whiskey", "Vodka", "Gin", "Whiskey")
county <- c("1", "1", "1", "2", "2", "2", "3", "3", "3")
bott_sold <- sample(20, 9)

df <- as.data.frame(cbind(category, county, bott_sold)) %>%
  mutate(bott_sold = as.numeric(as.character(bott_sold)))

ggplot(df) +
  facet_wrap(category ~ .) +
  geom_bar(aes(x = county, y = bott_sold), stat = "identity")

iowa_liquor_ggplot

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