[英]How to pass variable into ggplot aes / ..y.. part of function
我正在查看將transform
傳遞到下面的函數中,以便能夠對齊輔助軸...但是,出現錯誤:
Error in y * transform : non-numeric argument to binary operator
library(ggplot2)
a.samples <- rbeta(10000, 25, 75)
b.samples <- rbeta(10000, 35, 65)
df <- data.frame(x = b.samples/a.samples)
p <- ggplot(df, aes(x = x)) + geom_histogram(fill = 'grey50', alpha = .6)
transform <- max(ggplot_build(p)$data[[1]]$ymax)
p + geom_line(aes(y = ..y.. * transform), stat='ecdf') + # error here
geom_vline(xintercept = 1, linetype = 'dashed') +
scale_y_continuous(sec.axis = sec_axis(~./transform, name = "ecdf")) +
guides(color = FALSE)
如果我將值1260
明確寫入aes參數,它將起作用。
...geom_line(aes(y = ..y.. * 1260), stat='ecdf')...
能夠aes_string
與aes_string
一起aes_string
p + geom_line(aes_string(y = paste0('..y.. * ', transform)), stat='ecdf') +
geom_vline(xintercept = 1, linetype = 'dashed') +
scale_y_continuous(sec.axis = sec_axis(~./transform, name = "ecdf")) +
guides(color = FALSE)
因此,您上面的代碼對我來說很奇怪,除了binwidths關閉以外,沒有任何其他錯誤,所以我不知道是什么產生了錯誤,但是我只是通過添加stat_ecdf()進行了調整
library(ggplot2)
a.samples <- rbeta(10000, 25, 75)
b.samples <- rbeta(10000, 35, 65)
df <- data.frame(x = b.samples/a.samples)
p <- ggplot(df, aes(x = x)) + geom_histogram(fill = 'grey50', alpha = .6)
transform <- max(ggplot_build(p)$data[[1]]$ymax)
transform
p + stat_ecdf(aes(y = ..y.. * transform),geom = "line") +
geom_vline(xintercept = 1, linetype = 'dashed') +
scale_y_continuous(sec.axis = sec_axis(~./transform, name = "ecdf")) +
guides(color = FALSE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.