gpt4 book ai didi

r - 各个条上的数字对齐

转载 作者:行者123 更新时间:2023-12-04 03:30:01 25 4
gpt4 key购买 nike

我需要在 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 transition guide了解更多详情。

标签没有正确排列还有另一个问题;我通过添加 group=B 解决了这个问题到 geom_text 的美学;不过,我不太确定为什么这是必要的。我也拿出了x=A来自 geom_text美学,因为它不需要(它将继承自 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()

enter image description here

关于r - 各个条上的数字对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com