条形图宽度和其他条形顶部的顺序

时间:2018-03-20 18:01:57

标签: r ggplot2

我有这个情节

library(ggplot2)
library(reshape)


x = c("Band 1", "Band 2", "Band 3")
y1 = c("1","2","3")
y2 = c("2","3","4")

to_plot <- data.frame(x=x,y1=y1,y2=y2)
melted<-melt(to_plot, id="x")

ggplot(melted,aes(x=x,y=value,fill=variable)) + 
        geom_bar(stat="identity",position = "identity", alpha=.3 ,width = .3)

是否可以:

(1)使变量= y1的宽度为.3,变量= y2为.5

(2)make variable = y1始终是顶栏

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用position_dodge(width=0)获得完全重叠(抛出警告)。对于宽度,您可以为每个y*创建一个新变量。

<强>产率:

melted$width <- with(melted, ifelse(variable=="y1", .3, .5))

ggplot(melted, aes(x=x, y=value, fill=variable)) + 
  geom_bar(stat="identity",
           position = position_dodge(width=0), 
           width = melted$width, alpha=.7) 

enter image description here

注意:我已将alpha更改为.7来说明。