【问题标题】:Adding geom_text from different data frame to facets将来自不同数据框的 geom_text 添加到构面
【发布时间】:2019-02-04 00:21:18
【问题描述】:

我正在使用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)

在同一张图上,我想在第二个数据框中添加带有降水总量的注释:

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

我的第一种方法是使用geom_text(),但df.year 数据框没有可用作aes() 中的y 参数的month 列。

有什么想法可以帮助我实现目标吗?

【问题讨论】:

  • 你能合并这两个数据框,让所有信息合二为一吗?然后添加值会更容易
  • 这是一个选择,但我什至不需要这样做。请检查接受的答案。

标签: r ggplot2 facet-wrap


【解决方案1】:

我可能错过了重点,但是这个怎么样?

# 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)))

这里,我只是在geom_textaes中指定了年降水量注释的xy坐标。

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 2017-04-26
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多