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

问题描述 投票:0回答:1

我已经尝试过堆叠条形图,并希望获得真实值,而不是将条形图放置在另一个上。例如,如果空载时的交流能量消耗为193,空载时的非交流能量消耗为175,那么我希望有一个图显示相同的值,而不是一个叠加在另一个上,y轴值大约为350我不想要这种类型的图:enter image description 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"   
)

这就是我尝试过的:

ggplot(a,aes(x=para,y=energy))+
  geom_bar(stat="identity", aes(fill=type))
r ggplot2 geom-bar stacked-chart
1个回答
0
投票

这将解决问题:

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

或者如果您想要一个堆叠的图表,并在图表中显示其中的值,则这样:

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))
© www.soinside.com 2019 - 2024. All rights reserved.