用ggplot2堆积的条形图标签 [英] Stacked Bar Graph Labels with ggplot2

查看:168
本文介绍了用ggplot2堆积的条形图标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  to_graph<  -  structure(list(Teacher = c(BS ,BS,FA
),等级=结构(c(2L,1L,1L),。标签= c(BE,AE,ME,
(2L,25L,28L)),.Names = c(Teacher,
Level,Count),row.names = c (NA,3L),class =data.frame)

并且想要在那件作品的百分比中的每一条的中间部分。基于这篇文章,我想出了:

  ggplot(data = to_graph,aes(x = Teacher,y = Count,fill = Level),ordered = TRUE)+ 
geom_bar(aes(fill = Level),position ='fill')+
opts(axis.text.x = theme_text(angle = 45))+
scale_y_continuous(,formatter =%)+
opts(title =Score Distribution)+
scale_fill_manual(values = c(#FF0000,#FFFF00,#00CC00,#0000FF) )+
geom_text(aes(label = Count),size = 3,hjust = 0.5,vjust = 3,position =stack)

但它


  1. 没有任何效果图表

  2. 可能不会显示百分比(尽管我并不完全确定这一点)

非常感谢任何帮助。谢谢!

解决方案

文本的y坐标是实际的计数(2,25或28),而y-

使用 ddply来计算计数的比例 / code>(或 tapply 或其他)。

  graph_avgs < -  ddply(
to_graph,
。(Teacher),
summary,
Count.Fraction = Count / sum(Count)


to_graph< - cbind(to_graph,graph_avgs $ Count.Fraction)

简化版你的情节。我没有打扰过因子订单,所以数字匹配到吧。

  ggplot(to_graph,aes (Teacher),ordered = TRUE)+ 
geom_bar(aes(y = Count,fill = Level),position ='fill')+
scale_fill_manual(values = c(#FF0000,# FFFF00,#00CC00,#0000FF))+
geom_text(
aes(y = graph_avgs $ Count.Fraction,label = graph_avgs $ Count.Fraction),
size = 3


I am trying to graph the following data:

to_graph <- structure(list(Teacher = c("BS", "BS", "FA"
), Level = structure(c(2L, 1L, 1L), .Label = c("BE", "AE", "ME", 
"EE"), class = "factor"), Count = c(2L, 25L, 28L)), .Names = c("Teacher", 
"Level", "Count"), row.names = c(NA, 3L), class = "data.frame")

and want to add labels in the middle of each piece of the bars that are the percentage for that piece. Based on this post, I came up with:

ggplot(data=to_graph, aes(x=Teacher, y=Count, fill=Level), ordered=TRUE) + 
    geom_bar(aes(fill = Level), position = 'fill') + 
    opts(axis.text.x=theme_text(angle=45)) + 
    scale_y_continuous("",formatter="percent") + 
    opts(title = "Score Distribution") + 
    scale_fill_manual(values = c("#FF0000", "#FFFF00","#00CC00", "#0000FF")) + 
    geom_text(aes(label = Count), size = 3, hjust = 0.5, vjust = 3, position = "stack")

But it

  1. Doesn't have any effect on the graph
  2. Probably doesn't display the percentage if it did (although I'm not entirely sure of this point)

Any help is greatly appreciated. Thanks!

解决方案

The y-coordinate of the text is the actual count (2, 25 or 28), whereas the y-coordinates in the plot panel range from 0 to 1, so the text is being printed off the top.

Calculate the fraction of counts using ddply (or tapply or whatever).

graph_avgs <- ddply(
  to_graph, 
  .(Teacher), 
  summarise, 
  Count.Fraction = Count / sum(Count)
)

to_graph <- cbind(to_graph, graph_avgs$Count.Fraction)

A simplified version of your plot. I haven't bothered to play about with factor orders so the numbers match up to the bars yet.

ggplot(to_graph, aes(Teacher), ordered = TRUE) + 
  geom_bar(aes(y = Count, fill = Level), position = 'fill') + 
  scale_fill_manual(values = c("#FF0000", "#FFFF00","#00CC00", "#0000FF")) + 
  geom_text(
    aes(y = graph_avgs$Count.Fraction, label = graph_avgs$Count.Fraction),  
    size = 3
  )

这篇关于用ggplot2堆积的条形图标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