-2

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)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Where is `gather` function from? As far as I know, it's in `tidyr` packge to convert wide data to long, since `tidyr 1.0.0` came, you should use `pivot_longer` instead. And you need to provide a sample of your data to let us work on. – yusuzech Nov 19 '19 at 00:09
  • This may help: https://stackoverflow.com/questions/4877357/how-to-plot-all-the-columns-of-a-data-frame-in-r – ravic_ Nov 19 '19 at 00:12
  • can you also provide more information about where Iowa_Liquor etc is coming from? – StupidWolf Nov 19 '19 at 00:28
  • It's coming from Iowa. – Simon Woodward Nov 19 '19 at 00:30
  • Hello, To provide a sample of your data use the function `dput(YOURDATA)`. If your data are very long then something like: `dput(head(YOURDATA))` if it is a dataframe. I think the function you might be looking for in `ggplot2` is `facet_grid` or `facet_wrap`. – QAsena Nov 19 '19 at 00:31
  • Ravic I'm not trying to plot every column. I just want plots for all the values in ONE column. There are 7 different types of values in the Category Column. – diju kun Nov 19 '19 at 01:31
  • Data is too big to post even with head – diju kun Nov 19 '19 at 01:31

1 Answers1

0

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

Chris Umphlett
  • 380
  • 3
  • 15