2 series barplot ggplot2 or Openair

271 views Asked by At

I am trying to create a bar plot of air quality data. I want Monitoring Site on the X-axis, and the concentration of 2 pollutants measured at each site against the y-axis, e.g. at the London site I measured 5 ng/m3 of antimony (series 1) and 10 ng/m3 barium (series 2). So the y-axis will just be Concentration (ng/m3).

I have searched already and the only examples of getting a 2nd series seem to work if the second series is a qualitative property that can be defined by colour, so you still only have one bar not two.

Also the examples seem to involve entering the data frame in the code manually, but I have a lot of data and want to import the file. I have successfully imported from a csv, but can't see how to refer to it in the code.

Openair only seems to plot stacked bar charts that have to relate to dates on the X-axis with timeProp.

Sorry I am very inexperienced with programming like this, I hope my question makes sense. Any help appreciated!

1

There are 1 answers

8
Santiago I. Hurtado On

I think you need something like this:

library(ggplot2)

data <- data.frame(cite= c('london','madrid','barcelona','london','madrid','barcelona'),
               contaminent = c('Argon','Argon','Argon','Barium','Barium','Barium'),
               Concentration= runif(6)*5)

ggplot() + geom_col(data = data,aes(x = cite,y=Concentration,fill=contaminent))

Edit:

Now suppose your data is name my_data and organize as follows: first column name cites with the cites names, then each column's name is a pollution with the values of the pollution concentration for each cite. Then your code for manage your data and used in ggplot could be:

library(ggplot2)
new_data <- data.frame(cites = rep(my_data$cites,ncol(my_data)-1),
                       contaminent_type= rep(colnames(my_data)[2:ncol(my_data)],each=nrow(my_data)),
                       Concentration= as.vector(my_data[,2:ncol(my_data)]))

ggplot() + geom_col(data = new_data,aes(x = cites,y=Concentration,fill=contaminent_type))