通过R迅速绘制散点图
pairs(iris[1:4],
main = "Anderson's Iris Data -- 3 species",
pch = 21,
bg = c("#1b9e77", "#d95f02", "#7570b3")[unclass(iris$Species)]
一些其他的方式绘制散点图
# not run
library(ggplot2)
library(GGally)
ggpairs(iris, columns=1:4, aes(color=Species)) +
ggtitle("Anderson's Iris Data -- 3 species")
library(lattice)
splom(iris[1:4],
groups=iris$Species,
main="Anderson's Iris Data -- 3 species")
通过ggplot2结合cdata进行相关的操作
library(ggplot2)
library(cdata)
meas_vars <- colnames(iris)[1:4]
# the data.frame() call strips the attributes from
# the frame returned by expand.grid()
controlTable <- data.frame(expand.grid(meas_vars, meas_vars,
stringsAsFactors = FALSE))
# rename the columns
colnames(controlTable) <- c("x", "y")
# add the key column
controlTable <- cbind(
data.frame(pair_key = paste(controlTable[[1]], controlTable[[2]]),
stringsAsFactors = FALSE),
controlTable)
controlTable
iris_aug = rowrecs_to_blocks(
iris,
controlTable,
columnsToCopy = "Species")
head(iris_aug)
splt <- strsplit(iris_aug$pair_key, split = " ", fixed = TRUE)
iris_aug$xv <- vapply(splt, function(si) si[[1]], character(1))
iris_aug$yv <- vapply(splt, function(si) si[[2]], character(1))
head(iris_aug)
# reorder the key columns to be the same order
# as the base version above
iris_aug$xv <- factor(as.character(iris_aug$xv),
meas_vars)
iris_aug$yv <- factor(as.character(iris_aug$yv),
meas_vars)
ggplot(iris_aug, aes(x=x, y=y)) +
geom_point(aes(color=Species, shape=Species)) +
facet_grid(yv~xv, labeller = label_both, scale = "free") +
ggtitle("Anderson's Iris Data -- 3 species") +
scale_color_brewer(palette = "Dark2") +
ylab(NULL) +
xlab(NULL)
不过这样的操作有点负载,通过ggpairs(),splom()这样的方式就会简单许多,下面的这个包也有相关的效果
library(WVPlots)
PairPlot(iris,
colnames(iris)[1:4],
"Anderson's Iris Data -- 3 species",
group_var = "Species")