从不同数据帧面添加geom_text

问题描述 投票:0回答:1

我使用ggplot2在某个位置绘制了多年月降水总量:

library(ggplot2)
df.mon <- data.frame(id=rep("Station 1", 192),
                     month=rep(seq(1:12), 16),
                     year=rep(1999:2014, each=12),
                     monprec=runif(n=192, min=0, max=400))

ggplot(df.mon, aes(x=month, y=monprec)) +
  geom_bar(stat="identity") +
  theme_bw(base_size=18) +
  facet_wrap(~year, ncol=3)

enter image description here

在同一图中,我要与年度总降水量,这是在第二个数据帧添加注释:

df.year <- data.frame(id=rep("Station 1", 16),
                      year=1999:2014,
                      totprec=runif(n=16, min=200, max=1000))

我的第一种方法是使用geom_text(),但df.year数据帧不具有可以用作monthy参数一个aes()列。

任何想法,以帮助我实现我的目标?

r ggplot2 facet-wrap
1个回答
3
投票

我可能已经错过了点,但这个怎么样?

# Data frames
df.mon <- data.frame(id=rep("Station 1", 192),
                     month=rep(seq(1:12), 16),
                     year=rep(1999:2014, each=12),
                     monprec=runif(n=192, min=0, max=400))

df.year <- data.frame(id=rep("Station 1", 16),
                      year=1999:2014,
                      totprec=runif(n=16, min=200, max=1000))

# Plotting
library(ggplot2)

ggplot(df.mon, aes(x=month, y=monprec)) +
  geom_bar(stat="identity") +
  theme_bw(base_size=18) +
  facet_wrap(~year, ncol=3) +
  ylim(c(0, 500)) +
  geom_text(data = df.year, aes(x = 6.25, y = 450, label = round(totprec)))

enter image description here

在这里,我只指定xy坐标在aesgeom_text年降水量注解。

© www.soinside.com 2019 - 2024. All rights reserved.