情节之外的文字;如何添加段落,选择性加粗,并用注释保存绘图

问题描述 投票:0回答:1

我正在尝试在图例之外编辑图例下方的文本。 我已按照here建议的说明进行操作,并且已经能够将以下代码组合在一起

p <- ggplot(Amh2, aes(x=syllable, y=durZ, color=condition)) + 
  geom_boxplot(lwd=1) + 
  theme_minimal() + 
  theme(text = element_text(size=15, family="Times New Roman")) + 
  theme(axis.text = element_text(size=13)) +
  xlab("syllable") +
  ylab("SDs from by-speaker mean") +
  scale_y_continuous(breaks=scales::pretty_breaks(n=7), limits=c(-3,3)) +
  theme(axis.title.x = element_text(margin=margin(t=5, r=0, b=0, l=0))) +
  theme(axis.title.y = element_text(margin=margin(t=0, r=5, b=0, l=0))) +
  theme(axis.text.x = element_text(color="#000000", size=12, family="Arial")) +
  theme(axis.text.y = element_text(color="#000000", size=12, family="Arial")) +
  theme(legend.position="right",
        legend.justification="center",
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(0,0,0,-5)) +
  scale_color_manual(values=c("#FF0000", "#000000"), labels=c("focus", "nonfocus")) +
  theme(legend.text = element_markdown(size=12, family="Arial")) + 
  theme(legend.title=element_blank()) +
  annotate(geom="text", x=1, y=2.35, label="*", color="black", size=15) +
  annotate(geom="text", x=2, y=2.35, label="*", color="black", size=15) +
  annotate(geom="text", x=3, y=2.35, label="*", color="black", size=15) +
# Here is the relevant portion:
  theme(legend.position="right", plot.margin = unit(c(1,1,3,1),"lines")) +
  annotation_custom(grob = textGrob("A1-CV.CV"), 
                    xmin = 2, xmax = 12.5, ymin = -4.5, ymax = 3.2) +
  annotation_custom(grob = textGrob("B1-CV.CVC"), 
                    xmin = 2, xmax = 12.65, ymin = -4.5, ymax = 2.7) +
  annotation_custom(grob = textGrob("C1-CV.CVG"), 
                    xmin = 2, xmax = 12.65, ymin = -4.5, ymax = 2.2)

gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

这是它生成的图,图例下方添加了“A1,B1,C1”文本。 (我想做的是添加文本以进一步扩展 x 轴标签。)

enter image description here我有三个问题,我似乎无法弄清楚。

  1. 有没有办法在图例下方添加一个文本框,这样我就可以有多行左对齐的文本? 事实上,我通过调整 ymax 和 xmax 精心对齐了三个注释,但这并不理想。 换行文本也可能有效,但最理想的是控制它何时跳到下一行。

  2. 有没有办法添加选择性粗体这些注释? 这篇文章解释了如何用常规的

    annotate
    来做到这一点,但我不知道如何用
    annotation(custom)
    来做到这一点。 例如,我希望第一个标签为“A1-CV.CV”。

  3. 有没有办法使用

    ggsave
    之类的东西来保存整个情节? 我想在不损失质量的情况下保存绘图,但似乎
    ggsave
    没有选择添加的自定义注释 - 它只是保存基本绘图。

r ggplot2 legend annotate
1个回答
0
投票

这里有一个选项可以更轻松地实现您想要的结果,它使用假的

size
图例来添加文本注释,不需要任何摆弄来设置位置和对齐方式,可以轻松使用
ggtext
来设置文本注释的样式文本注释(即使部分变为粗体)并最终摆脱不必要的
grid
黑客(您可以使用
coord_cartesian(clip = "off")
设置剪辑关闭。

使用一些虚假的示例数据:

library(ggplot2)
library(ggtext)

set.seed(123)

Amh2 <- expand.grid(
  syllable = c(paste0(LETTERS[1:3], 1), paste0(LETTERS[1:3], 2)),
  condition = c("focus", "nonfocus")
) |>
  cbind(durZ = rnorm(100 * 6 * 2))


p <- ggplot(Amh2, aes(x = syllable, y = durZ, color = condition)) +
  geom_boxplot(lwd = 1) +
  scale_y_continuous(
    breaks = scales::pretty_breaks(n = 7), limits = c(-3, 3)
  ) +
  scale_color_manual(
    values = c("#FF0000", "#000000"),
    labels = c("focus", "nonfocus")
  ) +
  theme_minimal() +
  theme(
    text = element_text(size = 15, family = "Times New Roman"),
    axis.text = element_text(size = 13),
    axis.title.x = element_text(margin = margin(t = 5, r = 0, b = 0, l = 0)),
    axis.title.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 0)),
    axis.text.x = element_text(color = "#000000", size = 12, family = "Arial"),
    axis.text.y = element_text(color = "#000000", size = 12, family = "Arial"),
    legend.position = "right",
    legend.justification = "center",
    legend.margin = margin(0, 0, 0, 0),
    legend.box.margin = margin(0, 0, 0, -5),
    legend.text = element_markdown(size = 12, family = "Arial"),
    legend.title = element_blank(),
    plot.margin = unit(c(1, 1, 3, 1), "lines")
  ) +
  xlab("syllable") +
  ylab("SDs from by-speaker mean") +
  annotate(geom = "text", x = 1:3, y = 2.35, label = "*", color = "black", size = 15) +
  # Add annotations using a fake size legend and a geom_point
  geom_point(
    data = data.frame(
      label = c("A1-**CV**.CV", "B1-*CV*.CVC", "C1-**CV**.CVG")
    ),
    aes(size = label),
    x = NA, y = NA,
    key_glyph = "blank",
    inherit.aes = FALSE,
    na.rm = TRUE
  ) +
  guides(
    # Use order to set the order
    color = guide_legend(order = 1),
    size = guide_legend(
      order = 2,
      theme = theme(
        legend.text.position = "left",
        legend.text = element_markdown(hjust = 0)
      )
    )
  )

p
#> Warning: Using size for a discrete variable is not advised.
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_boxplot()`).

© www.soinside.com 2019 - 2024. All rights reserved.