简体   繁体   English

ggplot2条形图的宽度相同

[英]ggplot2 barplot the same width

I want to draw a bar plot using ggplot2 so that the bars have the same width. 我想使用ggplot2绘制条形图,以使条形具有相同的宽度。 I have the following data-set. 我有以下数据集。

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

I used the following code to draw the plot, but note that the width of the bars for Ts260 and Ts520 are larger. 我使用以下代码绘制了该图,但请注意,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))  

So I include NA for missing values for Ts260 and Ts520, and redraw the plot. 因此,我为Ts260和Ts520的缺失值添加了NA,并重新绘制了图。

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

The question now is how do I reduce the space between Ts260, Ts520 for 0%male and 100%male. 现在的问题是,如何减少0%male和100%male的Ts260,Ts520之间的间距。

Or how do I draw vertical lines between the levels of the factor TS; 或者如何在因子TS的水平之间绘制垂直线; that is four vertical lines at Ts130, Ts260, Ts520, Ts1040. 分别是Ts130,Ts260,Ts520,Ts1040的四条垂直线。

If I understand correctly, you want the same width for all bars, but also you want the space between the bars to be the same. 如果我理解正确,则您希望所有条形的宽度相同,但也希望条形之间的间隔相同。

First, there appears to be problem with your code: you drop one of the factor levels for sexratio . 首先,您的代码似乎有问题:您降低了sexratio的因子水平sexratio Here, I arrange the levels for sexratio in order from 0% through to 100%. 在这里,我将sexratio的级别按从0%到100%的顺序进行排列。

Second, let me show you the effect of using facet.grid using your second version of mydata ; 其次,让我向您facet.grid使用第二个版本的mydata使用facet.grid的效果; here named mydata2 . 这里名为mydata2 The strip text is moved to the bottom using switch = "x" . 使用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))

在此处输入图片说明

In my opinion, this is a better chart than the one that follows becaus it draws the reader's attention to the fact that there are gaps in the data. 我认为,这是一个比其后的图表更好的图表,因为它引起读者注意数据中存在空白这一事实。

But if you insist on there being no spacing between the bars, then something like this gets you close. 但是,如果您坚持在条形之间没有间距,那么类似的事情会使您靠近。 It uses your first version of mydata ; 它使用您的mydata的第一个版本; it uses facet_grid with both scales and space set to "free_x" ; 它使用facet_grid ,其scalesspace设置为"free_x" ; moves the strip text to the bottom ( switch = "x" ); 将带状文本移到底部( switch = "x" ); plus some tidying up. 加上一些整理。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM