R 尝试使用ggplot2中的“镶嵌面_包裹”固定条宽时重叠条

R 尝试使用ggplot2中的“镶嵌面_包裹”固定条宽时重叠条,r,ggplot2,geom-bar,R,Ggplot2,Geom Bar,我有同样的问题,这里也解释了 也就是说,当切面时,我的条显示不同的宽度。我尝试了那里提出的解决方案,但似乎我做错了什么,因为我的酒吧重叠。我不是一个高级R用户,所以任何解释都将不胜感激 我的代码: # Data z <- rbind.data.frame(c(23.230077, 109.824940, 72.313763, 76.95888), c(29.154963, 113.716729, 94.689684, 64.29041),

我有同样的问题,这里也解释了 也就是说,当切面时,我的条显示不同的宽度。我尝试了那里提出的解决方案,但似乎我做错了什么,因为我的酒吧重叠。我不是一个高级R用户,所以任何解释都将不胜感激

我的代码:

# Data
z <- rbind.data.frame(c(23.230077, 109.824940, 72.313763, 76.95888), 
                  c(29.154963, 113.716729, 94.689684, 64.29041),
                  c(8.450325, 99.190459, 53.193431, 32.97232),
                  c(15.719120, 126.947621, 39.767791, 56.8059),
                  c(15.497960, 117.942545, 73.659386, 69.37012),
                  c(13.522866, 9.939251, 5.906541, 27.69283))
colnames(z) <- c("Biomass", "Metals", "Other minerals", "Fossil fuels")
rownames(z) <- c("Exiobase full", "Exiobase global", "Exiobase EU","ENVIMAT", "ENVIMAT corrected", "Direct Imports")
library(ggplot2)
library(reshape2)
library(plyr)

z1 <- melt(as.matrix(z)); z2 <- c(rep(c(rep("Exiobase", 3), rep("Envimat",2), "Direct"),4))
z3 <- cbind.data.frame(z1, z2)
colnames(z3) <- c("Model", "Material", "Value", "Version")

# Here from the solution posted
N <- ddply(z3, .(Version), function(x) length(row.names(x)))
N$Fac <- N$V1 / max(N$V1)
z4 <-  merge(z3, N[,-2], by = c("Version"))

# Plotting
fig3 <- ggplot(data=z4, aes(x=Version, y=Value ,fill=Model))+
    geom_bar(aes(width = 1.5*Fac),stat="identity", position=position_dodge())+  scale_fill_brewer(palette="Set2")+ ylab("Million Tons")+
    geom_text(aes(label = sprintf("%.1f",round(Value, 1), size=10)), position=position_dodge(width=0.9), vjust=-0.25)+
    theme_bw()+theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(colour="grey", linetype = "dotted"),axis.title.x = element_blank(),legend.title=element_blank(), panel.background = element_rect(colour = "black"),legend.key=element_blank(),legend.text = element_text(colour="black"), strip.background = element_blank())+
    facet_wrap(~Material, nrow=2)
plot(fig3)
非常感谢

position\u dodge()
可以采用宽度参数。使用它设置遮光宽度,并确保将其应用于
geom_条
geom_文本
。您可能需要调整y轴比例的限制、文本的大小以及图形设备的大小

另外,将
size
置于
geom\u text
aes()
语句之外

大概是这样的:

# Data
z <- rbind.data.frame(c(23.230077, 109.824940, 72.313763, 76.95888), 
                  c(29.154963, 113.716729, 94.689684, 64.29041),
                  c(8.450325, 99.190459, 53.193431, 32.97232),
                  c(15.719120, 126.947621, 39.767791, 56.8059),
                  c(15.497960, 117.942545, 73.659386, 69.37012),
                  c(13.522866, 9.939251, 5.906541, 27.69283))
colnames(z) <- c("Biomass", "Metals", "Other minerals", "Fossil fuels")
rownames(z) <- c("Exiobase full", "Exiobase global", "Exiobase EU","ENVIMAT", "ENVIMAT corrected", "Direct Imports")
library(ggplot2)
library(reshape2)
library(plyr)

z1 <- melt(as.matrix(z)); z2 <- c(rep(c(rep("Exiobase", 3), rep("Envimat",2), "Direct"),4))
z3 <- cbind.data.frame(z1, z2)
colnames(z3) <- c("Model", "Material", "Value", "Version")

# Here from the solution posted
N <- ddply(z3, .(Version), function(x) length(row.names(x)))
N$Fac <- N$V1 / max(N$V1)
z4 <-  merge(z3, N[,-2], by = c("Version"))

