我的目标是在 ggplot 的边缘创建一个比例标签,并且这个比例标签在图形中的位置应该是不变的(这样在类似的图形中循环不会显示比例标签移动)。
这项任务比我预期的要困难得多,因为 ggplot:
- 没有刻度标签的概念,
- 不容易在页边空白处添加注释,
- 坚持以数据相关(即非位置不变)的方式定位注释
编辑:为清楚起见,我特别关注标尺标签。有关示例,请参见生成的 ggplot。
这是我的解决方案,尽管是以一种骇人听闻的方式:
library(ggplot2)
library(grid)
library(gridExtra)
library(tidyverse)
library(dplyr)
dat = data.frame("Country" = c(rep("USA",50),
rep("Canada",50),
rep("Japan",50),
rep("Poland",50),
rep("Germany",50)),
"Outcome" = c(rnorm(n = 50, mean = 4.0, sd = 1.0),
rnorm(n = 50, mean = 5.0, sd = 0.5),
rnorm(n = 50, mean = 3.0, sd = 1.5),
rnorm(n = 50, mean = 4.5, sd = 1.0),
rnorm(n = 50, mean = 4.5, sd = 1.5))
)
myfunction = function(data, IV, DV, varlevels){
# bumpsize is the hacky code that gets it to work
# parameters need fine-tuning for perfect consistency but good enough here
bumpsize = .45-(.06*length(varlevels))
# compute mean for plotting
as_tibble(dat) %>%
group_by({{IV}}) %>%
summarise(Outcome = mean({{DV}},na.rm=T)) %>%
# do the plotting
ggplot(mapping = aes({{IV}}, Outcome, fill = {{IV}})) +
geom_col() +
scale_x_discrete(limits = {{varlevels}}) +
scale_y_continuous(breaks = c(1:7), limits = c(1,7), oob = scales::squish) +
theme(legend.position = "none") +
coord_cartesian(clip = "off") +
annotation_custom(
grob = textGrob(label = "Good", gp = gpar(col = "grey50"), rot = 90),
ymin = 6, ymax = 7, xmin = bumpsize, xmax = bumpsize) +
annotation_custom(
grob = textGrob(label = "Bad", gp = gpar(col = "grey50"), rot = 90),
ymin = 1, ymax = 2, xmin = bumpsize, xmax = bumpsize)
}
myfunction(data = dat, IV = Country, DV = Outcome, varlevels = c("USA","Canada","Japan","Poland","Germany"))
ggsave("outcome_by_country.png", dpi = 300, units = "in", height= 5.89, width = 6.69)
我的问题: 有没有一种方法可以实现不变的位置比例标签,而无需求助于当前实现的 hacky 变量级方程?
