[英]How to add to the face labels in ggplot2
我想知道如何在下面的 plot 中的構面標簽旁邊顯示paste("n =", ns)
的結果?
library(tidyverse)
dd <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/3.csv')
ns <- dd %>% group_by(schoolid) %>%
summarise(n = n()) %>% pull(n)
ggplot(data = dd, aes(x = year, y = math, group = factor(childid)))+
geom_point() + geom_smooth(method = "lm", se = FALSE, size = .1, formula = y~offset(.5*x)) +
facet_wrap(~factor(schoolid))
# Tried the following without success:
# +
# geom_label(aes(x=4, y=5), label= paste("n =", ns), show.legend = FALSE)
我們可以使用命名向量並傳遞給labeller
ns1 <- setNames(paste0(unique(dd$schoolid), ' (', ns, ')'),
unique(dd$schoolid) )
ggplot(data = dd, aes(x = year, y = math,
group = factor(childid)))+
geom_point() +
geom_smooth(method = "lm", se = FALSE, size = .1,
formula = y~offset(.5*x)) +
facet_wrap(~schoolid, labeller = labeller(schoolid = ns1)) +
guides(fill = FALSE)
-輸出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.