标出躲闪的条形图 [英] label a dodged bar chart

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

问题描述

也许这是因为外面黑暗,但我无法得到这个



将geom_text定位于躲闪的条形图



用于处理我相当简单的数据框

$ b (产品类别= c(c2,c2),产品= c(p4,p5), ),ms1 = c(2,1))

plot < - ggplot(data = NULL)
plot +
geom_bar(data = fs,aes(x = productcategory ,y = ms1,weight = ms1,fill = product),stat =identity,position =dodge)+
geom_text(data = fs,aes(label = ms1,x = productcategory,y = ms1 +0.2),position = position_dodge(width = 1)))

产品类别的中间,而不是正确的产品。



看起来像这样,即使它看起来很简单,但我完全停留在这个

p>

所以任何提示都非常赞赏如何将标签放在prope上面r bar。



Tom

解决方案

对于每个geom而言, geom_text 并不意味着您将x变量细分为 productcategory 填充变量 product

您可以通过将 fill = product 添加到 aes( )调用 geom_text ,或者您可以尝试在原始 ggplot()中定义尽可能多的美学。 code> call,这样所有的geoms都会自动选择这些美学,并且只需要定义它们,只要它们特定于特定的geom就可以。

  plot2 < -  ggplot(data = fs,aes(x = productcategory,y = ms1,fill = product))+ 
geom_bar(stat =identity,position = (标记= ms1,y = ms1 + 0.2),position = position_dodge(width = 1))
print(plot2)


Maybe it's because of the dark outside, but I can't get this

Position geom_text on dodged barplot

to work on my fairly simple dataframe

fs <- data.frame(productcategory=c("c2","c2"), product=c("p4", "p5"), ms1=c(2,1))

plot <- ggplot(data=NULL)
plot + 
    geom_bar(data=fs, aes(x=productcategory, y=ms1, weight=ms1, fill=product),stat="identity", position="dodge") + 
    geom_text(data=fs, aes(label = ms1,  x = productcategory, y=ms1+0.2), position=position_dodge(width=1)))

My plot still shows the labels in the "middle" of the product category and not above of the proper product.

Looks like this even it seems very simple, but I'm totally stuck on this

So any hints are very much appreciated how to get labels above the proper bars.

Tom

解决方案

Because you have the aesthetics defined for each geom individually, geom_text isn't picking up on the fact that you're subdividing the x variable productcategory by the fill variable product.

You can get the graph you want by adding fill=product to the aes() call for geom_text, or you can try to define as many aesthetics as possible in the original ggplot() call, so that all the geoms pick up on those aesthetics automatically and you only have to define them if they're specific to that particular geom.

plot2 <- ggplot(data=fs, aes(x=productcategory, y=ms1, fill=product)) +
  geom_bar(stat="identity", position="dodge") +
  geom_text(aes(label=ms1, y =ms1 + 0.2), position=position_dodge(width=1))
print(plot2)

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

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