Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计数或者..密度。。作为ggplot2中的变量名_R_Ggplot2_Histogram_Density Plot - Fatal编程技术网

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)