ggplot2 – 将数据环绕图例放置在自定义位置

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

ggplot2 - wrap data around legend in custom position

问题

当在ggplot中以自定义位置放置图例(使用legend.position = c(x, y))时,是否可以格式化图例,使其不重叠数据,而是数据点围绕它包裹?

在这个例子中,是否可以让ggplot在图中插入额外的空间,以使数据点不被图例遮挡(而不改变legend.position)?

谢谢!

英文:

When placing a legend in a custom position (using legend.position = c(x, y)) in a ggplot, is it possible to format the legend so that it does not overlay the data, and instead, the datapoints wrap around it?

In this example, would it be possible to, say, have ggplot insert extra space in the plot, so that datapoints are not obscured by the legend (without changing the legend.position)?

Thanks!

library(tidyverse)

data(mtcars)

ggplot(data = mtcars, aes(x = wt, y = hp))+
  geom_point(aes(color = mpg))+
  theme(legend.direction = "horizontal",
        legend.position = c(0.5, 0.9))

答案1

得分: 0

一个不太优雅的解决方法是添加plot.title(element_text(margin = margin(a, b, c, d))),其中a、b、c和d分别表示顶部、右侧、底部和左侧的填充值,然后调整c的值,直到有足够的空间。如果您想出更好的解决方法,请告诉我!

英文:

An inelegant solution is to add plot.title(element_text(margin = margin(a, b, c, d))) where a, b, c and d are padding values for top, right, bottom, left, respectively, and adjust the c value until there is sufficient space. Let me know if you come up with a better solution!

huangapple
  • 本文由 发表于 2023年2月8日 08:21:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380297.html
匿名

发表评论

匿名网友

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

确定