在躲避条形图上定位 geom_text [英] Position geom_text on dodged barplot

查看:26
本文介绍了在躲避条形图上定位 geom_text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让标题不言自明,但这里是 - 数据优先:

dtf <-结构(列表(变量=结构(c(1L,1L,2L,2L,3L,3L,4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"),ustanova = 结构(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,2L), .Label = c("srednja škola", "fakultet"), class = "factor"),`(全部)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139,33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692,23.9426605504587, 33.2200772200772, 26.94930875576​​04)), .Names = c("变量","ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df","data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(结构(列表(变量=结构(c(1L,1L,2L,2L,3L,3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma","fla"), class = "factor"), ustanova = structure(c(1L, 2L,1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola","fakultet"), class = "factor")), .Names = c("variable", "ustanova"), row.names = c("vma_srednja škola", "vma_fakultet", "vla_srednja škola","vla_fakultet", "ia_srednja škola", "ia_fakultet", "fma_srednja škola","fma_fakultet", "fla_srednja škola", "fla_fakultet"), class = "data.frame"),结构(list(value = structure(1L, .Label = "(all)", class = "factor")), .Names = "value", row.names = "(all)", class = "data.frame")))

我想创建一个躲避的条形图,执行 coord_flip 并在条形图中放置一些文本标签:

ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") +geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = "dodge") +坐标翻转()

您可以在提供了对该主题的更全面的描述.

I tried to make the title self-explanatory, but here goes - data first:

dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"), 
    ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L), .Label = c("srednja škola", "fakultet"), class = "factor"), 
    `(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139, 
    33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692, 
    23.9426605504587, 33.2200772200772, 26.9493087557604)), .Names = c("variable", 
"ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df", 
"data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(
    structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 
    3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", 
    "fla"), class = "factor"), ustanova = structure(c(1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola", 
    "fakultet"), class = "factor")), .Names = c("variable", "ustanova"
    ), row.names = c("vma_srednja škola", "vma_fakultet", "vla_srednja škola", 
    "vla_fakultet", "ia_srednja škola", "ia_fakultet", "fma_srednja škola", 
    "fma_fakultet", "fla_srednja škola", "fla_fakultet"), class = "data.frame"), 
    structure(list(value = structure(1L, .Label = "(all)", class = "factor")), .Names = "value", row.names = "(all)", class = "data.frame")))

And I'd like to create a dodged barplot, do the coord_flip and put some text labels inside the bars:

ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
 geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = "dodge") +
 coord_flip()

you can see output here.

I reckon I'm asking for something trivial. I want the text labels to "follow" stacked bars. Labels are placed correctly on the y-axis, but how to position them correctly on x-axis?

解决方案

Is this what you want?

library(ggplot2)

ggplot(bar) + 
  geom_col(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
  geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`), group = ustanova), 
            position = position_dodge(width = .9)) +
  coord_flip()

The key is to position = position_dodge(width = .9) (where .9 is the default width of the bars) instead of position = "dodge", which is just a shortcut without any parameter. Additionally you have to set the group=ustanova aesthetic in geom_text to dodge the labels by ustanova (A second option would be to make fill = ustanova a global aesthetic via ggplot(bar, aes(fill = ustanova))


In ggplot2_2.0.0 you find several examples in ?geom_text on how to position geom_text on dodged or stacked bars (the code chunk named "# Aligning labels and bars"). The Q&A What is the width argument in position_dodge? provides a more thorough description of the topic.

这篇关于在躲避条形图上定位 geom_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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