各个条上的数字对齐 [英] Alignment of numbers on the individual bars

查看:93
本文介绍了各个条上的数字对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将标签放在ggplot的小节上方.我曾经使用发现的方法( HERE ),但是自从我更新ggplot2以来,它似乎不再起作用,因为我现在收到错误消息:

Error in continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept",  : 
  unused argument(s) (formatter = "percent")

使用示例时,如何再次在小节上方绘制数字值:

df <- structure(list(A = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L), .Label = c("0-50,000", "50,001-250,000", "250,001-Over"), class = "factor"),
    B = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0-50,000",
    "50,001-250,000", "250,001-Over"), class = "factor"), Freq = c(0.507713884992987,
    0.258064516129032, 0.23422159887798, 0.168539325842697, 0.525280898876405,
    0.306179775280899, 0.160958904109589, 0.243150684931507,
    0.595890410958904)), .Names = c("A", "B", "Freq"), class = "data.frame", row.names = c(NA,
-9L))

library(ggplot2)

ggplot(data=df, aes(x=A, y=Freq))+
    geom_bar(aes(fill=B), position = position_dodge()) + 
    geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""),
                  y = Freq+0.015, x=A),
              size = 3, position = position_dodge(width=0.9)) +
    scale_y_continuous(formatter = "percent") +
    theme_bw()

在Win 7机器上运行R 2.15 ggplot2 0.9

解决方案

该错误来自scale_y_continuous调用.标签的格式现在由labels参数处理.有关更多详细信息,请参见 ggplot2 0.9.0过渡指南. >

还有另一个问题,即标签未正确排列;我通过在geom_text的美感上添加group=B来解决此问题;不过,我不太确定为什么要这样做.我还从geom_text美学中删除了x=A,因为它不是必需的(它将从ggplot调用中继承.

library("ggplot2")
library("scales")

ggplot(data=df, aes(x=A, y=Freq))+
    geom_bar(aes(fill=B), position = position_dodge()) + 
    geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""),
                  y = Freq+0.015, group=B),
              size = 3, position = position_dodge(width=0.9)) +
    scale_y_continuous(labels = percent) +
    theme_bw()

I have the need to place labels above bars on ggplot. I used to use the method found (HERE) but this does not appear to work anymore since my ggplot2 update as I now get the error message:

Error in continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept",  : 
  unused argument(s) (formatter = "percent")

How can I again plot numeric values above the bars when using the example:

df <- structure(list(A = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L), .Label = c("0-50,000", "50,001-250,000", "250,001-Over"), class = "factor"),
    B = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0-50,000",
    "50,001-250,000", "250,001-Over"), class = "factor"), Freq = c(0.507713884992987,
    0.258064516129032, 0.23422159887798, 0.168539325842697, 0.525280898876405,
    0.306179775280899, 0.160958904109589, 0.243150684931507,
    0.595890410958904)), .Names = c("A", "B", "Freq"), class = "data.frame", row.names = c(NA,
-9L))

library(ggplot2)

ggplot(data=df, aes(x=A, y=Freq))+
    geom_bar(aes(fill=B), position = position_dodge()) + 
    geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""),
                  y = Freq+0.015, x=A),
              size = 3, position = position_dodge(width=0.9)) +
    scale_y_continuous(formatter = "percent") +
    theme_bw()

Running R 2.15 ggplot2 0.9 on a win 7 machine

解决方案

The error is from the scale_y_continuous call. Formatting of labels is now handled by the labels argument. See the ggplot2 0.9.0 transition guide for more details.

There was another problem with the labels not lining up correctly; I fixed that by adding a group=B to the aesthetics for the geom_text; I'm not quite sure why this is necessary, though. I also took out x=A from the geom_text aesthetics because it was not needed (it would be inherited from the ggplot call.

library("ggplot2")
library("scales")

ggplot(data=df, aes(x=A, y=Freq))+
    geom_bar(aes(fill=B), position = position_dodge()) + 
    geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""),
                  y = Freq+0.015, group=B),
              size = 3, position = position_dodge(width=0.9)) +
    scale_y_continuous(labels = percent) +
    theme_bw()

这篇关于各个条上的数字对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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