ggplot2中的圆角?

Jon*_*mon 4 themes r ggplot2

感觉它必须是可能的,但我无法弄清楚我是否可以相对简单地做到theme()或者必须深入挖掘.

我想围绕我的阴谋区域的角落ggplot2.我该怎么做呢?

问题的灵感来自:https://twitter.com/Hoog10HK/status/951305194809143296

Cla*_*lke 12

您可以用圆角矩形替换背景grob:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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


teu*_*and 8

感谢 ggplot2 v3.0.0 中的主题元素子类化,我能够在 github 包中使用elementalist::element_rect_round(). (免责声明:我写了 github 包)

library(ggplot2)
library(elementalist) # devtools::install_github("teunbrand/elementalist")

ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  facet_grid(~ cyl) +
  theme(
    legend.background = element_rect_round(radius = unit(0.2, "snpc")),
    legend.key = element_rect_round(radius = unit(0.4, "snpc")),
    panel.background = element_rect_round(radius = unit(1, "cm")),
    strip.background = element_rect_round(radius = unit(8, "pt")),
    plot.background  = element_rect_round(fill = "#C4E7FF")
  )
Run Code Online (Sandbox Code Playgroud)

reprex 包(v1.0.0)创建于 2021-07-05