I have a data frame looks like this one:
X Y
100 50
80 60
70 90
110 60
30 20
... ...
Around 100 lines more. Both X column and Y column are numeric
When I plot this points, I would like to draw a line between first point (100,50) and any of the rest points. In other words, I would like to have a line connects (100,50) with (80,60), a line connects (100,50) with (70,90), a line connects (100,50) with (110,60) but there is no line between (80,60) and (70,90). All lines start from the first point.
I do not have the third column. I can not use group. I wonder whether I can still plot this graph in the ggplot.
Thank you
segment() function in R Language is used to draw a line segment between to particular points. Parameters: x, y: coordinates to draw a line segment between provided points. Here, x0 & y0 are starting points of the line segment and x1 & y1 are ending points of line segment .
In ggplot2 we can add lines connecting two data points using geom_line() function and specifying which data points to connect inside aes() using group argument.
the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library. It would be something like (df %>% sample_frac(0.01))
The idea is to use group. For this, you have to add a third column that can be used for grouping. A possible way to achieve this is as follows.
First, I define the sample data
df <- read.table(text = "X Y
100 50
80 60
70 90
110 60
30 20", header = TRUE)
Then, I create a new data frame, where the first row is repeated for each one of the other rows. A column grp
is added, that links each of the repetitions of the first row to one of the other rows:
n <- nrow(df) - 1
new_data <- data.frame(X = c(rep(df$X[1], n), df$X[-1]),
Y = c(rep(df$Y[1], n), df$Y[-1]))
new_data$grp <- as.factor(rep(1:n, times = 2))
new_data
## X Y grp
## 1 100 50 1
## 2 100 50 2
## 3 100 50 3
## 4 100 50 4
## 5 80 60 1
## 6 70 90 2
## 7 110 60 3
## 8 30 20 4
Now, the plot can be created directly with ggplot:
library(ggplot2)
ggplot(new_data, aes(X, Y, group = grp)) +
geom_point() + geom_line()
The aesthetic group
controls which points should be connected by a line. Since the column grp
in new_data
alway pairs a repetition of the first row with each of the other rows, the point corresponding to the first row is connected to each of the other points.
If you omit group = grp
, a plot with a single line going through all the points is drawn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With