How can I create scatter plots by group in R using two examples? 2

How can I create scatter plots by group in R using two examples?

In R, scatter plots can be created by grouping data points into different categories and displaying them on a single plot. This allows for easy comparison between the groups and identification of any patterns or relationships. To create scatter plots by group in R, the “ggplot2” package can be utilized. Two examples of creating scatter plots by group in R could be comparing the heights of males and females in a population, or examining the relationship between income and education level in different regions. By specifying the grouping variable in the code, the scatter plot can be divided into distinct groups and color-coded for better visualization. This method is useful in identifying any variations or trends within different groups and can aid in making data-driven decisions.

Create Scatter Plots by Group in R (2 Examples)


You can use the following methods to create a scatter plot by group in R:

Method 1: Use Base R

plot(df$x, df$y, col=as.factor(df$group))

Method 2: Use ggplot2

library(ggplot2)

ggplot(df, aes(x, y)) +
  geom_point(aes(color=group))

The following examples show how to use each method in practice with the following data frame:

#create data frame
df <- data.frame(x=c(1, 2, 2, 3, 5, 6, 7),
                 y=c(4, 8, 7, 9, 15, 14, 20),
                 group=c('A', 'A', 'A', 'B', 'B', 'B', 'B'))

#view data frame
df

  x  y group
1 1  4     A
2 2  8     A
3 2  7     A
4 3  9     B
5 5 15     B
6 6 14     B
7 7 20     B

Example 1: Scatter Plot by Group in Base R

The following code shows how to create a scatterplot in base R where the points are colored based on the value of the ‘group’ variable:

#create scatterplot with points colored by group
plot(df$x, df$y, col=as.factor(df$group), pch=19)

The color of each point reflects the value of the ‘group’ variable in the data frame.

The points with a ‘group’ value of A are shown in black and the points with a ‘group’ value of B are shown in red.

Note that pch=19 tells R to use solid circles for the points in the plot.

You can find a complete list of pch values and their corresponding shapes .

Example 2: Scatter Plot by Group in ggplot2

The following code shows how to create a scatterplot in ggplot2 where the points are colored based on the value of the ‘group’ variable:

library(ggplot2)

#create scatterplot with points colored by group
ggplot(df, aes(x, y)) +
  geom_point(aes(color=group))

Note that you can also modify the colors and size of the points in the plot:

library(ggplot2)

#create scatterplot with points colored by group
ggplot(df, aes(x, y)) +
  geom_point(aes(color=group), size=3) +
  scale_color_manual(values=c('purple', 'steelblue'))

scatter plot by group in R

Notice that we increased the point size using the size argument and the colors of the points match the colors that we specified in the scale_color_manual() argument.

Additional Resources

The following tutorials explain how to perform other common tasks with scatterplots in R:

Cite this article

stats writer (2024). How can I create scatter plots by group in R using two examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-scatter-plots-by-group-in-r-using-two-examples/

stats writer. "How can I create scatter plots by group in R using two examples?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-scatter-plots-by-group-in-r-using-two-examples/.

stats writer. "How can I create scatter plots by group in R using two examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-scatter-plots-by-group-in-r-using-two-examples/.

stats writer (2024) 'How can I create scatter plots by group in R using two examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-scatter-plots-by-group-in-r-using-two-examples/.

[1] stats writer, "How can I create scatter plots by group in R using two examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create scatter plots by group in R using two examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top