0

I am trying to plot two bar charts next to each other using df = melt but I can't figure out how to use it properly.

Here are the two bar charts I created:

unemployment_rate_averages<-
ggplot(data = urate_data,
       aes(x=regions, y=averages), fill = grp) +
  geom_bar(stat="identity")
Lactuca
  • 115
  • 2
  • 8
  • How do you mean 'next to each other'. Like the two plots you showed there side by side? – user438383 Jan 11 '21 at 18:38
  • Exactly! Side by side – Lactuca Jan 11 '21 at 18:39
  • 3
    Does this answer your question? [ggplot side by side geom\_bar()](https://stackoverflow.com/questions/25070547/ggplot-side-by-side-geom-bar) – user438383 Jan 11 '21 at 18:43
  • If it's two separate grobs, wouldn't [`patchwork`](https://cran.r-project.org/web/packages/patchwork/index.html) be a good fit? That is, `library(patchwork); combined_grob <- unemployment_rate_averages + unemployment_rate_changes`, and you can treat `combined_grob` as you would any other grob (I think), including `combined_grob` to view it on the console. – r2evans Jan 11 '21 at 18:46
  • 1
    Sorry, I deleted my answer since I think it's a duplicate, but ``urate_data %>% tidyr::pivot_longer(-regions) %>% ggplot(aes(x=regions, y=value)) + geom_bar(stat='identity') + facet_wrap(~name)`` will do what you need if not. – user438383 Jan 11 '21 at 18:49
  • @user438383 I tried using the code you posted but it says "Error: Aesthetics must be either length 1 or the same as the data (18): y"; also, I tried following what was posted in that topic (which is exactly what I want my graph to look like), but I keep getting confused over what I'm supposed to put inside "[]"; also, I don't really understand how they managed to select those two values in the legend. Hence why I asked to dumb it down as much as possible in my post :s @r2evans I tried what yous suggested but I get "Error: Can't add `unemployment_rate_changes` to a ggplot object.". Thanks guys! – Lactuca Jan 11 '21 at 18:53

1 Answers1

2
urate_data %>% 
    tidyr::pivot_longer(-regions) %>% 
    ggplot(aes(x=regions, y=value)) + 
    geom_bar(stat='identity') + 
    facet_wrap(~name)

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43