Search code examples
rggplot2powerbipowerbi-desktopgauge

Creating a linear gauge in R with ggplot2: reducing barplot width


I have created a linear gauge in R to be displayed within PowerBI. My only issue is that the width of the plot cannot be adjusted so I am getting the following:

enter image description here

(Plot is being rendered in PowerBI)

Whereas I would like to obtain the same graph but half the width. I tried using width within geom_bar but it resizes the bar and the final output is the same.

Ideally, the bar would be half its current width (I am building this graph for a PowerBI report).

This is the code I used:

library(ggplot2)

scores = factor(c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional'), 
       levels = (c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional')),
       ordered = TRUE)

x <- data.frame(points = rep(1,7), scores= scores)

x %>%
  ggplot(aes(x=points, fill=scores)) +
  geom_bar(position = "stack", show.legend = FALSE) +
  geom_text(aes(label=scores, y = seq(from=0.5, to=6.5, by = 1)), label.size = 0.25)+
  coord_flip() +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank()) +
  geom_point(aes(x= 1.45, y=5), shape = 25, size=10, colour = "black", fill = "black") +
  geom_point(aes(x= 0.55, y=3), shape = 24, size=10, colour = "black", fill = "black") +
  geom_point(aes(x= 0.55, y=6), shape = 24, size=10, colour = "black", fill = "black") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1)

Solution

  • If simply resizing the Power BI visual is no option, you can use theme(plot.margin = unit(c(0, 0.2, 0, 0.2), "npc")) for increasing margins that ggplot draws around plot. Full code:

    library(tidyverse)
    
    scores = factor(c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional'), 
           levels = (c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional')),
           ordered = TRUE)
    
    x <- data.frame(points = rep(1,7), scores= scores)
    
    x %>%
      ggplot(aes(x=points, fill=scores)) +
      geom_bar(position = "stack", show.legend = FALSE) +
      geom_text(aes(label=scores, y = seq(from=0.5, to=6.5, by = 1)), label.size = 0.25)+
      coord_flip() +
      theme(panel.background = element_blank(),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            axis.line = element_blank(),
            axis.title = element_blank(),
            axis.ticks = element_blank(),
            axis.text = element_blank()) +
      geom_point(aes(x= 1.45, y=5), shape = 25, size=10, colour = "black", fill = "black") +
      geom_point(aes(x= 0.55, y=3), shape = 24, size=10, colour = "black", fill = "black") +
      geom_point(aes(x= 0.55, y=6), shape = 24, size=10, colour = "black", fill = "black") +
      scale_fill_brewer(palette = "RdYlGn", direction = -1) +
      theme(plot.margin = unit(c(0, 0.2, 0, 0.2), "npc"))