Problems with ggplot

Dear I have some problem when plotting segments (representing edges of a grah) with geom_segment (package ggplot2).
Here is the code:

#fake data creation fro testing 
# patients data frame
idStr = matrix(1:npz, nrow=npz, ncol=1)
  idStr = as.character(idStr)
  age <- as.numeric(matrix(floor(runif(npz,min=0,max=101)), nrow=npz, ncol=1))
  stage <- as.numeric(matrix(floor(runif(npz,min=1,max=5)), nrow=npz, ncol=1))
  relevantFeat <-as.numeric(matrix(runif(npz), nrow=npz, ncol=1))
  patients <- data.frame(id=idStr, feat1= age, feat2= stage, feat3 = relevantFeat)
 #end patient data frame

#fake classification scores for each patient andlabel generation
  scores<-data.frame(id=idStr, score=matrix(runif(npz), nrow=npz, ncol=1))
  ind.pos <- which(scores$score > 0.5) #genero dei positivi a caso
  labels <- matrix(0, nrow = npz, ncol=1)
  labels[ind.pos,]<-1
  
  threshSim <- 0.5
#fake adjacency matrix
  similarity <- cor(t(patients[,2:ncol(patients)]), method = "pearson")
  diag(similarity)<- 0 #così quella similarità non viene contata!
  indSimilar <- which(similarity>threshSim);
  indSimilar <- arrayInd(indSimilar, dim(similarity))

#creation of a data frame containing edges connecting patients (idx1 = index in patients of one vertex, idx2 = index of patient at the end of the vertex)
  similarTo <- data.frame(idx1=indSimilar[,1], idx2= indSimilar[,2])

#now I plot the data : THIS WORKS
p2<- ggplot(patients, aes(feat1, feat2, color = labels) )+ geom_point()+
    xlab("feat1") + ylab("feat2") + ggtitle("patient similarity")     

# Adding edges
for (i in 1: nrow(similarTo)){
# create data frame for x, xend, y yend
    df<- data.frame(x1 = as.numeric(patients[similarTo[i,"idx1"],]$feat1),
                    y1 = as.numeric(patients[similarTo[i,"idx1"],]$feat2),
                    x2 = as.numeric(patients[similarTo[i,"idx2"],]$feat1),
                    y2 = as.numeric(patients[similarTo[i,"idx2"],]$feat2))
    p2<-p2+geom_segment(aes(x = as.numeric(x1), y = as.numeric(y1), 
                            xend = as.numeric(x2), yend = as.numeric(y2), color='segment'), data = df)
  }

WHEN I RUN THE LAST FOR LOOP R says:
"Error: Discrete value supplied to continuous scale"
WHY??? all the data are numeric!

HI,

You don't need a loop to add segments, you can add them all at once:

library(ggplot2)

npz = 10
#fake data creation fro testing 
# patients data frame
idStr = matrix(1:npz, nrow=npz, ncol=1)
idStr = as.character(idStr)
age <- as.numeric(matrix(floor(runif(npz,min=0,max=101)), nrow=npz, ncol=1))
stage <- as.numeric(matrix(floor(runif(npz,min=1,max=5)), nrow=npz, ncol=1))
relevantFeat <-as.numeric(matrix(runif(npz), nrow=npz, ncol=1))
patients <- data.frame(id=idStr, feat1= age, feat2= stage, feat3 = relevantFeat)
#end patient data frame

#fake classification scores for each patient andlabel generation
scores<-data.frame(id=idStr, score=matrix(runif(npz), nrow=npz, ncol=1))
ind.pos <- which(scores$score > 0.5) #genero dei positivi a caso
labels <- matrix(0, nrow = npz, ncol=1)
labels[ind.pos,]<-1

threshSim <- 0.5
#fake adjacency matrix
similarity <- cor(t(patients[,2:ncol(patients)]), method = "pearson")
diag(similarity)<- 0 #così quella similarità non viene contata!
indSimilar <- which(similarity>threshSim);
indSimilar <- arrayInd(indSimilar, dim(similarity))

#creation of a data frame containing edges connecting patients (idx1 = index in patients of one vertex, idx2 = index of patient at the end of the vertex)
similarTo <- data.frame(idx1=indSimilar[,1], idx2= indSimilar[,2])

#now I plot the data : THIS WORKS
p2<- ggplot(patients, aes(feat1, feat2, color = as.factor(labels)))+ geom_point()+
  xlab("feat1") + ylab("feat2") + ggtitle("patient similarity") +
  geom_label(aes(label = labels), nudge_y = 0.1)

# Adding edges
p2+geom_segment(aes(x = patients[similarTo[,"idx1"],]$feat1, 
                        y = patients[similarTo[,"idx1"],]$feat2, 
                        xend = patients[similarTo[,"idx2"],]$feat1, 
                        yend = patients[similarTo[,"idx2"],]$feat2),
                data = patients[similarTo[,"idx1"],], color= "black")

I also added some code for adding labels, if you wanted that.

Hope this helps,
PJ

1 Like

Thanks so much!
It helped indeed!

Do you also know how to add an event handler?
I would like to know if the user clicks on one node of the graph and react to the clck by showing another graph

This is a different question and not possible to implement in ggplot2 as it is a static plot (implement it in shiny could be a possibility), so please don't go off-topic in your own thread and make this question on a new topic providing a relevant REPRoducible EXample (reprex)

yes thanks a lot! I will soon ask for some advices!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.