在R中使用ggplot2创建线性仪表:减少barplot宽度。

问题描述 投票:0回答:1

我已经在R中创建了一个线性仪表,要在PowerBI中显示,我唯一的问题是不能调整绘图的宽度,所以我得到以下信息。

enter image description here

(绘图正在PowerBI中渲染)

我试过在geom_bar中使用width,但它会调整条形图的大小,而最终的输出是一样的。

理想情况下,条形图的宽度应该是其当前宽度的一半(我正在为PowerBI报告构建这个图形)。

这是我使用的代码。

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)
r ggplot2 powerbi powerbi-desktop gauge
1个回答
1
投票

如果简单地调整Power BI可视化的大小是不可能的,你可以使用 theme(plot.margin = unit(c(0, 0.2, 0, 0.2), "npc")) 用来增加ggplot在绘图时的边距。完整的代码。

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"))
© www.soinside.com 2019 - 2024. All rights reserved.