2016-07-27 5 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)) 

따라서 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)) 

문제는 지금은 0 %의 남성과 100 % 남성을위한 Ts260, Ts520 사이의 공간을 줄일 않는 방법이다.

또는 어떻게 요인 TS의 수준 사이에 수직선을 그립니 까? 즉 Ts130, Ts260, Ts520, Ts1040에서 네 개의 수직선이 있습니다.

답변

0

정확하게 이해하면 모든 막대에 대해 동일한 너비가 필요하지만 막대 사이의 간격을 동일하게 유지해야합니다.

먼저 코드에 문제가있는 것처럼 보입니다. sexratio에 대한 요인 수준 중 하나를 떨어 뜨립니다. 여기에서 나는 sexratio의 레벨을 0 %에서 100 %까지 순서대로 정렬합니다.

두 번째로 의 두 번째 버전을 사용하여 facet.grid을 사용하는 효과를 보여 드리겠습니다. 여기에 mydata2으로 명명되었습니다. 스트립 텍스트는 switch = "x"을 사용하여 하단으로 이동합니다. 제 생각에는

enter image description here

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

이는 데이터에 차이가 있다는 사실을 독자의 주목을 받고 becaus 다음 사람보다 더 나은 차트이다.

그러나 막대 사이에 간격이 없다고 주장하면이 같은 것이 가까이에있게됩니다. 첫 번째 버전은 mydata입니다. scalesspace 모두가 "free_x"으로 설정된 facet_grid을 사용합니다. 스트립 텍스트를 아래쪽으로 이동합니다 (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