Here is small dataset:
myd <- data.frame(PC1 = rnorm(5, 5, 2),
PC2 = rnorm (5, 5, 3), label = c("A", "B", "C", "D", "E"))
plot(myd$PC1, myd$PC2)
text( myd$PC1-0.1, myd$PC2, lab = myd$label)
I want connect all possible combination between line with straight (euclidean) distance, to produce some graph like this (preferrably in base graphics or ggplot2)
And our scatter plot with connected lines would look this “spaghetti”. What we are interested in is understanding how the relationship between two quantitiave variables on the x-and y-axis in the scatter plot changes over time. To do that, we aim to connect the data points at one time point with the corresponding data point in the next time point.
How to Connect Scatterplot Points With Line in Matplotlib? 1 Python3. import numpy as np. import matplotlib.pyplot as plt. x = [0.1, 0.2, 0.3, 0.4, 0.5] y = [6.2, -8.4, 8.5, 9.2, -6.3] plt.title ("Connected ... 2 Python3. 3 Python3.
The linestyle and marker arguments allow to use line and circles to make it look like a connected scatterplot. It means everything is very close to a line chart or a scatterplot that are extensively described in the gallery. Basic connected scatterplot with Python and Seaborn.
# initialize x and y coordinates x =[0.1, 0.2, 0.3, 0.4, 0.5] y =[6.2, -8.4, 8.5, 9.2, -6.3] # set the title of a plot plt.title("Connected Scatterplot points with lines") # plot scatter plot with x and y data plt.scatter(x, y) # plot with x and y data plt.plot(x, y) Output:
Here is the base plot solution:
plot(myd$PC1, myd$PC2)
apply(combn(seq_len(nrow(myd)), 2), 2,
function(x) lines(myd[x, ]$PC1, myd[x, ]$PC2))
Here is the ggplot2 solution:
ps <- data.frame(t(apply(combn(seq_len(nrow(myd)), 2), 2,
function(x) c(myd[x, ]$PC1, myd[x, ]$PC2))))
qplot(myd$PC1, myd$PC2) +
geom_segment(data = ps, mapping = aes(x = X1, xend = X2, y = X3,yend = X4))
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