r - 向ggplot条形图添加标签

标签 r ggplot2

我想做一个用黑色勾勒出的条形图,条形内有百分比。这可能来自qplot吗?我得到了显示的百分比,但它们与特定的条不对齐。

包:ggplot2, reshape

created in Illustrator

x <- data.frame(filename = c("file1", "file2", "file3", "file4"),
                    low = c(-.05,.06,.07,-.14),
                    hi = c(.87,.98,.56,.79))
x$tot <- x$hi + x$low

x <- melt(x, id = 'filename')

bar <- qplot(x = factor(filename), 
             y = value*100,
             fill = factor(variable),
             data = x,
             geom = 'bar',
             position = 'dodge') + coord_flip()
bar <- bar + scale_fill_manual(name = '',
                               labels = c('low',
                                          'Hi',
                                          "Tot"),
                               values = c('#40E0D0',
                                          '#FF6347',
                                          "#C7C7C7")) 
bar <- bar + geom_text(aes(label = value*100))+geom_bar(colour = 'black')
bar <- bar + opts(panel.background = theme_rect(colour = NA))
bar <- bar + opts(legend.justification = 'bottom')
print(bar)

最佳答案

干得好:

library(scales)
ggplot(x, aes(x = filename, fill = variable)) +
  geom_bar(stat="identity", ymin=0, aes(y=value, ymax=value), position="dodge") +
  geom_text(aes(x=filename, y=value, ymax=value, label=value, 
                hjust=ifelse(sign(value)>0, 1, 0)), 
            position = position_dodge(width=1)) +
  scale_y_continuous(labels = percent_format()) +
  coord_flip()

enter image description here

关于r - 向ggplot条形图添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11653268/

相关文章:

r - 更新下拉列表 Shiny

r - 如何避免在 R data.table fwrite 1.9.7 中将日期转换为 idate?

r - ggplot2 传递变量以绘制函数

R ggplot 从图例中删除某些项目

r - 如何更改 ggplot2 中的默认字体大小 - 包括 geom_text

r - ggplot2的默认字体是什么

r - 在没有公共(public)变量的情况下执行 dplyr full_join 以混合数据帧

r - R中DataTable中的条件格式单元格

r - 轴标签放置

r - 正确扩展 ggplot2 吗?