繁体   English   中英

ggplot 中的标签从 R 中的表格中躲避条形图

[英]labels in ggplot dodged barchart from table in R

我有下表和代码:

tt = structure(c(7L, 13L, 24L, 30L, 30L, 38L, 35L, 45L, 37L, 
43L, 38L, 59L, 33L, 45L, 37L, 58L), .Dim = c(2L, 8L), .Dimnames = structure(list(param = c("A", 
"B"), xvar = c("5", "6", "7", "8", "9", "10", "11", "12")), .Names = c("param", "xvar")), class = "table")
tt
     xvar
param  5  6  7  8  9 10 11 12
    A  7 24 30 35 37 38 33 37
    B 13 30 38 45 43 59 45 58

dd = data.frame(tt)
ggplot(dd, aes(xvar, Freq, fill=param))+ 
   geom_bar(stat='identity',position='dodge')+
   geom_text(aes(label=Freq), position='dodge' , vjust=2)

在此处输入图片说明

但价值观并没有被“回避”而留在中心。 如何将它们正确放置在酒吧上? 谢谢你的帮助。

使用position=position_dodge(...)而不是position="dodge"

dd <- as.data.frame(tt)
ggplot(dd, aes(xvar, Freq, fill=param))+ 
  geom_bar(stat='identity',position='dodge')+
  geom_text(aes(label=Freq), position=position_dodge(width=.9),vjust=2)

上面的答案很好,但只有在 ggplot 调用中有fill=param时它才会起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM