How to stack a graph on geom_crossbar based on one column in r

r
plot

#1

I am trying to create stacked bar but remain unsuccessful. I was wondering if there is way :

My Dataset

structure(list(variable = structure(1:10, .Label = c("GrossNetEquity", 
"GrossTotalEquityPerfAttr", "LongNetEquity", "LongTotalEquity", 
"NetEquity", "NetEquityPerfAttr", "NetTotalEquity", "ShortNetEquity", 
"ShortTotalEquity", "TotalNetEquity"), class = "factor"), mx = c(134.5, 
8.1, 95.6, 106.4, 61, 6.8, 71.6, -21.4, -24.9, 148.7), mn = c(71.1, 
-4.6, 49.7, 66.2, 27, -4.1, 36.4, -46.3, -47.4, 96), avg = c(112.173148148148, 
1.14814814814815, 77.7388888888889, 84.5111111111111, 43.262037037037, 
1.05092592592593, 48.0694444444444, -34.4194444444444, -36.4416666666667, 
120.952777777778), sd = c(14.5968093202928, 2.39877232936504, 
9.87368667081958, 8.7204382695887, 7.29159953981859, 2.24405738054356, 
7.05196278547511, 6.04899711056417, 5.77265751334298, 13.0003483658092
), md = c(114.15, 1.4, 77.35, 82.65, 41.45, 1.25, 46.35, -34.1, 
-35.55, 119.75), firstldiff = c(82.9795295075625, -3.64939651058193, 
57.9915155472497, 67.0702345719337, 28.6788379573998, -3.4371888351612, 
33.9655188734942, -46.5174386655728, -47.9869816933526, 94.9520810461593
), firstlsum = c(141.366766788734, 5.94569280687823, 97.4862622305281, 
101.951987650289, 57.8452361166742, 5.53904068701305, 62.1733700153947, 
-22.3214502233161, -24.8963516399807, 146.953474509396), secldiff = c(68.3827201872697, 
-6.04816883994697, 48.1178288764302, 58.349796302345, 21.3872384175813, 
-5.68124621570476, 26.9135560880191, -52.566435776137, -53.7596392066956, 
81.9517326803501), seclsum = c(155.963576109027, 8.34446513624327, 
107.359948901348, 110.672425919877, 65.1368356564928, 7.78309806755661, 
69.2253328008698, -16.2724531127519, -19.1236941266377, 159.953822875205
), value = c(116.1, -1.2, 88, 92.3, 58.8, -1.2, 63, -28.1, -29.3, 
121.6), Criteria = c(NA, NA, "", "", "orange", "", "orange", 
"orange", "", "orange")), .Names = c("variable", "mx", "mn", 
"avg", "sd", "md", "firstldiff", "firstlsum", "secldiff", "seclsum", 
"value", "Criteria"), row.names = c(NA, -10L), class = "data.frame")

My Incomplete Code

ggplot(df3,aes(variable,mn))+
  geom_crossbar(aes(ymin = mn, ymax = mx,fill = Criteria),width = 0.5,alpha = 0.50,position =position_dodge())+
  geom_point(data=df3, aes(x=variable,y=md,group=1),shape = "|", size = 10,color ="brown1")+
  geom_text(data=df3, aes(x=variable, y=md, label = paste("Median",md)), size = 3, vjust = 2,hjust = -1.5,color = "brown1",
            position = position_dodge(width=0.9))+
  geom_point(data=df3, aes(x=variable,y=avg,group=1),shape = "|", size = 10,color = "coral4")+
  geom_text(data=df3, aes(x=variable, y=avg, label = paste("Mean",mn)),size = 3, vjust = 4, hjust = -1.5,color ="coral4")+
  geom_point(data=df3, aes(x=variable,y=value,group=1),shape = "|", size = 10,color ="brown1")+
  geom_text(data=df3,aes(x=variable, y=value,label = paste("Current Value",value)), size = 3, vjust = 6, hjust = -1.5,color = "brown1")+
  coord_flip()

My expected result should be something like this based on Criteria
image


#2

@gurtejhira how do you want your plot to behave based on “Criteria”?


#3

I like to show stack the cross bar with criteria… currently there is only orange but it can be red or yellow. Therefore I like to show them something as shown in the output example


#4

is there any way it can be achieved ?


#5

Hi @gurtejhira I tried but could not generate the plot. Keep trying and let me know if you are able to do it.


#6

Thank you though