r - Problems with ordering for geom_segment chart -
i appreciate advice plot - ggplot novice!
i trying create cleveland dot plot faceted cluster, has 3 levels. have 3 issues struggling with:
within each cluster, want dots ordered continuous x-var. code below isn't ordering correctly.
is possible change dot type based on whether y-var ends in 0 (does not have characteristic) or 1 (does have characteristic)?
i have variable in data set (population) shows population % of characteristic. see if cluster characteristic over/under-represented compared population. add dot on same line of each y-var.
here code :
ggplot(cl1, aes(x=cluster_prop, y=reorder(var, cluster_prop)))+ geom_segment(aes(yend=var), xend=0, colour="grey50")+ geom_point(size=3, aes(colour=cluster))+ facet_grid(cluster~., scales="free_y", space="free_y") + ggtitle("top 10 cluster characteristics: % children within cluster feature") here data:
> dput(cl1) structure(list(var = structure(c(2l, 3l, 5l, 7l, 14l, 16l, 18l, 19l, 20l, 22l, 15l, 9l, 7l, 6l, 21l, 13l, 17l, 12l, 4l, 11l, 15l, 17l, 21l, 1l, 13l, 4l, 10l, 12l, 6l, 8l), .label = c("asthdoc_1", "attacksonexer_1_0", "attacksttt_1_0", "attacksttt_1_1", "breath0rmal_1_0", "breath0rmal_1_1", "casthmamed_1_0", "casthmamed_1_1", "ccurrentasthma_1_0", "ccurrentasthma_1_1", "congcolds_1_1", "coughnight_1_1", "coughwithcolds_1_1", "everwheeze_1_0", "everwheeze_1_1", "wheeze6m_1_0", "wheeze6m_1_1", "wheezemostdays_1_0", "wheezeocc_1_0", "wheezewithcolds_1_0", "wheezewithcolds_1_1", "wheezewithshort_1_0"), class = "factor"), cluster_prop = c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 99.4219653, 98.8439306, 95.3757225, 94.7976879, 83.2369942, 79.1907514, 53.7572254, 50.867052, 50.867052, 100, 100, 100, 93.103448, 89.655172, 86.206897, 86.206897, 82.758621, 79.310345, 79.310345), population = c(96.131528, 78.143133, 63.636364, 95.16441, 60.928433, 67.891683, 97.485493, 89.555126, 62.669246, 90.32882, 39.071567, 94.584139, 95.16441, 36.363636, 37.330754, 68.665377, 32.108317, 43.520309, 21.856867, 42.166344, 39.071567, 32.108317, 37.330754, 9.864603, 68.665377, 21.856867, 5.415861, 43.520309, 36.363636, 4.83559), cluster = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l), .label = c("1", "2", "3"), class = "factor")), .names = c("var", "cluster_prop", "population", "cluster"), row.names = c(na, -30l), vars = "cluster", drop = true, indices = list( 0:9, 10:19, 20:29), group_sizes = c(10l, 10l, 10l), biggest_group_size = 10l, labels = structure(list( cluster = 1:3), row.names = c(na, -3l), class = "data.frame", vars = "cluster", drop = true, .names = "cluster"), class = c("grouped_df", "tbl_df", "tbl", "data.frame")) many advice!
for second (edit , third) issue(s):
library(tidyverse) library(stringr) str_sub(str, start = -1, end = -1) cl2 <- cl1 %>% mutate(shape = str_sub(var, start = -1, end = -1)) ggplot(cl2, aes(x=cluster_prop, y=reorder(var, cluster_prop)))+ geom_segment(aes(yend=var), xend=0, colour="grey50")+ geom_point(size=3, aes(colour=cluster, shape = shape))+ geom_point(aes(x = population), size = 2, color = "black")+ facet_grid(cluster~., scales="free_y", space="free_y") + ggtitle("top 10 cluster characteristics: % children within cluster feature") 

Comments
Post a Comment