how to generate a plot with 2 o more legend using 2 data frame in R

30 Views Asked by At

I'm trying to make a function using ggplot2 with 2 data frames.

The fist data frame:

head(DF1, 15) # with 280 rows

          SeqTech         S1          S2         C1         C2
AM.AD.1    Sanger -1.0436581 -0.37756924 -0.7685488 -0.0441722
AM.AD.2    Sanger -0.5714184 -0.60946292 -0.7685488 -0.0441722
AM.F10.T1  Sanger -0.6000866 -0.09649056 -0.7685488 -0.0441722
AM.F10.T2  Sanger -0.5232439 -0.25033653 -0.7685488 -0.0441722
DA.AD.1    Sanger -0.8911968 -0.16821038 -0.7685488 -0.0441722
DA.AD.1T   Sanger -0.9517195 -0.11437614 -0.7685488 -0.0441722
DA.AD.2    Sanger -0.8475660  0.17727014 -0.7685488 -0.0441722
DA.AD.3    Sanger -1.1066888  0.06575432 -0.7685488 -0.0441722
DA.AD.3T   Sanger -0.9968716  0.07856669 -0.7685488 -0.0441722
DA.AD.4    Sanger -1.0187115 -0.05531954 -0.7685488 -0.0441722
ES.AD.1    Sanger -0.2993661  0.57074665 -0.7685488 -0.0441722
ES.AD.2    Sanger -0.7723388 -0.28290136 -0.7685488 -0.0441722
ES.AD.3    Sanger -0.5537318 -0.13383022 -0.7685488 -0.0441722
ES.AD.4    Sanger -0.7448872  0.11534246 -0.7685488 -0.0441722
FR.AD.1    Sanger -0.6601532 -0.01037697 -0.7685488 -0.0441722

And 3 categories in SeqTech column

unique(DF1$SeqTech)
[1] Sanger   Illumina Pyro454 
Levels: Illumina Pyro454 Sanger

the second data frame is

DF2

   SeqTech      Cent1        Cent2
1 Illumina -0.7304342  0.031788410
2  Pyro454  0.6077754 -0.005785419
3   Sanger -0.7685488 -0.044172203

this is my code:

Category = "SeqTech"

#generate the points (sites: S1 and S2)
P <- ggplot(DF1, aes(x = S1, y = S2)) + 
    geom_vline(xintercept = 0, linetype="dashed", size = 0.25, color= "#999999") + 
    geom_hline(yintercept = 0, linetype="dashed", size = 0.25, color= "#999999") +
    geom_point(size = 2, alpha=0.7, aes_string(color= Category, shape=Category))

Now add the lines that connect each point with average point using DF1: C1 and C2

P <- P + geom_segment(data = DF1, aes(xend = C1, yend = C2, colour=DF1[, Category]), alpha=0.7, linetype="dashed")

Now I will add a point for each category that represent the average using the data frame DF2

P <- P + geom_point(data = DF2, x=DF2[,"Cent1"], y=DF2[,"Cent2"], size = 1.5, alpha= 0.9, shape= 21, aes_string(color=Category), fill="black")

This is the obtained plot:

enter image description here

So my problem is, that I want to add a second legend, I mean the legend in the first plot all elements are overlapped in a single legend, and I want the fist legend until: geom_segment(data = DF1, aes(xend = C1, yend = C2....... and a second legend using the data obtained from the data frame DF2 with a single point in a second the legend like the "o Centroid":

enter image description here

sorry for the picture, it is edited !!!!

Any suggestion ??

Thanks so much !!!!

0

There are 0 best solutions below