In an instance like this, I would probably use ggplot2, as it excels at this kind of thing.
Assuming that your variables are Y, X1 and X2.
myplot<- ggplot(yourdataframe, aes(x=X1, y=Y, colour=X2))+layer(geom="point")
print(myplot)
That will plot X1 against Y with different colours representing values of X2. You can also call geom="smooth", method="lm"
to get a plot of the regression line, and loess smoothers are also available. HTH.
The following proposition is surely perfectible:
zucchini <- function(st, en, mingap=1)
{
i <- order(st, en-st);
st <- st[i];
en <- en[i];
last <- r <- 1
while( sum( ok <- (st > (en[last] + mingap)) ) > 0 )
{
last <- which(ok)[1];
r <- append(r, last);
}
if( length(r) == length(st) )
return( list(c = list(st[r], en[r]), n = 1 ));
ne <- zucchini( st[-r], en[-r]);
return(list( c = c(list(st[r], en[r]), ne$c), n = ne$n+1));
}
coliflore <- function(st, en, mingap = 1)
{
zu <- zucchini(st, en, mingap);
plot.new();
plot.window( xlim=c(min(st), max(en)), ylim = c(0, zu$n+1));
box(); axis(1);
for(i in seq(1, 2*zu$n, 2))
{
x1 <- zu$c[[i]];
x2 <- zu$c[[i+1]];
for(j in 1:length(x1))
rect( x1[j], (i+1)/2, x2[j], (i+1)/2+0.5, col="gray", border=NA );
}
}
Application:
> st <- runif(20,0,50)
> en <- st + runif(20, 5,20)
> st
[1] 25.571385 17.074676 4.564936 27.247745 23.832638 11.045469 2.845222
[8] 2.824046 23.319625 19.684993 42.610242 48.185618 47.748637 39.813871
[15] 9.235512 40.299425 13.797027 21.079956 31.638772 24.152991
> en
[1] 35.43667 32.20029 19.37133 44.30378 35.73845 16.63794 11.52551 16.06469
[9] 32.22477 26.05563 49.51284 67.77664 67.27914 49.35472 28.27657 50.49421
[17] 27.29273 37.87611 48.76251 39.89335
> coliflore(st, en)
![example](https://i.stack.imgur.com/5C39M.png)
Happy new year!
Best Answer
So, as the question is perfectly on-topic here, as it deals with "Data Visualization", I would reproduce the comment as an answer, so that future viewers can be benefitted.
What is this type of graph called?
This graph is called as a
"Tree diagram"
and sometimes also called adendrogram
Are there existing tools to produce this type of graph?
Yes, the tree diagram can be drawn in all the major data science tools.
Here is a link to the tutorial for plotting the tree diagram in D3.js
In Python, the pydot package can be used. Here is a link to a detailed tutorial.
Here is another tutorial in R, which gives a step-by-step guide on various types of tree diagrams.
I would recommend going with D3 for the dendrograms, owing to better aesthetics, ease of code and flexibility.