ggplot2中的圆角? [英] Rounded corners in ggplot2?

查看:258
本文介绍了ggplot2中的圆角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是必须的,但是我无法弄清楚我是否可以相对简单地在theme()中进行操作或必须更深入地研究.

This feels like it has to be possible, but I cannot figure out if I can do it relatively simply in a theme() or have to dig deeper.

我想在我的ggplot2地块的地块上拐角.我将如何去做?

I would like to round the corners on the plot areas of my ggplot2 plots. How would I go about doing that?

问题的灵感源于: https://twitter.com/Hoog10HK/status/951305194809143296

推荐答案

您可以将背景格罗布替换为圆形矩形:

You can replace the background grob with a round rectangle:

library(ggplot2)
library(grid)

# make a plot with blue background
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
  theme(plot.background = element_rect(fill = "#C4E7FF"),
        panel.background = element_blank(),
        plot.margin = margin(20, 20, 20, 20))

# switch out background grob
g <- ggplotGrob(p)
bg <- g$grobs[[1]]
round_bg <- roundrectGrob(x=bg$x, y=bg$y, width=bg$width, height=bg$height,
                          r=unit(0.1, "snpc"),
                          just=bg$just, name=bg$name, gp=bg$gp, vp=bg$vp)
g$grobs[[1]] <- round_bg

# draw both plots side by side
cowplot::plot_grid(p, g, labels = c("rectangular", "rounded"),
                   scale = 0.85, hjust = 0.5, label_x = 0.5)

如果您想对图的其他方面进行四舍五入,则可以将相同的技巧应用于面板背景,图例背景等.

The same trick could be applied to the panel background, legend background, etc., if you wanted other aspects of the plot to be rounded.

这篇关于ggplot2中的圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