[英]using an apply function with ggplot2 to create bar plots for more than one variable in a data.frame
有沒有一種方法可以在R中使用apply函數來用ggplot2
創建ggplot2
?
假設我們有一個僅包含因子變量的數據框,其中一個是布爾值。 就我而言,我有一個包含+40個變量的日期框架。 可以用一行代碼將所有變量與布爾值相對應嗎?
data("diamonds")
factors <- sapply(diamonds, function(x) is.factor(x))
factors_only <- diamonds[,factors]
factors_only$binary <- sample(c(1, 0), length(factors_only), replace=TRUE)
factors_only$binary <- as.factor(factors_only$binary)
但是我想創建像這樣的條形圖:
qplot(factors_only$color, data=factors_only, geom="bar", fill=factors_only$binary)
這不起作用:
sapply(factors_only,function(x) qplot(x, data=factors_only, geom="bar", fill=binary))
請指教
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.