2016-09-07 3 views
2

작성하려고하는 점선을 아래에 표시합니다. 도트는 몇 가지 다른 옵션을 사용하여 배치되었습니다.geom_dotplot 그룹이 포함 된 도트 레이아웃

require(data.table) 
require(ggplot2) 

set.seed(10000) 
n <- 300 

dt <- data.table(Duration=sample(100:800,n,replace=T), EndType=round(runif(n,min=.4)), Group=sample(c("GrpA","GrpB"),n,replace=T)) 
dt <- rbind(dt, dt[, -c("Group"), with=F][, Group:="All"]) 
dt[, ":="(Group=factor(Group, levels=c("All","GrpA","GrpB"), ordered=T), EndType=factor(EndType, levels=c(0,1)))] 

#option 1 - creates space between dots which are filled and not filled 
g <- ggplot(dt, aes_string(x="Group", y="Duration")) + coord_flip() + 
    geom_boxplot(aes(ymin=..lower.., ymax=..upper..), fatten=1.1, lwd=.1, outlier.shape=NA) + 
    geom_dotplot(aes(fill=EndType), binaxis="y", stackdir="center", stackgroups=T, method="histodot", binwidth=15, dotsize=.5) + 
    scale_fill_manual(values=c("white","black")) 
print(g) 

#option 2 - extends to the other bar when there are many of one type 
g <- ggplot(dt, aes_string(x="Group", y="Duration")) + coord_flip() + 
    geom_boxplot(aes(ymin=..lower.., ymax=..upper..), fatten=1.1, lwd=.1, outlier.shape=NA) + 
    geom_dotplot(data=dt[EndType==1], aes(fill=EndType), fill="black", binaxis="y", stackdir="up", method="histodot", binwidth=15, dotsize=.5) + 
    geom_dotplot(data=dt[EndType==0], aes(fill=EndType), fill="white", binaxis="y", stackdir="down", method="histodot", binwidth=15, dotsize=.5) 
print(g) 

도트 (흑백)를 함께 배치하여 옵션 2와 같이 함께 표시되도록하는 방법이 있습니까?

옵션 1 enter image description here

옵션 2 enter image description here

> sessionInfo() 
R version 3.3.1 (2016-06-21) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C       
[5] LC_TIME=English_United States.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] ggplot2_2.1.0 data.table_1.9.7 

loaded via a namespace (and not attached): 
[1] labeling_0.3  colorspace_1.2-6 scales_0.4.0  plyr_1.8.4  gtable_0.2.0  
[6] Rcpp_0.12.6  grid_3.3.1  digest_0.6.10 munsell_0.4.3 
+0

귀하의 옵션 2 오류 : '오류 bmerge에 (내가 여기

대신 우리는 측면을 사용하는 하나 개의 축에 여러 dotplots의, 작업을 수행하는 빠른 해결 방법입니다 , x, leftcols, rightcols, io <- FALSE, xo, roll = 0, : x.'EndType '은'double '유형 인 i.'V1'에 결합되는 factor 열입니다. 문자 열입니다. " – Axeman

+0

이것은 data.table 문제입니다 (https://github.com/Rdatatable/data.table/issues/). 1361). 방금 sessionInfo를 추가했습니다. 'data.table'을 업데이트해야 할 필요가 있습니다. – ironv

+1

어쨌든 큰 문제는 아닙니다. 나는 당신의 질문이 기본적으로 '왜 옵션 1이 저에게 이상한 결과를 줍니까? – Axeman

답변

3

나는 당신이 버그에 실행 한 것 같아요. 첫 번째 옵션에서 왜 그룹 스태킹이 잘못 될지 잘 모르겠습니다. 아마도 다른 사람들이 몸무게가 늘어날 수 있습니다. here을 검색하거나보고해야 할 수 있습니다. 나를 위해

ggplot(dt, aes_string(x=1, y="Duration")) + coord_flip() + 
    geom_boxplot(aes(ymin=..lower.., ymax=..upper..), fatten=1.1, lwd=.1, outlier.shape=NA) + 
    geom_dotplot(aes(fill=EndType), binaxis="y", stackdir="center", stackgroups=T, method="histodot", binwidth=15, dotsize=.5) + 
    scale_fill_manual(values=c("white","black")) + 
    facet_grid(Group~.) 

enter image description here

+1

좋은 해결 방법. 스트립을 전환하고 테마를 사용하여 패싯이없는 그래프처럼 보이게 만들 수 있습니다. 시작 :'facet_grid (Group ~., switch = "y") + theme (panel.margin.y = unit (0, "lines"), panel.grid.major.y = element_blank(), panel.grid. minor.y = element_blank(), axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.ticks.length = unit (0, "lines "))') – aosmith

관련 문제