Home > OS >  ggplot with variables different columns in dataset
ggplot with variables different columns in dataset

Time:07-19

I have a dataset like this:

landtype O3 PM10 PM2.5 Month_name
urban 40 27 50 January
rural 30 25 40 January
urban 50 29 50 February
rural 20 25 40 February
urban 40 25 50 March
rural 30 25 40 March
urban 60 25 50 April
rural 30 25 40 April
urban 70 27 50 May
rural 30 25 40 May
urban 40 27 50 June
rural 30 25 40 June
urban 40 27 50 July
rural 30 25 40 July
urban 40 27 50 August
rural 30 25 40 August

Now I would like to create a histogram which shows the contrast between urban and rural and all the values 03,PM10 and PM2.5 for Months.

I used that code

data %>%
  ggplot(aes(x = Month_name, y = O3,PM10,PM2.5, fill = O3,PM10,PM2.5))  
  geom_col()  
  facet_wrap(~landtype)    ggtitle("air pollution contrast between city and countryside")

BUt it is not what i want

CodePudding user response:

You probably want to reshape to long first to do this:

library(ggplot2)

data  |>
  tidyr::pivot_longer(
   -c(landtype, Month_name), 
   names_to = "pollution_type"
  ) |>
  dplyr::mutate(
    month_short = substr(Month_name, 1, 3)
  )  |>
  ggplot(aes(
    x = month_short, 
    y = value, 
    fill = landtype))  
  geom_bar(position = "dodge", stat = "identity")  
  ggtitle("air pollution contrast between city and countryside")  
  facet_wrap(vars(pollution_type))  
  theme_bw()  
  theme(
    axis.text.x = element_text(angle = 60), 
    axis.title.x = element_blank()
  )  
  ylab("μg")

enter image description here

  •  Tags:  
  • r
  • Related