如何用ggplot2标记具有正面和负面酒吧的barplot酒吧 [英] How to label a barplot bar with positive and negative bars with ggplot2

查看:790
本文介绍了如何用ggplot2标记具有正面和负面酒吧的barplot酒吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2绘制带有正面和负面栏的带标签的barplot。
这工作到目前为止,但我想在酒吧外面设置标签,以便它位于酒吧上方或下方。我尝试在 vjust = c(x1,...,xn)中设置一个调整,其中 x 是一个正数或根据 geom_text()中的栏的值设置负值。这不起作用。我刚收到错误消息错误:当设置美学时,它们可能只有一个值。问题:vjust



命令的作品。我想在ggplot2中复制这个命令:

  xpos < -  barplot(d,col = mycols,main ='Verteilung in Dresden 2004',
ylab ='Anteil in%',xlab ='Milieu',names.arg = l,
cex.axis = 0.7,cex.names = 0.7,ylim = c(0, max(d)+0.05))
boxed.labels(xpos,d + 0.02,sprintf('%d%s',d * 100,'%'),
bg ='transparent', border = FALSE,cex = 0.7)

- )



有人有有什么建议吗?



感谢您的帮助。

解决方案

这就是诀窍

pre $ library $
library $ g
$ b $ library $ $ b dtf < - data.frame(x = c(ETB,PMA,PER,KON,TRA,
DDR,BUM,MAT HED,EXP),
y = c(.02,.11,-.01,-.03,-.03,.02,.1, - 。 01,-.02,0.06))
ggplot(dtf,aes(x,y))+
geom_bar(stat =identity,aes(fill = x),legend = FALSE)+
geom_text(aes(label = paste(y * 100,%),
vjust = ifelse(y> = 0,0,1)))+
scale_y_continuous(Anteil in Prozent,labels = percent_format())+
opts(axis.title.x = theme_blank())


I'm trying to plot a labeled barplot with ggplot2 with positive and negative bars. That works so far, but I would like to set the label outside of the bar, so that it is above or under the bar. I tried to set an adjustment in vjust = c(x1,...,xn) where x is a positive or negative value according to the value of the bar in geom_text(). This doesn't work. I just got the Error message Error: "When setting aesthetics, they may only take one value. Problems: vjust"

With the normal plot command that works. I want to replicate this command in ggplot2:

xpos <- barplot(d, col=mycols, main='Verteilung in Dresden 2004',
         ylab='Anteil in %', xlab='Milieu', names.arg=l, 
         cex.axis=0.7, cex.names=0.7, ylim=c(0,max(d)+0.05))
boxed.labels(xpos,d+0.02,sprintf('%d%s', d*100, '%'),
          bg='transparent', border=FALSE, cex=0.7)

So that it looks like this just in nice... ;-)

Does someone have any suggestions?

Thank's for y'all help.

解决方案

This does the trick

library(plyr)
library(ggplot2)
library(scales)
dtf <- data.frame(x = c("ETB", "PMA", "PER", "KON", "TRA", 
                  "DDR", "BUM", "MAT", "HED", "EXP"),
                  y = c(.02, .11, -.01, -.03, -.03, .02, .1, -.01, -.02, 0.06))
ggplot(dtf, aes(x, y)) +
  geom_bar(stat = "identity", aes(fill = x), legend = FALSE) + 
  geom_text(aes(label = paste(y * 100, "%"),
               vjust = ifelse(y >= 0, 0, 1))) +
  scale_y_continuous("Anteil in Prozent", labels = percent_format()) +
  opts(axis.title.x = theme_blank())

这篇关于如何用ggplot2标记具有正面和负面酒吧的barplot酒吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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