0

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)
oscartorom
  • 92
  • 7
  • How exactly are you rendering the plot? Can't you just make the whole plot less tall? ggplots resize to fill whatever space you given them. Alternatively add margins to the top and bottom of the plot. Otherwise I'm not sure what or where you want to fill the extra space with. – MrFlick Jun 03 '20 at 04:43
  • The plot is being rendered automatically in PowerBI, ad it maintains the width when I manipulate its size. I will try adding margins within ggplot and PowerBI to see if it works, seems like a good solution. – oscartorom Jun 03 '20 at 04:47
  • 1
    @oscartorom: not answering your question but please consider not using red and green together because it's not good for colorblind people. Some other colormaps to choose https://stackoverflow.com/a/52812120/786542 & https://www.nature.com/articles/519291d – Tung Jun 03 '20 at 05:37

1 Answers1

1

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"))
Wolfgang Arnold
  • 1,252
  • 8
  • 17
  • 1
    Thanks, the approach works in a different way. I found out the item has to be scaled within R when plotted in PowerBI, I ended up using xlim. I tried your approach and it works too but xlim is a bit simpler, you need to adjust it manually when in PowerBI though. – oscartorom Jun 08 '20 at 23:48