Reputation: 621
I want to produce the scatterplots between the first variable of a dataset and all others, e.g. from iris
the Sepal.Length
with all others. I have created the following:
data <- iris[,c(-5)]
par(mfrow = c(2, 2))
for (i in seq(ncol(data))[-1]) {
plot(
data[, 1],
data[, i],
xlab = colnames(data)[i],
ylab = "Y"
)
lines(lowess(data[,1],data[,i]),col="red")
}
Is there any way to make it looks more professional and not so simple??
Upvotes: 0
Views: 244
Reputation: 526
ggplot2
is great for this type of thing. There are a bunch of themes that can be used to quickly create high quality plots. It also gives you a lot of flexibilty to customize your plot by changing individual elements.
In addition to being able to make the plots pretty, it is very effective at creating the plots in the first place. Here is somewhere to start :
library(tidyverse)
#your example data
data <- iris[, c(-5)]
#pivot_longer rearranges the data in to a long form, which makes it easier to plot
data_def <- pivot_longer(data, -Sepal.Length)
#the data to be plotted
ggplot(data_def, aes(x = Sepal.Length, y = value)) +
#adding the scatter plot (each value is a point)
geom_point() +
#adding a LOESS smoothed line (the default method of smoothing), without the standard error
geom_smooth(se = FALSE, color = "red", size = 0.5) +
#Splits into the three plots based on the measurements and moves the titles underneath the x-axis
facet_wrap( ~ name, scales = "free_y", strip.position = "bottom") +
#Changes the overall look of the plot
theme_classic() +
#Removes elements of the former title (now x-axis) so that there is no surrounding box
theme(strip.background = element_blank(),
strip.placement = "outside") +
#Manually change the axis labels
labs(x = NULL, y = "Y")
I also use ggpubr
which is based on ggplot2
Upvotes: 1