简体   繁体   English

在 r 中使用 ggplot 在 y 轴上具有实际值的堆叠条形图

[英]Stacked bar chart with real values in y-axis using ggplot in r

I have tried stacked bar chart and wanted to get the real values, instead of placing bars one over the other.我尝试过堆叠条形图并希望获得实际值,而不是将条形放在另一个上。 for example if Ac energy consumption is 193 at empty load and 175 in non ac energy consumption at empty load, then i desired to have a plot that shows the same values instead of piling one over the other where the y axis values goes to around 350. i dont want this type of graph:例如,如果空载时的交流能耗为 193,空载时的非交流能耗为 175,那么我希望有一个 plot 显示相同的值,而不是在 y 轴值达到 350 左右的情况下将一个堆放在另一个上.我不想要这种类型的图表: 在此处输入图像描述

The sample data is here:样本数据在这里:

a<-tribble(
~"para",       ~"energy", ~"type",

  "Empty_Load",    175, "NoAC",   
 "Half_Load"  ,   184, "NoAC",     
 "Full_Load"   ,  191, "NoAC",    
 "Empty_Load"   , 193, "AC",       
 "Half_Load"     ,206, "AC",       
 "Full_Load"     ,211, "AC"   
)

and this is what i tried:这就是我尝试过的:

ggplot(a,aes(x=para,y=energy))+
  geom_bar(stat="identity", aes(fill=type))

This will do the trick:这可以解决问题:

ggplot(a,aes(x=para,y=energy))+
  geom_bar(stat="identity", aes(fill=type), position ="dodge") #add position = "dodge" to place bars next to each other

Or like this if you want a stacked chart with the values in presented in the graph:或者像这样,如果你想要一个堆叠图表,其中的值显示在图表中:

ggplot(a,aes(x=para,y=energy))+
  geom_bar(stat="identity", aes(fill=type)) + #add position = "dodge" to place bars next to each other
  geom_text(aes(label = energy), position = position_stack(vjust = 0.5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM