【问题标题】:Grouped Barplot (Ml Accuracies) Decrease Size / Padding between Groups and Borders分组条形图(Ml 精度)减小组和边界之间的大小/填充
【发布时间】:2020-04-21 09:33:06
【问题描述】:

在我的分组条形图的可重现示例下方。我正在为组之间的空间/填充而苦苦挣扎。我已经在 stackoverflow 上找到了一些问题,例如 thishere
我还尝试将geom_bar(position= .. 参数设置为"dodge"dodge_position(0.5) 以及我的条的宽度。但是我不想再增加我的栏的宽度。是否有任何解决方案,例如更改离散 x 轴的大小以减少图 1 中的标记空间? 更新: 减少了代码,使其对于最小的可重现示例更加精简。问题依旧

library(ggplot2)
library(ggthemes)
library(dplyr)

algorithm <- c(rep("0_DT",2),rep("1_RF",2),rep("2_MLP",2))
target <- rep(c("Some Data","Some Other Data"),3)
value <- runif(6,85,95) # Simulated Accuracies
CI_lower  <- value - 5
CI_upper  <- value + 5
data <- data.frame(target,algorithm,value,CI_lower,CI_upper)
ggplot(data, aes(fill=algorithm, y=value, x=target)) +
  geom_bar(position=position_dodge(0.75), stat="identity",  width = 0.65)+ theme_classic()+
  scale_fill_manual("Algorithm",
                    values = alpha(c("0_DT" = "#20639B", "1_RF" = "#3CAEA3", "2_MLP" = "#F6D55C"),0.8),
                    labels=c("DT","RF","MLP"))+
  scale_y_continuous("Accuracy in %",limits = c(0,100),oob = rescale_none,
                     # breaks= sort(c(seq(0,90,10),h)),
                     breaks= seq(0,100,10),
                     expand = c(0,0)) 

这是我的条形图图 1

【问题讨论】:

  • 您尝试过position_dodgeposition_dodge2 参数吗? preservepadding 可能会有所帮助
  • 您好,感谢您的评论。我刚刚尝试了position=position_dodge2(0.4,preserve="single",padding=.05) 的不同组合,将填充更改为 0,但是条形和 y 轴之间以及两组条形之间的距离始终保持不变。
  • scale_x_discrete(expand=c(0.3,0.1)) here 找到了一些东西来减少左边(y 轴)和右边的空间。然而,酒吧组之间的空间仍然有点高

标签: r ggplot2 plot bar-chart


【解决方案1】:

我可能会误解你,但你应该能够通过调整 position 参数来获得你想要的结果:

ggplot(data, aes(fill = algorithm, y = value, x = target)) +
  geom_bar(position = position_dodge(1), stat = "identity",  width = 0.65) + 
  theme_classic() +
  scale_fill_manual("Algorithm", 
                    values = alpha(c("0_DT"  = "#20639B", 
                                     "1_RF"  = "#3CAEA3", 
                                     "2_MLP" = "#F6D55C"), 0.8),
                    labels = c("DT", "RF", "MLP")) +
  scale_y_continuous("Accuracy in %",limits = c(0, 100), oob = rescale_none,
                     breaks= seq(0, 100, 10),
                     expand = c(0, 0)) 

如果您想保持条的外观宽度但同时减少条之间的空间,那么您将不得不增加宽度但通过更改窗口尺寸或绘图边距来更改整个绘图的形状:

ggplot(data, aes(fill = algorithm, y = value, x = target)) +
  geom_bar(position = position_dodge(1), stat = "identity",  width = 0.8) + 
  theme_classic() +
  scale_fill_manual("Algorithm", 
                    values = alpha(c("0_DT"  = "#20639B", 
                                     "1_RF"  = "#3CAEA3", 
                                     "2_MLP" = "#F6D55C"), 0.8),
                    labels = c("DT", "RF", "MLP")) +
  scale_y_continuous("Accuracy in %",limits = c(0, 100), oob = rescale_none,
                     breaks= seq(0, 100, 10),
                     expand = c(0, 0)) +
 theme(plot.margin = unit(c(10, 50, 10, 50), "points"))

这可能不是您想要的,但position_dodgewidthplot.margin 的组合将为您提供所需的间距。


编辑

按照 OP 的 cmets,看起来最终的解决方案应该是这样的,由 width = 0.9position_dodge(0.9)plot.margin = unit(c(10, 50, 10, 50), "points")) 完成:

【讨论】:

  • 您好,感谢您的回答(赞成)。我想我现在很满意。使用您的解决方案或使用 scale_x_discrete 扩展版本。您发布的图的第二个版本已经非常接近我的预期。然而,一组中三种算法之间的空间应该为零。因此,Some Data 的所有 3 个条彼此之间的距离应该为 0,然后是一个小的(er)间隙,然后是 Some Other Data 的三个算法。
  • 完美。非常感谢