注释标签在GGPLOT2中剪辑

发布于 2025-02-12 06:03:09 字数 610 浏览 2 评论 0原文

我正在努力避免被剪切的底部注释。被砍掉的是“ P”上的下降。我在vjust上使用了“内向”选项。

df <- data.frame(x=c(as.Date("2020-01-01"),as.Date("2022-01-01"))
                     ,y=c(0,1))
df
ggplot(df) +
  geom_point(mapping=aes(x=x,y=y)) +
  annotate("text",x=mean(df$x),y=-Inf,label="Clipped",hjust=0.5,vjust="inward",size=12,colour="red") +
  annotate("text",x=mean(df$x),y=Inf,label="Not Clipped",hjust=0.5,vjust="inward",size=12,colour="blue")

I'm trying to avoid the bottom annotation being clipped. It's the descender on the "p" that gets chopped off. I've used the "inward" option on vjust.

df <- data.frame(x=c(as.Date("2020-01-01"),as.Date("2022-01-01"))
                     ,y=c(0,1))
df
ggplot(df) +
  geom_point(mapping=aes(x=x,y=y)) +
  annotate("text",x=mean(df$x),y=-Inf,label="Clipped",hjust=0.5,vjust="inward",size=12,colour="red") +
  annotate("text",x=mean(df$x),y=Inf,label="Not Clipped",hjust=0.5,vjust="inward",size=12,colour="blue")

clipped text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2025-02-19 06:03:18

如果您不希望将其夹在相同的位置上,则可以使用coord_cartesian(clip =“ off”)

df <- data.frame(x=c(as.Date("2020-01-01"),as.Date("2022-01-01"))
                 ,y=c(0,1))
library(ggplot2)
ggplot(df) +
  geom_point(mapping=aes(x=x,y=y)) +
  annotate("text",x=mean(df$x),y=-Inf,label="Clipped",hjust=0.5,vjust="inward",size=12,colour="red") +
  annotate("text",x=mean(df$x),y=Inf,label="Not Clipped",hjust=0.5,vjust="inward",size=12,colour="blue") +
  coord_cartesian(clip = 'off')

”“

在2022-07-02创建的 reprex软件包(v2.0.1)

If you don't want it to be clipped on the same position, you can use coord_cartesian(clip = "off"):

df <- data.frame(x=c(as.Date("2020-01-01"),as.Date("2022-01-01"))
                 ,y=c(0,1))
library(ggplot2)
ggplot(df) +
  geom_point(mapping=aes(x=x,y=y)) +
  annotate("text",x=mean(df$x),y=-Inf,label="Clipped",hjust=0.5,vjust="inward",size=12,colour="red") +
  annotate("text",x=mean(df$x),y=Inf,label="Not Clipped",hjust=0.5,vjust="inward",size=12,colour="blue") +
  coord_cartesian(clip = 'off')

Created on 2022-07-02 by the reprex package (v2.0.1)

只有影子陪我不离不弃 2025-02-19 06:03:17

一种可能的方法是使用minmax y值:

library(tidyverse)

df <- data.frame(
  x = c(as.Date("2020-01-01"), as.Date("2022-01-01")),
  y = c(0, 1)
)

ggplot(df) +
  geom_point(aes(x, y)) +
  annotate("text", x = mean(df$x), y = min(df$y), label = "Clipped", hjust = 0.5, vjust = "inward", size = 12, colour = "red") +
  annotate("text", x = mean(df$x), y = max(df$y), label = "Not Clipped", hjust = 0.5, vjust = "inward", size = 12, colour = "blue")

“

在2022-07-02创建的 reprex软件包(v2.0.1)

A possible approach would be to use the min and max y values:

library(tidyverse)

df <- data.frame(
  x = c(as.Date("2020-01-01"), as.Date("2022-01-01")),
  y = c(0, 1)
)

ggplot(df) +
  geom_point(aes(x, y)) +
  annotate("text", x = mean(df$x), y = min(df$y), label = "Clipped", hjust = 0.5, vjust = "inward", size = 12, colour = "red") +
  annotate("text", x = mean(df$x), y = max(df$y), label = "Not Clipped", hjust = 0.5, vjust = "inward", size = 12, colour = "blue")

Created on 2022-07-02 by the reprex package (v2.0.1)

泪痕残 2025-02-19 06:03:15

有趣的。看来此问题与选择的基本线路对齐文本标签有关。切换到geom_label时,可以清楚地看到这一点,在其中我们看到,对于剪辑标签,为对齐方式选择的基本线并不是“ p”的末端。因此,“ p” s被删除:

ggplot(df) +
  geom_point(mapping = aes(x = x, y = y)) +
  annotate("label", x = mean(df$x), y = -Inf, label = "Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "red", label.padding = unit(0, "lines")) +
  annotate("label", x = mean(df$x), y = Inf, label = "Not Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "blue", label.padding = unit(0, "lines"))

”在此处输入图像描述

一个可能的修复是切换到ggtext :: geomrichtext

library(ggplot2)
library(ggtext)

ggplot(df) +
  geom_point(mapping = aes(x = x, y = y)) +
  annotate(ggtext::GeomRichtext, x = mean(df$x), y = -Inf, label = "Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "red", 
           label.size = 0, fill = NA, label.padding = unit(0, "lines")) +
  annotate(ggtext::GeomRichtext, x = mean(df$x), y = Inf, label = "Not Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "blue", 
           label.size = 0, fill = NA, label.padding = unit(0, "lines"))

“”

Interesting. Looks like this issue is related to what is chosen as the base line to align the text labels. This could be seen clearly when switching to geom_label where we see that for the clipped label the base line chosen for the alignment is not the end of the "p". Hence the "p"s get clipped off:

ggplot(df) +
  geom_point(mapping = aes(x = x, y = y)) +
  annotate("label", x = mean(df$x), y = -Inf, label = "Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "red", label.padding = unit(0, "lines")) +
  annotate("label", x = mean(df$x), y = Inf, label = "Not Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "blue", label.padding = unit(0, "lines"))

enter image description here

One possible fix would be to switch to ggtext::GeomRichtext:

library(ggplot2)
library(ggtext)

ggplot(df) +
  geom_point(mapping = aes(x = x, y = y)) +
  annotate(ggtext::GeomRichtext, x = mean(df$x), y = -Inf, label = "Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "red", 
           label.size = 0, fill = NA, label.padding = unit(0, "lines")) +
  annotate(ggtext::GeomRichtext, x = mean(df$x), y = Inf, label = "Not Clipped", 
           hjust = 0.5, vjust = "inward", size = 12, colour = "blue", 
           label.size = 0, fill = NA, label.padding = unit(0, "lines"))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文