0

I hope this title wasn't too confusing. I have data from 10 dates, and each data point is either labelled as "Irrigation" or "Precipitation". I would like to make one bar graph that has a bar for each value in chronological order with the date labelled under the bar, but would also like the bars to be color coded (for example, all bars from "Irrigation" would be red, while those from "Precipitation" would be blue.)

I am fairly new to R, is this possible? If so, any help would be appreciated.

Here is my data:

dput(head(data)) 
     structure(list(Date = structure(4:9, .Label = c("7/11/2019", 
     "7/13/2019", "7/15/2019", "7/2/2019", "7/3/2019", "7/4/2019", 
     "7/5/2019", "7/6/2019", "7/7/2019", "7/9/2019"), class = "factor"), 
         Inches = c(1.6, 0.02, 0.35, 0.64, 0.07, 0.35), Type = structure(c(2L, 
    2L, 1L, 2L, 2L, 1L), .Label = c("Irrigation", "Precipitation"
    ), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
Matt
  • 2,947
  • 1
  • 9
  • 21
Tom
  • 377
  • 3
  • 13

1 Answers1

0

You can use dplyr and ggplot2 to make a bar graph. The stat='identity' call means each bar height uses the corresponding value in the data, and position='dodge' places the bars side by side, rather than stacked.

df <- data.frame(date=c(paste0('2019-0',1:9),'2019-10'),
                 precipitation=runif(10),irrigation=runif(10))
df

df %>% gather(k,v,-date) %>% 
  ggplot(aes(date,v,fill=k)) + geom_bar(stat='identity',position = 'dodge') +
  theme(axis.text.x = element_text(angle = 45,hjust=1))

bar graph

EDIT

Here is the bar graph using the data you provided from dput. I didn't realize your data were already grouped in one column.

df <- structure(list(Date = structure(4:9, .Label = c("7/11/2019", 
                                                "7/13/2019", "7/15/2019", "7/2/2019", "7/3/2019", "7/4/2019", 
                                                "7/5/2019", "7/6/2019", "7/7/2019", "7/9/2019"), class = "factor"), 
               Inches = c(1.6, 0.02, 0.35, 0.64, 0.07, 0.35), Type = structure(c(2L, 
                                                                                 2L, 1L, 2L, 2L, 1L), .Label = c("Irrigation", "Precipitation"
                                                                                 ), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
df %>% ggplot(aes(Date,Inches,fill=Type)) + geom_bar(stat='identity',position = 'dodge') +
  theme(axis.text.x = element_text(angle = 45,hjust=1))

bar graph 2

kstew
  • 1,104
  • 6
  • 21
  • Thank you for the response! The graph that you made looks perfect. Do you know how I can apply this to my data? When I change df to data in that last section of code, I get the error "Error in -x : invalid argument to unary operator" – Tom Jul 16 '19 at 16:18
  • Actually, I think I didn't look close enough at the graph. There is not one of each factor for each data point. There are 10 individual data points, 6 of which are 'Irrigation' and 4 of which of 'Precipitation'. – Tom Jul 16 '19 at 16:23
  • Hi Trev, here is the updated figure using the data from your `dput` call. – kstew Jul 16 '19 at 16:24
  • Thank you! This is wonderful. It works with my full dataset, however, I've noticed that it reads dates in the teens (ie 7/11/2019) as earlier than dates before then (7/7/2019). I imagine that that has something to do with the first number in 11 being a 1, which is lower than a 7. Do you know how I can resolve this issue? – Tom Jul 16 '19 at 16:30
  • I would suggest converting your dates from character strings to date variables. You can use `as.Date(data$date, format='%m/%d/%Y')`, the `format` option will specify how the dates are ordered. Plotting with `ggplot` will then preserve the chronological order of the dates. – kstew Jul 16 '19 at 17:21
  • Thanks, that solves my issue. If possible, could you look into one more issue? I noticed that in your graph, each bar has a date under it, whereas in my graph, there is only a date for the beginning, middle, and end of the x axis. That is strange to me, do you have any advice for that? – Tom Jul 16 '19 at 17:51
  • You can add `+ scale_x_date(date_labels="%d-%m",date_breaks="1 day")` to your `ggplot` command, which will add each day as a tick mark in a DD-MM format. You can change the number of tick marks by adjusting `date_breaks`. Thanks to this [post](https://stackoverflow.com/questions/41855673/r-ggplot-display-all-dates-on-x-axis). – kstew Jul 16 '19 at 18:10
  • That worked perfectly, thanks again! You've been a huge help – Tom Jul 16 '19 at 18:48