在 ggplot2 3.1.1 中工作的注释在 3.2.1 中失败。怎么修?
Annotations that work in ggplot2 3.1.1 fail in 3.2.1. How to fix?
我在 ggplot2 3.1.1 中的多面直方图中进行自定义注释的代码在另一台计算机上失败 运行 ggplot2 3.2.1,抛出此错误:
错误:美学必须是长度1或与数据相同(9):label
如何让我的注释出现在 ggplot2 3.2.1 中制作的刻面中?
下面是使用 mtcars 的最小示例。
谢谢!!
library(ggplot2, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
lb <- mtcars %>%
group_by(cyl) %>%
summarize(n=n(), avg_gear=round(mean(gear),1))
lb$label <- paste0("n=",lb$n, " avg_gear=",lb$avg_gear)
print(lb)
ggplot(data=mtcars, aes(x=gear)) +
geom_histogram(binwidth=1) +
facet_grid(. ~ cyl) +
annotate("text", x=4, y=13, label=lb$label) +
ggtitle("histograms of gear, faceted by cyl")
您可以使用另一层:
ggplot(data=mtcars, aes(x=gear)) +
geom_histogram(binwidth=1) +
facet_grid(. ~ cyl) +
geom_text(data = lb, aes(x = 4, y = 13, label = label)) + # in place of annotate
ggtitle("histograms of gear, faceted by cyl")
我在 ggplot2 3.1.1 中的多面直方图中进行自定义注释的代码在另一台计算机上失败 运行 ggplot2 3.2.1,抛出此错误: 错误:美学必须是长度1或与数据相同(9):label
如何让我的注释出现在 ggplot2 3.2.1 中制作的刻面中?
下面是使用 mtcars 的最小示例。
谢谢!!
library(ggplot2, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
lb <- mtcars %>%
group_by(cyl) %>%
summarize(n=n(), avg_gear=round(mean(gear),1))
lb$label <- paste0("n=",lb$n, " avg_gear=",lb$avg_gear)
print(lb)
ggplot(data=mtcars, aes(x=gear)) +
geom_histogram(binwidth=1) +
facet_grid(. ~ cyl) +
annotate("text", x=4, y=13, label=lb$label) +
ggtitle("histograms of gear, faceted by cyl")
您可以使用另一层:
ggplot(data=mtcars, aes(x=gear)) +
geom_histogram(binwidth=1) +
facet_grid(. ~ cyl) +
geom_text(data = lb, aes(x = 4, y = 13, label = label)) + # in place of annotate
ggtitle("histograms of gear, faceted by cyl")