# Plotting
BarWidth = .75
DodgeWidth = .75
fig3 <- ggplot(data=z4, aes(x=Version, y=Value ,fill=Model))+
    geom_bar(aes(width = BarWidth*Fac),stat="identity", position=position_dodge(width = DodgeWidth))+  
   scale_fill_brewer(palette="Set2")+ ylab("Million Tons")+
    geom_text(aes(label = sprintf("%.1f",round(Value, 1))), size=2, 
        position=position_dodge(width=DodgeWidth), vjust=-0.25)+
    theme_bw()+theme(panel.grid.major.x = element_blank(), 
    panel.grid.major.y = element_line(colour="grey", linetype = "dotted"),
     axis.title.x = element_blank(),legend.title=element_blank(), 
    panel.background = element_rect(colour = "black"),
    legend.key=element_blank(),
    legend.text = element_text(colour="black"), 
    strip.background = element_blank())+
    facet_wrap(~Material, nrow=2)
plot(fig3)

谢谢桑迪。我使用了你的解决方案,但另一个问题出现了。我想控制一组条(两组用于envimat,三组用于envimat),这样它们在更改减淡宽度时保持在一起(我将编辑这个问题)@Pablo我添加了一个特定于
图3
的编辑。该解决方案修改
ggplot\u build
数据中条形图的位置。它所做的只是修改两个
Envimat
条的位置。需要一个更普遍的解决方案,但我需要一些时间来解决。同时,如果有人提出了一个普遍的解决方案,那就好了。
BarWidth = 0.75
DodgeWidth = .75
# Data
z <- rbind.data.frame(c(23.230077, 109.824940, 72.313763, 76.95888), 
                  c(29.154963, 113.716729, 94.689684, 64.29041),
                  c(8.450325, 99.190459, 53.193431, 32.97232),
                  c(15.719120, 126.947621, 39.767791, 56.8059),
                  c(15.497960, 117.942545, 73.659386, 69.37012),
                  c(13.522866, 9.939251, 5.906541, 27.69283))
colnames(z) <- c("Biomass", "Metals", "Other minerals", "Fossil fuels")
rownames(z) <- c("Exiobase full", "Exiobase global", "Exiobase EU","ENVIMAT", "ENVIMAT corrected", "Direct Imports")
library(ggplot2)
library(reshape2)
library(plyr)

z1 <- melt(as.matrix(z)); z2 <- c(rep(c(rep("Exiobase", 3), rep("Envimat",2), "Direct"),4))
z3 <- cbind.data.frame(z1, z2)
colnames(z3) <- c("Model", "Material", "Value", "Version")

# Here from the solution posted
N <- ddply(z3, .(Version), function(x) length(row.names(x)))
N$Fac <- N$V1 / max(N$V1)
z4 <-  merge(z3, N[,-2], by = c("Version"))

# Plotting
BarWidth = .75
DodgeWidth = .75
fig3 <- ggplot(data=z4, aes(x=Version, y=Value ,fill=Model))+
    geom_bar(aes(width = BarWidth*Fac),stat="identity", position=position_dodge(width = DodgeWidth))+  
   scale_fill_brewer(palette="Set2")+ ylab("Million Tons")+
    geom_text(aes(label = sprintf("%.1f",round(Value, 1))), size=2, 
        position=position_dodge(width=DodgeWidth), vjust=-0.25)+
    theme_bw()+theme(panel.grid.major.x = element_blank(), 
    panel.grid.major.y = element_line(colour="grey", linetype = "dotted"),
     axis.title.x = element_blank(),legend.title=element_blank(), 
    panel.background = element_rect(colour = "black"),
    legend.key=element_blank(),
    legend.text = element_text(colour="black"), 
    strip.background = element_blank())+
    facet_wrap(~Material, nrow=2)
plot(fig3)
gb <- ggplot_build(fig3)

w = with(gb$data[[1]][1,], xmax-xmin)

for(i in 1:2) {
gb$data[[i]][gb$data[[i]]$group == 2, "x"] = 2-(w/2)
gb$data[[i]][gb$data[[i]]$group == 2, "xmin"] = 2-(w/2)-(w/2)
gb$data[[i]][gb$data[[i]]$group == 2, "xmax"] = 2-(w/2)+(w/2)

gb$data[[i]][gb$data[[i]]$group == 3, "x"] = 2+(w/2)
gb$data[[i]][gb$data[[i]]$group == 3, "xmin"] = 2+(w/2)-(w/2)
gb$data[[i]][gb$data[[i]]$group == 3, "xmax"] = 2+(w/2)+(w/2)
}

# Get the ggplot grob
gp = ggplot_gtable(gb)

library(grid)
grid.newpage()
grid.draw(gp)