如何创建没有累积值的geom_bar图

时间:2018-08-09 16:17:01

标签: r ggplot2

类似这样的东西

enter image description here

我正在尝试制作堆叠的条形图,但不希望累积值

 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()

我的输出vs期望的输出 enter image description here

这是我的数据集

数据时代

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))

数据搜索

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")

数据新表

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")

1 个答案:

答案 0 :(得分: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())