1

I am trying to make a plot with ggplot2 and i cannot really get my head around how to do it.

My df is laid out like the below:

enter image description here

I would want the graph to show the X axis with the Area name with a bar chart of each of the column headings, I am able to do it for one of them but i am unsure how to combine them together.

Or I have seen facet_wrap been used which could create a graph for each area name and the 4 bars with their corresponding values in each facet.

This is what I have come up with so far...

wlcalcs %>% ggplot(aes(y= `80+ % d1`, x= `Area`)) + geom_bar(stat='identity')

So I have combined the fields I need into two new fields on my dataframe called "New" and "Values" this is my updated ggplot2 code below but doesnt seem to work for whatever reason. My New field is effectively 80+ %d1 X with x corresponding to its value line by line.

I want a facet for each Area name and I want the values of 'New' to be my X axis and values of 'Y' (the percentages) to be the fill for each of them bars.

I am probably doing somethign wrong but i hope im on teh right track

test1 <- ggplot(data = test) + 
  geom_bar(mapping = aes(x = `New`, y = `Values`)) + 
  facet_wrap(~ group_by(`Ward Name`, nrow = 2))

Any help is appreciated, this is the first time i have used R pretty much and first time trying plots out. I assume some combined field needs to be done as I have had a search around but cannot really understand it too well

paulr23
  • 53
  • 5
  • Use `pivot_longer()` https://tidyr.tidyverse.org/reference/pivot_longer.html to transform your data. Then set `y=` to the resulting column. – M.Viking Jan 27 '21 at 15:44
  • 1
    Ok thanks, do I need to add the area name into that also so I can use it in the ggplot(x....) parts? – paulr23 Jan 27 '21 at 15:49
  • 1
    @M.Viking would you mind adding your suggestion to the linked question? - A pivot_longer answer seems still outstanding, and would be appropriate. Better answer the other question – tjebo Jan 27 '21 at 16:03
  • 1
    So I have tried to pivot longer part, and I have got this to work how I want, however it is mapping this out which i am going to struggle with, effectively I would want a facet for each area name and the x axis to show each of the 4 colums and its corresponding percentages however unsure how to do that. – paulr23 Jan 27 '21 at 16:15
  • 1
    I have updated my initial post – paulr23 Jan 27 '21 at 16:24
  • 1
    Hmm this doesnt seem to work, the code in the console just has a + which i think means it is waiting for another input – paulr23 Jan 27 '21 at 16:38
  • @tjebo Thank you for the suggestion, I added the answer. @paulr23 The console at a `+` means it is waiting in RStudio press `ESC`, in R use `CTRL-D`. Then try a this facet command, `... + facet_wrap("Ward Name")` – M.Viking Jan 27 '21 at 16:42
  • Ok I had to add (stat = "identity") onto the end of the geom_bar part and it seemed to have worked ok.. Not sure what this means entirely but i believe its kinda good now – paulr23 Jan 27 '21 at 16:51
  • https://stackoverflow.com/questions/59008974/why-is-stat-identity-necessary-in-geom-bar-in-ggplot#59009108 – M.Viking Jan 27 '21 at 17:46

0 Answers0