R 计数或者..密度。。作为ggplot2中的变量名
我希望最终做到这一点:R 计数或者..密度。。作为ggplot2中的变量名,r,ggplot2,histogram,density-plot,R,Ggplot2,Histogram,Density Plot,我希望最终做到这一点: library(ggplot2) density=TRUE if (density) {ggplot(diamonds,aes(x=price)) + geom_histogram(aes(y=..density..),binwidth=1000) + facet_wrap(~color) } else ggplot(diamonds,aes(x=price)) + geom_histogram(aes(y=..count..),binwidth=1000)
library(ggplot2)
density=TRUE
if (density) {ggplot(diamonds,aes(x=price)) + geom_histogram(aes(y=..density..),binwidth=1000) + facet_wrap(~color)
} else ggplot(diamonds,aes(x=price)) + geom_histogram(aes(y=..count..),binwidth=1000) + facet_wrap(~color)
但我想为“.count..”或“.density..”使用变量名。我尝试了几件事,但都失败了:
if (density) {y.scale ="..density.."} else y.scale ="..count.."
ggplot(diamonds,aes(x=price)) + geom_histogram(aes(y=get(y.scale)),binwidth=1000) + facet_wrap(~color)
ggplot(diamonds,aes(x=price)) + geom_histogram(aes(as.formula(paste("y=",y.scale))),binwidth=1000) + facet_wrap(~color)
你知道如何将变量名传递到aes()中吗?我想你是在寻找
aes\u string()
而不是aes()
:
i、 e
没错,谢谢@Chase。下面的代码工作得很好:
ggplot(菱形,aes(x=price))+geom\u直方图(aes\u字符串(y=y.scale),binwidth=1000)+facet\u wrap(~color)
foo <- "..density.."
ggplot(diamonds, aes(price)) +
geom_histogram(aes_string(y = foo),binwidth=1000) +
facet_wrap(~color)