S.Guiry
S.Guiry

Reputation: 15

precipitation histogram and line graph

I'm trying to make a monthly precipitation histogram in r using ggplot2 this is the data ideally columns 1 would be the x axis, column 2 would be histogram and column 3 would be line graph

Month    MonthlyPrecipitation    30YearNormalPrecipitation 
January                 49.75                         67.1
February                 8.75                         53.6
March                   27                            64.2
April                   55.5                          77.7
May                     62.25                         89.2
June                   171.75                         84.7
July                    50.75                         83.6
August                  37.25                         77.6
September               75.75                         92.6
October                 99.25                         86.3
November                37.25                         90.7
December                43.25                         78.9

Upvotes: 1

Views: 636

Answers (1)

Patrick Williams
Patrick Williams

Reputation: 704

I've created fake data to recreate your example. Month a-l represents January through December. Here you first create a bar plot (I'm assuming that's what you meant by histogram. A true histogram of your data would be flat, given each item in your first variable is unique), then add the line graph. You have to include group = 1 at the end or it will return an error. And in case it wasn't clear, MP is my recreation of monthly precipitation, and NP is 30YearNormalPrecipitation.

set.seed(100)

Month <- c(letters[1:12])
MP <- rnorm(12, 50, 5)
NP <- rnorm(12, 80, 5)

df <- data.frame(Month, MP, NP)

ggplot(df, aes(x=Month, y = MP)) + geom_bar(stat = 'identity', alpha = 0.75) + 
  geom_line(aes(y = NP), colour="blue", group = 1)

Upvotes: 3

Related Questions