将geom_text放在躲避的条形图上我试图使标题不言自明,但这里 - 数据优先: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")))我想创建一个躲避的条形图,并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") +
coord_flip()你可以在这里看到输出。我估计我要求一些微不足道的东西。我希望文本标签“跟随”堆叠条。标签正确放置在y轴上,但如何在x轴上正确定位?
2 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
这是你想要的吗?
ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") + geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = position_dodge(width = 1)) + coord_flip()
关键是使用position = position_dodge(width = 1)
而不是position = "dodge"
,这只是一个没有任何参数的快捷方式。
在ggplot2_2.0.0
你找到几个?geom_text
关于如何定位geom_text
在躲闪或堆叠条形图上的例子(代码块命名为“ # Aligning labels and bars"
)。问答&a position_dodge中的width参数是什么?提供了对该主题的更全面的描述。
- 2 回答
- 0 关注
- 764 浏览
添加回答
举报
0/150
提交
取消