how to create a scatterplot in r

To create a scatterplot in R, you can use the ggplot2 package, which provides a powerful set of tools for data visualization. Here's an example code snippet for creating a scatterplot:

main.r
library(ggplot2)

# create a sample dataframe with x and y variables
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 3, 5, 6, 8))

# create the scatterplot using ggplot2
ggplot(df, aes(x = x, y = y)) +
  geom_point()
217 chars
9 lines

In this example, we first load the ggplot2 package using the library() function. We then create a sample dataframe with two variables, x and y.

To create the scatterplot, we use the ggplot() function and specify the dataframe as well as the aes() (aesthetics) mapping. We map the x variable to the x axis and the y variable to the y axis.

Finally, we add a layer to the plot using geom_point(), which creates the individual points on the plot.

This is just a basic example, but ggplot2 provides numerous functions and features for customizing and enhancing plots.

gistlibby LogSnag