How to make a connected scatter plot in R?

[This article was first published on Data Analysis in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to make a connected scatter plot in R? appeared first on finnstats.

If you are interested to learn more about data science, you can find more articles here finnstats.

How to make a connected scatter plot in R?, With the help of geom_path, you can depict the relationship between any two variables in a data frame.

library(ggplot2)
x <- c(1, 2, 3, 4, 5, 4, 7, 8, 9)
y <- c(112, 114, 124, 138, 146, 153, 165, 200, 211)
df <- data.frame(x, y)
ggplot(df, aes(x = x, y = y)) +geom_path()

Adoption from the banking sector to push the growth of RPA market

Connected scatter plot

ggplot(df, aes(x = x, y = y)) +
geom_path() +
geom_point(size = 2)

Labeling points

Using geom_text, you may give each observation a label. We’re going to add some dates in this example.

labels <- 2014:2022
df <- data.frame(x, y, labels = labels)
ggplot(df, aes(x = x, y = y)) +
geom_path(color = 4) +
geom_point(size = 2, color = 4) +
geom_text(aes(label = labels, x = x + 0.7, y = y))

Method for Counting TRUE Values in a Logical Vector »

Add arrows

ggplot(df, aes(x = x, y = y)) +
geom_path(color = 4, arrow = arrow()) +
geom_point(size = 2, color = 4) +
geom_text(aes(label = labels, x = x + 0.7, y = y))

However, you can add an arrow between each pair of observations if you use geom_segment as shown in the following example.

ggplot(df, aes(x = x, y = y)) +
geom_segment(aes(xend = c(tail(x, n = -1), NA),
yend = c(tail(y, n = -1), NA)),
arrow = arrow(length = unit(0.4, "cm")),
color = 4) +
geom_point(size = 2, color = 4) +
geom_text(aes(label = labels, x = x + 0.7, y = y))

XGBoost’s assumptions »

If you are interested to learn more about data science, you can find more articles here finnstats.

The post How to make a connected scatter plot in R? appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)