A scatterplot is a type of graph used to display the relationship between two variables. Each point on the graph represents an observation in the data set, with one variable plotted on the x-axis and the other on the y-axis.
- Load the ggplot2 package using the function
library(ggplot2).
- Set the working directory using the function
setwd()
.
setwd("D:/Documents/MAPASR")
3. Load the data using the function read.csv().
datos <- read.csv("Tecoma_stans.csv")
4. Create the scatterplot using the function ggplot().
This function takes the data to be plotted and the variables to be used on the x and y axes, specified using aes(x = Width.of.leaf..cm., y = Length.of.leaf..cm.).
5. Add a layer of points to the plot using the function geom_point(),
specifying the color and size of the points.
6. Add a layer of linear regression line to the plot using the function geom_smooth(),
with the argument method = “lm” and se = FALSE to not show the uncertainty band.
7. Specify the titles and labels for the axes and the plot using the functions labs()
and ggtitle().
8. Customize the appearance of the plot using the function theme()
, in this case, adjust the title by centering it and making the font size larger.
Dis <- ggplot(datos, aes(x = Ancho.de.la.hoja..cm., y = Longitud.de.la.hoja..cm.)) +
geom_point(color = "#A2CD5A", size = 4) +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Gráfico de disperción",
x = "Ancho de la hoja (cm)",
y = "Longitud de la hoja (cm)") +
ggtitle("Gráfico de disperción", subtitle = "") +
theme(plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),
axis.title = element_text(size = 14, face = "bold"))
9. Plot the graph.
Dis
10. Finally, save the graph as an image file with the desired specifications using the function ggsave().
ggsave("grafico_dispersion.png", plot = Dis, width = 8, height = 6, units = "in")
Made with ChatGPT