0

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!

Sg1
  • 1
  • 2
  • Welcome to SO! Can you please provide some sample data or your data using `dput()` and get more specific about how your desired plot looks like? – alex_555 Nov 08 '18 at 10:20
  • u r asking to plot time series data? – sai saran Nov 08 '18 at 10:21
  • I am not trying to plot time series data. Sorry, can I attach files to show you data and the plot in Excel? – Sg1 Nov 08 '18 at 10:23
  • > glimpse(Sb_Ba) Observations: 24 Variables: 4 $ Site "London Marylebone Road ", "Swansea Morriston", "Belfast Centre",... $ Designation "Urban Traffic", "Urban Traffic ", "Urban Background", "Urban Bac... $ Sb 5.250, 2.990, 1.060, 2.400, 0.999, 1.650, 1.700, 0.947, 0.955, 1.... $ Ba 35.700, 13.000, 3.630, 6.530, 4.500, 7.700, 9.390, 1.580, 3.030, ... – Sg1 Nov 08 '18 at 10:26
  • Example plot: https://projects.ncsu.edu/labwrite/res/gt/gt-bar-home.html under Creating a bar graph with two independent variables. I would have Monitoring site instead of Mammal, Concentration instead of Count and Antimony and Barium instead of Week 1 and 2 – Sg1 Nov 08 '18 at 10:50
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 09 '18 at 02:55
  • Is this helpful? – Sg1 Nov 09 '18 at 10:20
  • data.frame( c("c("London Marylebone Road ", "Swansea Morriston", "Belfast Centre")", "c(5.25, 2.99, 1.06)", "c(35.7, 13, 3.63)") – Sg1 Nov 09 '18 at 10:22

1 Answers1

0

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))
Santiago I. Hurtado
  • 1,113
  • 1
  • 10
  • 23
  • Thanks Santiago. So I have to list each site twice, once for each pollutant? I have 25 sites! This really illustrates my point: I've imported my data as a csv, but it still seems to take a lot of code to use it for a simple bar plot. – Sg1 Nov 08 '18 at 12:13
  • GGPLOT does make really enhance plots but in my experience required more code and lot of experience to do it fast and easy. I will edit the post to make and easier way to reorganize your data. – Santiago I. Hurtado Nov 08 '18 at 14:51
  • Hi Santiago, I have tried your updated code and it has returned this error: "Error in as.vector(as.numeric(Sb_Ba[, 2:ncol(Sb_Ba)])) : (list) object cannot be coerced to type 'double' " (Sb_Ba is my data frame). I have tried googling this error - is it because my site names are not numeric? – Sg1 Nov 09 '18 at 09:42
  • Just to clarify, my data frame is 3 columns, 'Site' (a list of names), 'Sb' and 'Ba' (both are lists of measured concentrations - numeric values) – Sg1 Nov 09 '18 at 09:45
  • data.frame( c("c("London Marylebone Road ", "Swansea Morriston", "Belfast Centre")", "c(5.25, 2.99, 1.06)", "c(35.7, 13, 3.63)") – Sg1 Nov 09 '18 at 10:23
  • tibble::tribble( ~Site, ~Sb, ~Ba, "London Marylebone Road ", 5.25, 35.7, "Swansea Morriston", 2.99, 13, "Belfast Centre", 1.06, 3.63,) – Sg1 Nov 09 '18 at 14:20
  • sorry, the as.numeric should not be there – Santiago I. Hurtado Nov 09 '18 at 17:39
  • It's still not quite working, but don't worry a colleague has helped me do it the long way (listing everything in the code rather than referring to the imported csv. Thanks for your efforts! – Sg1 Nov 12 '18 at 11:56