许多变量对单个变量的散点图

Scatterplot of many variables against a single variable

我想获得一维散点图数组,所有散点图都针对单个变量。我可以从 'pairs()' 返回的完整矩阵中提取它们,但是其他图在我的 case.Changing 布局中没有用,当 c(1,) 无法将整个图正确地放在一行中时变量的数量很高。

attach(iris)
caret::featurePlot(x = iris[, 1:4],
        y = iris[,5],#Species
        plot = "pairs",
        auto.key = list(columns = 3))

请使用条件变量为'Species'的'iris'数据集进行说明。我想要的只是一个简单的图,其中 "Petal.Width" 是针对其他 3 个预测变量绘制的,即 "Sepal.Length"、"Sepal.Width" 和 "Petal.Length" 并着色为 "Species".

(没有reproducible example,很难用这个做很多事情。)

一个简单的解决方案是打开一个 pdf 文件以接受绘制的图表,然后遍历其他变量,一次绘制一个散点图。您可以使用不同的符号和颜色来表示在您要作为条件的因素的两个不同水平上的观察结果。

pdf(file=<something>, <other settings>)
for(i in 1:13){  # or perhaps: for(i in c(1,2,5,10,11,12,13)){
  plot(AF[,i], AF[,14], pch=<something>, col=<something>)
}
dev.off()

更新: 我可以使用 iris 数据集作为示例来说明我上面的回答。

data(iris)
add.legend <- function(...){  # taken from: 
  opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), 
    mar=c(0, 0, 0, 0), new=TRUE)
  on.exit(par(opar))
  plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
  legend(...)
}

pdf(file="Scatterplots of variables against petal width.pdf")
for(i in 1:3){
  plot(iris[,i], iris[,4], pch=as.numeric(iris$Species), col=as.numeric(iris$Species),
       xlab=names(iris)[i], ylab="Petal Width")
  add.legend("top", legend=levels(iris$Species), horiz=TRUE, pch=1:3, col=1:3)
}
dev.off()

您将在新页面上获得每个图的 pdf 文件。 @Hack-R 和@Alex 的回答的问题(没有更好的词,我不想在这里太苛刻)是它们不能很好地扩展。这些图只挤了三个就可以了,但是当你有十三个(!)时,它们就不会提供很多信息。 pdf 中的所有图看起来都干净且成比例,如下图(这是 pdf 中的第一个图):

data(iris)
library("ggplot2")
library("reshape2")

#melt your data
iris$Species <- as.character(iris$Species)
df_melt      <- melt(iris,c("Petal.Width","Species"))
head(df_melt)

# define colors
df_melt$color <- "blue"
df_melt$color[df_melt$Species == "setosa"]      <- "green"
df_melt$color[df_melt$Species == "versicolor"]  <- "orange"

#scatterplot per group
ggplot(df_melt,aes(Petal.Width,value)) +
  geom_point(col=df_melt$color) +
  facet_grid(.~variable) 

Base R 解决方案-- par(mfrow= c(1,n)) 是关键:

data(iris)
par(mfrow=c(1,3))
plot(iris$Sepal.Length, iris[,2])
plot(iris$Sepal.Length, iris[,3])
plot(iris$Sepal.Length, iris[,4])

显然您可以使用 base 绘图的其他功能来自定义绘图