Reputation: 4453
I am using R
to perform a machine learning
analysis and I came across this beautiful data visualization done with Python
.
I would like to replicate this in R
. Is there a specific package in R
that can achieve this? Or may be some online examples of the same visualizations with R
.
Upvotes: 2
Views: 543
Reputation: 5000
You can use a package but can also just do this using base R
. You should decide where each single plot (e.g., histogram, scatter plot) goes in the panel. This is an example, also using the R iris
dataset. I did not format the plots much as that info can be found elsewhere and it is beyond the scope of the question.
v1 <- iris$Sepal.Length
v2 <- iris$Sepal.Width
v3 <- iris$Petal.Length
png('panel.png', units="in", width=9, height=9, res=150)
par(mfrow=c(3,3))
hist(v1, main='')
plot(v2, v1)
plot(v3, v1)
plot(v1, v2)
hist(v2, main='')
plot(v3, v2)
plot(v1, v3)
plot(v2, v3)
hist(v3, main='')
dev.off()
Upvotes: 0
Reputation: 37661
You can do this with the pairs
function in R. Here is an example that is only a modest modification of an example given in the help page ?pairs
. You need to define a function that plots the histogram on the diagonal - but the help page provides that code.
panel.hist <- function(x, ...) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, ...)
}
pairs(iris[,1:4], diag.panel=panel.hist, pch=16, col="steelblue")
Upvotes: 5