Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting all points (possible conbination) in scatter plot

Tags:

plot

r

line

connect

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)

enter image description here

like image 544
fprd Avatar asked Jul 13 '12 14:07

fprd


People also ask

What does a scatter plot with connected lines look like?

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?

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.

How do you make a connected scatter plot in Python?

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.

How to plot scatter plot with X and Y coordinates in Excel?

# 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:


1 Answers

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))

enter image description here

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))

enter image description here

like image 143
kohske Avatar answered Sep 28 '22 23:09

kohske