1

Something like this

enter image description here

I am trying to make stacked bar graph but don't want cumulative values

 ggplot() + 
   geom_bar(data = era, aes(x=variable, y=value, fill=Criteria), 
     stat="identity", alpha = 0.5) +
   scale_fill_manual(values=c("red3","orange3", "green3", "yellow3")) +
   geom_point(data = newtable, aes(x=Strategy, y=median_era, group=1), 
     shape = "|", size = 20, color ="brown1") +
   geom_point(data = newtable, aes(x=Strategy, y=mean_era, group=1), 
     shape = "|", size = 20, color = "coral4") +
   geom_point(data = searches, aes(x=Strategy, y=value, group=2), 
     shape = "|", size = 20, color = "darkblue") + 
   coord_flip()

My Output vs desired output enter image description here

Here is my data set

data era

era <- structure(list(Date = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("18.Apr", "18.Feb", "18.Jan"
), class = "factor"), variable = structure(c(3L, 3L, 3L, 4L, 
4L, 4L, 8L, 8L, 8L, 9L, 9L, 9L, 5L, 5L, 5L, 7L, 7L, 7L, 1L, 1L, 
1L, 10L, 10L, 10L, 6L, 6L, 6L, 2L, 2L, 2L), .Label = c("GrossNetEquity", 
"GrossTotalEquityPerfAttr", "LongNetEquity", "LongTotalEquity", 
"NetEquity", "NetEquityPerfAttr", "NetTotalEquity", "ShortNetEquity", 
"ShortTotalEquity", "TotalNetEquity"), class = "factor"), value = c(89.1, 
86.4, 79.6, 92.3, 89.6, 83, -28.1, -28, -26.9, -29.5, -29.5, 
-27.6, 61, 58.4, 52.7, 62.8, 60.1, 55.4, 117.2, 114.4, 106.5, 
121.8, 119.1, 110.6, 1.2, -2.1, 1.8, 1.4, -2.1, 1.9), Criteria = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 3L, 3L, 4L, 4L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Above 70", 
"Below 45", "Between 45 & 60", "Between 60 & 70"), class = "factor")), .Names = c("Date", 
"variable", "value", "Criteria"), class = "data.frame", row.names = c(NA, 
-30L))

data searches

