notMyName
notMyName

Reputation: 838

ggplot multiple abline with color legend

So I have a scatterplot data set and I'm trying to set on top of that many different linear lines using abline. The problem is that I want each line colored a different way and to appear in the Legend with a given class.

I have the following scatter plot with points colored by group:

data = data.frame(x=c(-0.1,0,0.2,1.3,-1.2,-0.5),
                 y=c(-0.8,0,-0.2,1.3,-0.4,0.2),
                 group=c(1,0,0,0,1,1))
g=ggplot(data, aes(x=x,y=y,color=group))+geom_point()+
   geom_hline(yintercept = 0)+geom_vline(xintercept = 0)+
   ggtitle("Scatterplot of Original Points")

enter image description here

Now I want to display different lines on top using a function I created, which takes in the values from my new data set and creates the slopes and intercepts (SKIP THIS AS ITS ONLY HERE FOR YOU TO BE ABLE TO REPRODUCE MY CODE).

decision = function(v1,v2,v3) {
  if(v3 == 0) {
    intercept = 0
    slope = -v2/v1
  }
  else {
    p1_intercept = -v3/v1
    p2_intercept = -v3/v2
    intercept = p2_intercept
    slope = (p2_intercept) / (-p1_intercept)
  }
  return( list( slope=slope, intercept = intercept))
}
init_gen = data.frame(ID=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),v1=c(-1,-0.9,0.9,-0.1,1,-0.8,-0.1,0.6,-0.3,0.6), v2=c(-0.7,0.9,-0.3,0.8,0.5,0.1,0.8,0.8,-0.2,0.4),v3=c(0,0.3,-0.9,-0.4,-0.5,0.6,0.3,0.2,1,0.3))

for(i in 1:10) {
  line = decision(init_gen$v1[i],init_gen$v2[i],init_gen$v3[i])
  init_gen$slope[i] = line$slope
  init_gen$intercept[i] = line$intercept
}

I then take my previous plot and add on those slopes and intercepts:

g+geom_abline(data=init_gen, aes(slope=slope,intercept=intercept, linetype=ID))

enter image description here

The problem now is that I want each line to be colored differently, which intuitively might mean to add (color=ID):

g+geom_abline(data=init_gen, aes(slope=slope,intercept=intercept, linetype=ID,color=ID))

But I get the following error

enter image description here

I've searched and I've found other resources but nothing pertaining to abline() using colors...

Upvotes: 1

Views: 1393

Answers (2)

Lucas Nesi
Lucas Nesi

Reputation: 403

The problem is that you already use color for your points with a continuous value. One trick here is to use fill for points with a shape = 21.

g=ggplot(data, aes(x=x,y=y)) + geom_point(aes(fill=group), shape = 21)+
   geom_hline(yintercept = 0)+geom_vline(xintercept = 0)+
   ggtitle("Scatterplot of Original Points")
g+geom_abline(data=init_gen, aes(slope=slope,intercept=intercept, linetype=ID,color=ID))

The Result:

enter image description here

Upvotes: 1

Kat
Kat

Reputation: 18714

You could use ggnewscale like @stefan said. It's a great option. Alternatively, you can make a palette.

I used a palette option from the library viridis, but you could use something else.

dPal = viridis::viridis_pal()(nrow(init_gen))  # palette size matching init_gen

g + geom_abline(data=init_gen, 
                aes(slope=slope,
                    intercept=intercept,
                    linetype=ID),
                    color = dPal) 

enter image description here

Upvotes: 1

Related Questions