2016-07-27 69 views
0

我想使用ggplot2绘制条形图,以便条形具有相同的宽度。 我有以下数据集。ggplot2 barplot相同宽度

 sexratio=c("0%male","0%male","0%male","0%male","25%male","25%male","50%male","50%male" ,"75%male","75%male","100%male","100%male","100%male", "100%male") 
    Trainsize=c("Ts130","Ts260","Ts520","Ts1040","Ts130", "Ts1040","Ts130", "Ts1040", "Ts130", "Ts1040","Ts130", "Ts260", "Ts520", "Ts1040") 
    Dm1=c(354.7015, 362.6982, 369.8013, 380.7233, 363.2208, 415.8980, 367.2899, 413.7292, 365.1060, 409.1913, 366.9871, 377.3490, 389.0739, 400.5590) 
    mydata=data.frame(Trainsize,sexratio,Dm1) 

    #Reorder the fators 
    mydata$sexratio<−factor(mydata$sexratio,levels(mydata$sexratio)[c(2:4,1)]) 

    mydata$Trainsize<- factor(mydata$Trainsize,levels(mydata$Trainsize)[c(2:4,1)]) 

我用下面的代码绘制的情节,但要注意,棒材Ts260和Ts520的宽度都较大。

 p2<- ggplot(mydata, aes(x=Trainsize, y=Dm1, fill=sexratio)) +geom_bar(stat="identity", color="black",position=position_dodge()) + 
     geom_errorbar(aes(ymin=Dm1-10, ymax=Dm1+10), width=.2,position=position_dodge(.9)) 

因此,我包括NA为Ts260和Ts520的缺失值,并重绘该图。

require(utils) 
    dat<-expand.grid(sexratio= c("25%male","50%male","75%male"), Trainsize= c("Ts260","Ts520")) 

    dat$Dm1<- NA 
    mydata<- rbind(mydata,dat) 

    p2<- ggplot(mydata, aes(x=Trainsize, y=Dm1, fill=sexratio)) + 

     geom_bar(stat="identity", color="black", 
       position=position_dodge()) + 
     geom_errorbar(aes(ymin=Dm1-10, ymax=Dm1+10), width=.2, 
        position=position_dodge(.9)) 

现在的问题是如何减少Ts260,Ts520之间0%男性和100%男性之间的空间。

或者我如何绘制因子TS的水平之间的垂直线;即在Ts130,Ts260,Ts520,Ts1040处有四条垂直线。

回答

0

如果我理解正确,您希望所有酒吧的宽度相同,但您也希望酒吧之间的空间相同。

首先,您的代码似乎存在问题:您放弃sexratio的某个因子级别。在这里,我按照从0%到100%的顺序排列sexratio的等级。第二,让我告诉你使用facet.grid使用你的第二版mydata的效果;这里命名为mydata2。使用switch = "x"将条形文字移至底部。

library(ggplot2) 

# Some data 
sexratio=c("0%male","0%male","0%male","0%male","25%male","25%male","50%male","50%male" ,"75%male","75%male","100%male","100%male","100%male", "100%male") 
    Trainsize=c("Ts130","Ts260","Ts520","Ts1040","Ts130", "Ts1040","Ts130", "Ts1040", "Ts130", "Ts1040","Ts130", "Ts260", "Ts520", "Ts1040") 
    Dm1=c(354.7015, 362.6982, 369.8013, 380.7233, 363.2208, 415.8980, 367.2899, 413.7292, 365.1060, 409.1913, 366.9871, 377.3490, 389.0739, 400.5590) 

mydata=data.frame(Trainsize,sexratio,Dm1) 

mydata$sexratio <− factor(mydata$sexratio, levels(mydata$sexratio)[c(2,5,4,3,1)]) 
mydata$Trainsize <- factor(mydata$Trainsize,levels(mydata$Trainsize)[c(2:4,1)]) 

# Add NAs where sexratio categories are missing 
dat <- expand.grid(sexratio = c("25%male", "50%male", "75%male"), Trainsize = c("Ts260", "Ts520")) 

dat$Dm1 <- NA 
mydata2 <- rbind(mydata, dat) 

# The plot 
ggplot(mydata2, aes(x = Trainsize, y = Dm1, fill = sexratio)) + 
    geom_bar(stat = "identity", color = "black", position = position_dodge(width = .85)) + 
    geom_errorbar(aes(ymin = Dm1 - 10, ymax = Dm1 + 10), width = .2, 
     position = position_dodge(width = .85)) + 
    facet_grid(. ~ Trainsize, scales = "free_x", switch = "x") + 
     theme(axis.text.x = element_blank(), 
      strip.background = element_rect(fill = NA)) 

enter image description here

在我看来,这是比后面是因为...提请读者注意一个事实,即有数据差距的一个更好的图表。

但是,如果你坚持酒吧之间没有间距,那么这样的事情会让你关闭。它使用您的第一个版本mydata;它使用facet_grid,其中scalesspace设置为"free_x";将条形文字移动到底部(switch = "x");加上一些整理。

ggplot(mydata, aes(x = factor(1:dim(mydata)[1]), y = Dm1, fill = sexratio)) + 
     geom_bar(stat = "identity", color = "black", width = 1) + 
     geom_errorbar(aes(ymin = Dm1 - 10, ymax = Dm1+10), width = .2) + 
     facet_grid(. ~ Trainsize, scales = "free_x", space = "free_x", switch = "x") + 
     scale_x_discrete("Trainsize", expand = c(0, .75)) + 
     theme(axis.text.x = element_blank(), 
      strip.background = element_rect(fill = NA), 
      axis.ticks.x = element_blank()) 

enter image description here

+0

是的,这就是我想要的。感谢Setye,致以最诚挚的问候 – setye