searches <- structure(list(Date = structure(c(100L, 100L, 100L, 100L, 100L, 
  100L, 100L, 100L, 100L, 100L), .Label = c("10.Apr", "10.Aug", 
  "10.Dec", "10.Feb", "10.Jan", "10.Jul", "10.Jun", "10.Mar", "10.May", 
  "10.Nov", "10.Oct", "10.Sep", "11.Apr", "11.Aug", "11.Dec", "11.Feb", 
  "11.Jan", "11.Jul", "11.Jun", "11.Mar", "11.May", "11.Nov", "11.Oct", 
  "11.Sep", "12.Apr", "12.Aug", "12.Dec", "12.Feb", "12.Jan", "12.Jul", 
  "12.Jun", "12.Mar", "12.May", "12.Nov", "12.Oct", "12.Sep", "13.Apr", 
  "13.Aug", "13.Dec", "13.Feb", "13.Jan", "13.Jul", "13.Jun", "13.Mar", 
  "13.May", "13.Nov", "13.Oct", "13.Sep", "14.Apr", "14.Aug", "14.Dec", 
  "14.Feb", "14.Jan", "14.Jul", "14.Jun", "14.Mar", "14.May", "14.Nov", 
  "14.Oct", "14.Sep", "15.Apr", "15.Aug", "15.Dec", "15.Feb", "15.Jan", 
  "15.Jul", "15.Jun", "15.Mar", "15.May", "15.Nov", "15.Oct", "15.Sep", 
  "16.Apr", "16.Aug", "16.Dec", "16.Feb", "16.Jan", "16.Jul", "16.Jun", 
  "16.Mar", "16.May", "16.Nov", "16.Oct", "16.Sep", "17.Apr", "17.Aug", 
  "17.Dec", "17.Feb", "17.Jan", "17.Jul", "17.Jun", "17.Mar", "17.May", 
  "17.Nov", "17.Oct", "17.Sep", "18.Apr", "18.Feb", "18.Jan", "18.Jun", 
  "18.Mar", "18.May", "7.Aug", "7.Dec", "7.Jul", "7.Nov", "7.Oct", 
  "7.Sep", "8.Apr", "8.Aug", "8.Dec", "8.Feb", "8.Jan", "8.Jul", 
  "8.Jun", "8.Mar", "8.May", "8.Nov", "8.Oct", "8.Sep", "9.Apr", 
  "9.Aug", "9.Dec", "9.Feb", "9.Jan", "9.Jul", "9.Jun", "9.Mar", 
  "9.May", "9.Nov", "9.Oct", "9.Sep"), class = "factor"), variable = 
  structure(1:10, .Label = c("LongNetEquity", 
  "LongTotalEquity", "ShortNetEquity", "ShortTotalEquity", "NetEquity", 
  "NetTotalEquity", "GrossNetEquity", "TotalNetEquity", "NetEquityPerfAttr", 
  "GrossTotalEquityPerfAttr"), class = "factor"), value = c(88, 
  92.3, -28.1, -29.3, 58.8, 63, 116.1, 121.6, -1.2, -1.2), Criteria = c("Above 
  70", "Above 70", "Below 45", "Below 45", "Between 45 & 60", "Between 60 & 
  70","Above 70", "Above 70", "Below 45", "Below 45")), .Names = c("Date", 
  "variable", "value", "Criteria"), row.names = c(1L, 109L, 217L, 
  325L, 433L, 541L, 649L, 757L, 865L, 973L), class = "data.frame")

data newtable

newtable <- structure(list(Strategy = structure(c(3L, 4L, 8L, 9L, 5L, 7L, 
1L, 10L, 6L, 2L), .Label = c("GrossNetEquity", "GrossTotalEquityPerfAttr", 
"LongNetEquity", "LongTotalEquity", "NetEquity", "NetEquityPerfAttr", 
"NetTotalEquity", "ShortNetEquity", "ShortTotalEquity", "TotalNetEquity"
), class = "factor"), median_era = c(77.35, 82.65, -34.1, -35.55, 
41.45, 46.35, 114.2, 119.8, 1.25, 1.4), mean_era = c(77.74, 84.51, 
-34.42, -36.44, 43.26, 48.07, 112.2, 121, 1.051, 1.148)), .Names = c("Strategy", 
"median_era", "mean_era"), row.names = c(NA, -10L), class = "data.frame")
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
gurtej
  • 41
  • 5
  • 2
    What does your data look like? Make sure to share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run and see the code. It seems you might have multiple `value` values for each `variable`. If you don't want to sum them, it's unclear how you want to combine them. – MrFlick Aug 09 '18 at 16:33
  • Some ideas [here](https://stackoverflow.com/questions/43997754/ggplot2-bar-plot-order-fill-as-in-data?noredirect=1&lq=1). Using the `group` aesthetic might help. – aosmith Aug 09 '18 at 16:48
  • I added my dataset as advised @MrFlick – gurtej Aug 09 '18 at 17:07
  • @aosmith i try using group function but values are coming like 1 to 300 where as in my data there is no value above 150 – gurtej Aug 09 '18 at 18:06
  • @MrFlick I shared data of era and thats the table I m concerned at this point. Requirements for graph are take all the values and show bar graph based on that and then give different color to the bar graph based on criteria. But my property is i m getting graph on cumulative basis . – gurtej Aug 09 '18 at 18:35
  • If i take out all the `geom_point` parts that wont work, I get a plot with the values you expect: https://i.stack.imgur.com/OTWsh.png So there must be something going on other that what you've shown us. – MrFlick Aug 09 '18 at 18:38
  • I'm not certain I understand the desired output, but using `group = row.names(era)` seems to plot each observation in a stack in the order of the dataset instead of grouping all the `fill` stacks together. – aosmith Aug 09 '18 at 18:44
  • @MrFlick I added other two datasets. In my data lastegest value is not more than 150 not sure why you are getting 300 – gurtej Aug 09 '18 at 18:46
  • @aosmith I am trying to get exact values in a graph and apply conditions to them i.e. if i have 1,2,3,4 values then i want bar graph to be on 1,2,3,4 axis rather than accumulated to 10 axis and then apply condition on that axis. I hope I able to clarify the requirements. Thank you for all your help – gurtej Aug 09 '18 at 18:51
  • Well, for example, your dataset has three values for GrossNetEquity (see `subset(era, variable=="GrossNetEquity")`): 117,2, 114.4, 106.5. They all have the same criteria ("Above 70"). Since they are all in the same group, R adds those values together to get a single value. What else would you like to do with them? At the end of the day, the bar can only have one height. I'm not really sure what you are expecting. – MrFlick Aug 09 '18 at 18:51
  • @MrFlick i want to take all the historical values for each variable and then create bar graph from minimum to maximum value and give different colors depending upon condition i.e. > 45 - yellow, 45-60 - blue, 60-70-orange,<70 red. and then show median,mean and current value in it. So like in GrossNetEquity i need graph from 106 to 117 only and then add mean,median and current value in it. – gurtej Aug 09 '18 at 19:05
  • @aosmith Thank you for all your help. Actually group worked with position = dodge. Problem is solved. – gurtej Aug 09 '18 at 19:51

1 Answers1

0
ggplot() +
  geom_bar(data = era, aes(x=variable, y=value,fill =Criteria, group= variable),stat="identity",position = "dodge",alpha = 0.5, width =0.5) +
  scale_fill_manual(values=c("red3","orange3","green3","yellow3")) +
  geom_point(data = newtable, aes(x=Strategy,y=median_era,group=1),shape = "|", size = 5,color ="brown1")+
  geom_text(data = newtable, aes(x=Strategy, y=median_era, label = paste("Median",median_era)), 
            size = 3, vjust = 2, hjust = -0.5,color = "brown1")+
  geom_point(data = newtable, aes(x=Strategy,y=mean_era,group=1),shape = "|", size = 5,color = "coral4")+
  geom_text(data = newtable, aes(x=Strategy, y=mean_era, label = paste("Mean",mean_era)), 
            size = 3, vjust = 4, hjust = -0.5,color ="coral4")+
  geom_point(data = searches, aes(x=Strategy,y=value,group=2),shape = "|", size = 5,color = "darkblue")+
  geom_text(data = searches, aes(x=Strategy, y=value, label = paste("Current Value",value)), 
            size = 3, vjust = 0, hjust = 0.25,color = "darkblue")+
  coord_flip()+
  theme(axis.text.x=element_blank())
gurtej
  • 41
  • 5