如何在ggplot中的绘图框外注释绘图?

huangapple go评论59阅读模式
英文:

How to annotate a plot outside the plot frame in ggplot?

问题

I created a small data frame

x <- 1:10
y1 <- x
y2 <- x^2
y3 <- x + 1

df <- data.frame(x, y1, y2, y3)

We want to plot y against x and remove the default space on both axis, so I did it like this

ggplot() +
  geom_line(data = df, aes(x, y1), linetype = "solid", size = 1, show.legend = TRUE) +
  geom_line(data = df, aes(x, y2), linetype = "dashed", size = 1, show.legend = TRUE) +
  geom_line(data = df, aes(x, y3), linetype = "dotted", size = 1, show.legend = TRUE) +
  theme(panel.border = element_rect(colour = "black", fill=NA),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  scale_x_continuous(expand = c(0, 0), labels = label_number(accuracy = 1)) +
  scale_y_continuous(expand = c(0, 0)) +
  xlab("x-axis") +
  ylab("y-axis")

and the output is

如何在ggplot中的绘图框外注释绘图?

How can I label each function at the end of each curve and outside the plot frame? So for example, I would like to show y2 at around x=10.5 and y=100.

I tried

annotate("text", x = max(df$x) + 0.5, y = max(df$y2), label = expression(y2), parse = TRUE)

but this doesn't show the text I think because of expand = c(0, 0).

Is there a way to keep expand = c(0, 0) and annotate the plot outside the frame?

英文:

I created a small data frame

x &lt;- 1:10
y1 &lt;- x
y2 &lt;- x^2
y3 &lt;- x + 1

df &lt;- data.frame(x, y1, y2, y3)

We want to plot y against x and remove the default space on both axis, so I did it like this

ggplot() +
  geom_line(data = df, aes(x, y1), linetype = &quot;solid&quot;, size = 1, show.legend = TRUE) +
  geom_line(data = df, aes(x, y2), linetype = &quot;dashed&quot;, size = 1, show.legend = TRUE) +
  geom_line(data = df, aes(x, y3), linetype = &quot;dotted&quot;, size = 1, show.legend = TRUE) +
  theme(panel.border = element_rect(colour = &quot;black&quot;, fill=NA),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  scale_x_continuous(expand = c(0, 0), labels = label_number(accuracy = 1)) +
  scale_y_continuous(expand = c(0, 0)) +
  xlab(&quot;x-axis&quot;) +
  ylab(&quot;y-axis&quot;)

and the output is

如何在ggplot中的绘图框外注释绘图?

How can I label each function at the end of each curve and outside the plot frame? So for example, I would like to show y2 at around x=10.5 and y=100.

I tried

annotate(&quot;text&quot;, x = max(df$x) + 0.5, y = max(df$y2), label = expression(y2), parse = TRUE)

but this doesn't show the text I think because of expand = c(0, 0).

Is there a way to keep expand = c(0, 0) and annotate the plot outside the frame?

答案1

得分: 3

基于@YacineHajji的评论,建议使用coord_cartesian(clip = "off")。您还需要:

  • 根据xy的范围手动设置限制,否则限制将扩展以包括注释。
  • 手动调整重叠的注释。
  • 调整边距以为注释腾出空间。
英文:

Based on @YacineHajji’s comment suggesting coord_cartesian(clip = &quot;off&quot;). You also need to:

  • manually set the limits based on the ranges of x and y; otherwise, the limits will expand to include the annotations,
  • manually nudge overlapping annotations, and
  • adjust the margins to make room for the annotations.
library(ggplot2)
library(scales)

ggplot() +
  geom_line(data = df, aes(x, y1), linetype = &quot;solid&quot;, size = 1, show.legend = TRUE) +
  geom_line(data = df, aes(x, y2), linetype = &quot;dashed&quot;, size = 1, show.legend = TRUE) +
  geom_line(data = df, aes(x, y3), linetype = &quot;dotted&quot;, size = 1, show.legend = TRUE) +
  annotate(&quot;text&quot;, x = max(df$x) + 0.5, y = max(df$y1) - 2.5, label = expression(y1), parse = TRUE) +
  annotate(&quot;text&quot;, x = max(df$x) + 0.5, y = max(df$y2), label = expression(y2), parse = TRUE) +
  annotate(&quot;text&quot;, x = max(df$x) + 0.5, y = max(df$y3) + 2.5, label = expression(y3), parse = TRUE) +
  theme(panel.border = element_rect(colour = &quot;black&quot;, fill=NA),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.margin = margin(20, 40, 10, 10)) +
  scale_x_continuous(labels = label_number(accuracy = 1)) +
  coord_cartesian(xlim = range(df$x), 
                  ylim = c(min(c(df$y1, df$y2, df$y3)), max(c(df$y1, df$y2, df$y3))),
				  expand=FALSE, 
				  clip=&quot;off&quot;) +
  xlab(&quot;x-axis&quot;) +
  ylab(&quot;y-axis&quot;)

如何在ggplot中的绘图框外注释绘图?

huangapple
  • 本文由 发表于 2023年4月6日 20:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定