r - geom_dotplot dot layout with groups -
the dotplot trying create shown below. dots have been laid out using couple of different options.
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 filled , 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 other bar when there many of 1 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)
is there way of laying out dots (black , white) in option 2 centered across line?
> 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 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
i think may have run bug. i'm not sure why stacking of groups going wrong in first option. perhaps others can weigh in. might want search for, , otherwise report, issue here.
here quick workaround does work, instead of multiple dotplots on 1 axes use facets:
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~.)
Comments
Post a Comment