Search code examples
rggplot2r-grid

why does it say unused argument for radius = unit(0.1, "snpc")


I am trying to create a graph with rounded corners, but when I put in the parameter radius =unit(0.1, "snpc") or radius = 0.1, both return 'unused argument (radius = unit(0.1, "snpc")) or (radius = 0.1)' respectively. How can I create rounded corners without this error?

library(tidyverse)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(grid)

read.csv("/Users/benjamincohn/Desktop/R studio/vaccination_percentages.csv") -> un

un %>%
  arrange(Uninsured) %>%
  ggplot(aes(y = Life.Expectancy.at.Birth..years., x = Uninsured, color = Location)) +
  geom_point() +
  scale_x_continuous(breaks = seq(2, 18, by = 1)) +
  scale_y_continuous(breaks = seq(70, 81, by = 1)) +
  geom_smooth(method = "lm", color = 'black') +
  ggtitle("Uninsured (%) vs. Life Expectancy at Birth (years) by State (2023)") +
  labs(y = "Life Expectancy at Birth (years)", x = "Uninsured (%)") +
  theme(axis.line = element_line(colour = "black", size = 1, linetype = "solid"),
        plot.background = element_rect(fill = "lightblue"),
        legend.key = element_rect(fill = "deepskyblue3"),
        legend.background = element_rect(fill = "deepskyblue3", radius = unit(0.1, "snpc")))

Solution

  • I don't believe "base" ggplot2 has rounded corner rectangles primitives available for themes (https://ggplot2.tidyverse.org/reference/element.html), but some related packages like ggfun do:

    ggplot(mtcars, aes(wt, mpg, color = gear)) +
      geom_point() +
      theme(legend.background = ggfun::element_roundrect(fill = "gray80"))
    

    enter image description here