【问题标题】:Adding values from one data set to the ggplots (with several variables)将一个数据集中的值添加到 ggplots(带有多个变量)
【发布时间】:2017-06-17 13:59:12
【问题描述】:

通过使用这个函数,我可以将异常值添加到 mpg 的图中

outlier_values. <- lapply(mtcars[-c(8,9)], function(x){outlier_values <- boxplot.stats(x)$out})
boxplot(mtcars$mpg, main="Pressure Height", boxwex=0.1)
mtext(paste("Outliers: ", paste(outlier_values., collapse=", ")), cex=0.6)

现在购买我想将异常值(outlier1) 添加到所有变量的图中:

library(reshape2)
library(ggplot2)

outlier <- do.call("cbind", lapply(mtcars[-c(8,9)], function(x) boxplot.stats(x)$out))
outlier1 <- melt(outlier)

mtcars_m = melt(mtcars[,-c(8,9)])
names(mtcars_m)=c("X2","CI")
box.plot<- ggplot(mtcars_m, aes(X2, CI,fill=Models)) +
 geom_boxplot(width = 0.1) +
 facet_wrap(~ Models, scales = "free") +
 guides(fill=FALSE) + 
 labs(x="", y="") +
 ggtitle("Box Plots")

我该怎么做?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您的代码包含一些未定义的变量 (Models)。我假设您的意思是X2。这是ggplot2 解决方案:

    outlier1 <- melt(data.frame(outlier))
    colnames(mtcars_m) <- colnames(outlier1) <- c("X2","CI")
    mtcars_m$Outlier <- FALSE
    outlier1$Outlier <- TRUE
    
    ggData <- rbind(mtcars_m, outlier1) 
    
    ggplot(ggData, aes(x=X2, y=CI, fill=X2) ) +
     geom_boxplot() + 
     geom_point(aes(colour=Outlier)) + 
      labs(x="",y="") +
      ggtitle("Box Plots") +
      guides(fill=FALSE) +
     facet_wrap(~ X2, scales = "free")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 2020-06-30
      • 2021-05-21
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多