ano273
ano273

Reputation: 69

plotting a graph with multiple bars in R

I am struggling to plot the following data and think it is because of the format of the data.

structure(list(HE_Provider = c("Coventry University", "The University of Leicester", 
"Total"), Bath_and_North_East_Somerset = c(15, 20, 205), Bedford = c(85, 
90, 1040), Blackburn_with_Darwen = c(10, 20, 95), Blackpool = c(10, 
5, 60), `Bournemouth,_Poole_and_Christchurch` = c(35, 15, 285
), Bracknell_Forest = c(15, 10, 210), Buckinghamshire = c(195, 
145, 1835), Cambridgeshire = c(130, 160, 2500), Central_Bedfordshire = c(115, 
70, 1120), Cheshire_East = c(45, 55, 935), Cheshire_West_and_Chester = c(25, 
40, 535), City_of_Bristol = c(40, 35, 390), City_of_Derby = c(65, 
135, 4115), City_of_Kingston_upon_Hull = c(25, 20, 265), City_of_Leicester = c(315, 
1275, 6860), City_of_Nottingham = c(65, 145, 5405), City_of_Plymouth = c(15, 
10, 135), City_of_Portsmouth = c(15, 15, 130), City_of_Southampton = c(15, 
20, 140), `City_of_Stoke-on-Trent` = c(50, 15, 475), City_of_York = c(35, 
20, 350), Cornwall = c(25, 25, 300), County_Durham = c(20, 40, 
330), Cumbria = c(30, 20, 305), Darlington = c(0, 15, 110), Derbyshire = c(100, 
145, 6925), Devon = c(50, 50, 630), Dorset = c(30, 20, 285), 
    East_Riding_of_Yorkshire = c(75, 45, 760), East_Sussex = c(55, 
    50, 650), Essex = c(365, 180, 3320), Gloucestershire = c(150, 
    85, 905), Greater_London = c(5550, 1930, 18285), Greater_Manchester = c(245, 
    280, 2820), Halton = c(5, 10, 80), Hampshire = c(180, 120, 
    1485), Hartlepool = c(5, 10, 55), Herefordshire = c(50, 15, 
    235), Hertfordshire = c(385, 270, 4815), Isle_of_Wight = c(10, 
    5, 90), Isles_of_Scilly = c(0, 0, 0), Kent = c(365, 195, 
    2590), Lancashire = c(75, 125, 985), Leicestershire = c(540, 
    980, 8010), Lincolnshire = c(145, 190, 7710), Luton = c(105, 
    75, 685), Medway = c(95, 35, 425), Merseyside = c(75, 120, 
    975), Middlesbrough = c(10, 5, 65), Milton_Keynes = c(265, 
    170, 2205), Norfolk = c(120, 115, 2410), North_East_Lincolnshire = c(20, 
    10, 810), North_Lincolnshire = c(20, 20, 810), North_Somerset = c(25, 
    15, 205), North_Yorkshire = c(500, 80, 1160), Northamptonshire = c(680, 
    510, 7505), Northumberland = c(10, 25, 235), Nottinghamshire = c(140, 
    185, 9410), Oxfordshire = c(280, 135, 1785), Peterborough = c(85, 
    135, 1560), Reading = c(75, 25, 260), Redcar_and_Cleveland = c(5, 
    5, 90), Rutland = c(5, 35, 345), Shropshire = c(60, 30, 500
    ), Slough = c(95, 40, 270), Somerset = c(40, 40, 490), South_Gloucestershire = c(40, 
    25, 310), South_Yorkshire = c(105, 180, 3220), `Southend-on-Sea` = c(35, 
    25, 345), Staffordshire = c(370, 150, 3825), `Stockton-on-Tees` = c(20, 
    15, 145), Suffolk = c(115, 115, 1935), Surrey = c(195, 155, 
    2900), Swindon = c(50, 25, 225), Telford_and_Wrekin = c(60, 
    20, 360), Thurrock = c(140, 40, 370), Torbay = c(5, 5, 65
    ), Tyne_and_Wear = c(45, 60, 680), Warrington = c(20, 20, 
    290), Warwickshire = c(2080, 210, 2825), West_Berkshire = c(35, 
    25, 300), West_Midlands = c(8315, 915, 8220), West_Sussex = c(105, 
    95, 1115), West_Yorkshire = c(200, 245, 3005), Wiltshire = c(90, 
    55, 630), Windsor_and_Maidenhead = c(40, 25, 405), Wokingham = c(70, 
    35, 395), Worcestershire = c(350, 110, 1350),       `England_(county_unitary_authority_unknown)` = c(0, 
    10, 770), Total_England = c(24990, 11530, 154930), Total = c(25380, 
    11845, 158480)), row.names = c(NA, -3L), class = "data.frame")

I would like to plot the Region on the bottom but don't have a title for these regions, with the numbers up the y axis and the fill being the university.

Upvotes: 0

Views: 54

Answers (2)

TarJae
TarJae

Reputation: 78917

We could bring the data in long format. For y we used log scale:

library(tidyverse)

df %>% 
  pivot_longer(-HE_Provider) %>% 
  group_by(HE_Provider, name) %>% 
  summarise(sum_value = sum(value)) %>% 
  ggplot(aes(x=name, y=log(sum_value), fill=HE_Provider))+
  geom_col(position=position_dodge())+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))

enter image description here

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

Reshape the data and plot with geom_col.

suppressPackageStartupMessages({
  library(dplyr)
  library(tidyr)
  library(ggplot2)
})

df1 %>%
  select(-matches("England"), -matches("Total")) %>%
  pivot_longer(-HE_Provider, names_to = "Region") %>%
  ggplot(aes(Region, value, fill = HE_Provider)) +
  geom_col() +
  theme_bw(base_size = 10) +
  theme(axis.text.x = element_text(size = 7, angle = 75, vjust = 1, hjust = 1),
        legend.position = "bottom")

Created on 2022-12-06 with reprex v2.0.2

Upvotes: 2

Related Questions