成组的Barplot(精确度M1)减小组和边界之间的大小/填充

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

下面是我的条形图的可复制示例。我在各组之间的空间/填充空间中苦苦挣扎。我已经发现了一些关于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)) 

这是我的Barchart 图1

enter image description here

r ggplot2 plot bar-chart
1个回答
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)) 

enter image description here

如果要保持条形的表观宽度但还要减小条形之间的间距,则必须增加宽度,但要通过更改窗口尺寸或图边距来更改所有图的形状:

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"))

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.