Create Gauge via ggplot2

I want to create the same plot, and make sure that ggplot using the full layout of the chart when i save the plot (not half)

image

library(tidyverse)

df <- tibble(x = 1, percent = 54.2) %>% 
  mutate(title = paste0(percent, "%"))

ggplot(df, aes(xmin = 1, xmax = 1.5, ymin = 0, ymax = percent)) +
  geom_rect(aes(xmin = 1, xmax = 1.5, ymin = 0, ymax = 100), fill = "#ece8bd") +
  geom_rect(fill = "#47d22d") + 
  xlim(c(0, 1.5)) + 
  ylim(c(0,200)) +
  geom_text(aes(x = 0, y = 0, label = title), 
            colour = "black", size = 10) +
  coord_polar(theta = "y", start = -pi/2) + 
  theme_void() + 
theme(plot.margin = unit(rep(0, 4), "cm"))
ggsave("x.png", width = 4, height = 4, bg = "transparent")

I tried this code but i can not crop the graph

df <- tibble(x = 1, percent = 54.2) %>% 
  mutate(title = paste0(percent, "%"))

ggplot(df, aes(xmin = 1, xmax = 1.5, ymin = 0, ymax = percent)) +
  geom_rect(aes(xmin = 1, xmax = 1.5, ymin = 0, ymax = 100), fill = "#ece8bd") +
  geom_rect(fill = "#47d22d") + 
  xlim(c(0, 1.5)) + 
  ylim(c(0, 200)) +
  coord_polar(theta = "y", start = -pi/2) + 
  theme_void()+
  theme(plot.margin = unit(c(-2, 0, -2, -.1), "cm")) + 
  geom_text(label = 0, x = 1.6, y = 2, colour = "black", size = 6, family = "Roboto") + 
  geom_text(label = 100, x = 1.7, y = 98, colour = "black", size = 6, family = "Roboto") + 
  geom_text(aes(label = title), x = 0.4, y = 50, colour = "black", size = 10, family = "Roboto")
ggsave("mtcars.png", width = 4, height = 4, bg = "transparent")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.