Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 带有ggplot2的堆叠条形图标签_R_Ggplot2 - Fatal编程技术网

R 带有ggplot2的堆叠条形图标签

R 带有ggplot2的堆叠条形图标签,r,ggplot2,R,Ggplot2,我正在尝试绘制以下数据: 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,

我正在尝试绘制以下数据:

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")
但是它

  • 对图形没有任何影响
  • 如果有,可能不会显示百分比(尽管我不完全确定这一点)

  • 非常感谢您的帮助。谢谢

    文本的y坐标是实际计数(2、25或28),而绘图面板中的y坐标范围为0到1,因此文本从顶部打印

    使用
    ddply
    (或
    tapply
    或其他方法)计算计数分数


    谢谢你的回复。我目前在你的代码中遇到了这个错误:do.call(“layer”,list(mapping=mapping,data=data,stat=stat,:object'Count'找不到有意义的,但由于某种原因,当我进行合并时,它基本上是在数据上进行外部连接。我得到结构(list)(Teacher=c(“BS”,“BS”,“BS”,“BS”,“BS”,“FA”),Level=structure(c(2L,2L,1L,1L,1L),.Label=c(“BE”,“AE”,“ME”,“EE”),class=“factor”),Count=c(2L,2L,25L,25L,28L),Count.Fraction=c(0.0740741,0.925925925925926,0.0740741,0.9259259225926,1)),name=c(“教师”,“级别”,“计数”,“计数”,“计数分数”),row.Names=c(NA,-5L),class=“数据框”)我尝试通过=“Teacher”添加但是运气不好…有什么想法吗?谢谢!因此,我最终使用了cbind,而不是只合并Count.Fraction列。这将以正确的格式获取数据,用于我的问题措辞。然后,我将标签更改为Count.Percentage列,以获取百分比而不是计数,正如我的问题中所述。感谢您的帮助!
    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")
    
    graph_avgs <- ddply(
      to_graph, 
      .(Teacher), 
      summarise, 
      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
      )