降水直方图和线图

时间:2017-03-09 22:58:40

标签: r ggplot2

我正在尝试使用ggplot2在r中制作每月降水直方图这是理想情况下数据列1将是x轴,第2列是直方图,第3列是线图

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

1 个答案:

答案 0 :(得分:2)

我创建了假数据来重新创建你的例子。月a-l代表1月到12月。在这里,您首先创建一个条形图(我假设这是您的直方图的意思。如果第一个变量中的每个项目都是唯一的,那么数据的真实直方图将是平坦的),然后添加折线图。您必须在结尾包含group = 1,否则它将返回错误。如果不清楚,MP是我每月降水的重建,NP是30年正常的沉淀。

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)