How to make a connected scatter plot in R?
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))