0

我正在尝试用一些文本注释我的 ggplot 图,但我很难找到最佳解决方案。我想在图的右侧创建一个边距,以便我可以写课程 A、课程 B 和课程 C。现在我没有足够的空间。我尝试使用 plot.margin = unit(c(1,4,1 ,1), "cm") 但这似乎并没有给我可以用来注释情节的空间。ggplot 中有我可以使用的功能吗?

在此处输入图像描述

trainingplot <- ggplot(aggreg.data, aes(x = trainingblock, y = performance)) +
  scale_y_continuous(name = "Time substracted from straight gliding time (sec.)", breaks = seq(-2, 6, 1)) +
  scale_x_discrete() +
  theme_pubr()+
  theme(legend.position="none",
       axis.title.x=element_blank(),
       plot.margin = unit(c(1,4,1 ,1), "cm")) +
  geom_hline(aes(yintercept = 0), linetype = "dashed", size=0.2) +
  annotate("text", x = 5.4, y = -0.4, label = "Course A", size=4) + 
  annotate("text", x = 5.4, y = 0.5, label = "Course B", size=4) +
  annotate("text", x = 5.4, y = 2, label = "Course C", size=4) 
  

4

1 回答 1

1

对于连续比例,您可以使用辅助轴来注释边距中的特定点。以下标准数据集的示例:

library(ggplot2)

ggplot(economics, aes(date)) +
  geom_line(aes(y = unemploy)) +
  scale_y_continuous(
    sec.axis = dup_axis(
      breaks = tail(economics$unemploy, 1),
      labels = "Unemployment", name = NULL
    )
  ) +
  theme(axis.ticks.y.right = element_blank())

reprex 包于 2021-09-30 创建(v2.0.1)

于 2021-09-30T16:53:01.830 回答