r - How to order error bars in ggplot2 on two conditions -
i trying order error bars in ggplot based on 2 columns. have looked @ provided answers helpful 1 column,but when want order on 2 fail obtain intended result.
this code fulfil firstwish of ordering teh bars based on first column (outcome)
ddf$outcome <- factor(ddf$outcome, levels=c("total", "cardva" , "ami" , "heartf" ,"dysrhy"), ordered=true)
however, when want order on column cat(with order of whole year, cold season , warm season) code not work.
ddf$cat <- factor(ddf$cat, levels= c("whole year", "cold","warm"), ordered=true)
how can order bars based on outcome , cat? next code produces plot alphabetical order of column cat.
dodge <- position_dodge(width=0.9) ggplot(ddf, aes(x = outcome, y = pinc,ymin = lcinc, ymax = ucinc, group=cat,color=cat,shape=cat)) + geom_point(position=dodge,size = 4) + geom_linerange(position=dodge,size =0.7) + geom_hline(aes(yintercept = 0)) + scale_x_discrete("", labels = c("ami" = "ami","cardva" = "any cvd", "dysrhy" = "dysrhythmia", "heartf" = "hf", "total"= "all subjects")) + theme_bw() + labs(colour="period", shape="period", y="relative difference (%)") + theme(axis.title=element_text(face="bold",size="14"),axis.text=element_text(size=14,face="bold"))
sample info follows
dput(ddf) structure(list(outcome = structure(c(1l, 1l, 1l, 2l, 3l, 4l, 5l, 2l, 3l, 4l, 5l, 2l, 3l, 4l, 5l), .label = c("total", "cardva", "ami", "heartf", "dysrhy"), class = c("ordered", "factor")), pinc = c(0.50389187458233, 0.579539836910437, 0.601685513579753, 0.28267013091543, 1.71852544919748, -0.284681087465499, -0.315288733679508, -0.214290507233894, -0.0319464172748196, -1.0103617140803, 0.0669795902723536, 0.338718648843339, 2.46728604502957, -0.626929184485836, -1.33251808343037), lcinc = c(0.124678813009127, -0.000690299706695985, 0.101542783301833, -0.317021516853733, 0.436205248000321, -1.32202043292773, -1.93050761165515, -1.18689153946393, -2.12567063251963, -2.7029110723661, -2.57800553889581, -0.573641468633246, 0.539635356054902, -2.18756799993103, -3.77359786707672), ucinc = c(0.884541170935749, 1.16313666688221, 1.10432713392166, 0.885969516714469, 3.01721768570142, 0.763563152101443, 1.32653303537096, 0.767883675589554, 2.10656667282445, 0.711630697043031, 2.78377546784718, 1.25945080162582, 4.43189566234674, 0.958610284817674, 1.17048722562318), cat = c("whole year", "cold season", "warm season", "whole year", "whole year", "whole year", "whole year", "cold season", "cold season", "cold season", "cold season", "warm season", "warm season", "warm season", "warm season")), .names = c("outcome", "pinc", "lcinc", "ucinc", "cat"), row.names = c("2", "8", "5", "41", "42", "44", "45", "31", "32", "34", "35", "26", "27", "29", "30"), class = "data.frame")
r plot ggplot2