Home > Software design >  R ggplot2 how to separate legend elements
R ggplot2 how to separate legend elements

Time:03-03

I read this example data as CSV into an R dataframe

"TEST","PREVIOUS","LAST","Interaction"
0.592,0.689,0.708,"Insertion"
0.702,0.796,0.836,"Insertion"
0.687,0.738,0.738,"Circular permutation and repetition"
0.727,0.675,0.734,"Insertion"
0.944,0.919,0.925,"Insertion and conformational variability"
0.862,0.847,0.847,"Conformational variability"
0.808,0.763,0.827,"Insertion"
0.981,0.983,0.983,"Conformational variability"
0.703,0.786,0.788,"Circular permutation and conformational variability"
0.691,0.732,0.732,"Insertion and conformational variability"
0.659,0.641,0.655,"Insertion"
0.6,0.602,0.676,"Insertion and conformational variability"
0.355,0.704,0.707,"Circular permutation and insertion"
0.436,0.716,0.739,"Circular permutation and insertion"
0.386,0.677,0.699,"Circular permutation and insertion"
0.976,0.979,0.979,"Conformational variability"
0.73,0.757,0.757,"Circular permutation"
0.492,0.918,0.918,"Circular permutation"
0.688,0.672,0.685,"Circular permutation and insertion"
0.79,0.799,0.799,"Circular permutation"
0.756,0.817,0.834,"Circular permutation and insertion"
0.677,0.668,0.677,"Insertion"
0.892,0.884,0.89,"Conformational variability"
0.604,0.752,0.759,"Insertion and conformational variability"
0.589,0.721,0.73,"Insertion and repetition"
0.916,0.927,0.927,"Conformational variability and repetition"
0.665,0.667,0.667,"Insertion"
0.62,0.684,0.717,"Insertion"
0.683,0.714,0.714,"Circular permutation and conformational variability"
0.55,0.75,0.788,"Insertion"
0.584,0.67,0.704,"Insertion and repetition"
0.635,0.684,0.684,"Circular permutation"
0.45,0.605,0.623,"Insertion and conformational variability"
0.53,0.611,0.611,"Insertion"
0.792,0.778,0.811,"Insertion and repetition"
0.524,0.595,0.631,"Insertion"
0.591,0.719,0.771,"Insertion and conformational variability"
0.633,0.719,0.719,"Insertion"
0.785,0.803,0.816,"Insertion and repetition"
0.738,0.723,0.731,"Insertion and repetition"

Then I plot the following figure using ggplot and the following code:

library(ggplot2)

fig = ggplot(dta, aes(x=LAST, y=TEST, fill=Interaction))  
geom_point(
    dta[which(dta["LAST"] == dta["PREVIOUS"]),],
    mapping =aes(x=LAST,y=TEST),
    shape=21, size=3,stroke = 0.25, alpha=0.9)   


geom_point(
    dta[which(dta["LAST"] != dta["PREVIOUS"]),],
    mapping =aes(x=LAST,y=TEST),
    shape=21, size=3, stroke = 0.25, alpha=0.9)   


geom_point(
    dta[which(dta["LAST"] != dta["PREVIOUS"]),],
    mapping =aes(x=LAST,y=TEST),
    shape=3, size=1,stroke = 0.25)   


xlim(0.4, 1.0)  
ylim(0.4, 1.0)  
theme(legend.position="top") 
     xlab("LAST")   ylab("TEST")  
     guides(fill = guide_legend(ncol=1, title=""))

p4b = p4b   coord_fixed() guides(fill =guide_legend(ncol=2, title="Interactions", title.position="top",
                                                  title.theme = element_text(
                                                  size = 15,
                                                  face = "bold",
                                                  colour = "black",
                                                  margin=margin(t = 0, r = 0, b = 0, l = 5, unit = "pt")
                                                ) 
                                                 )
                                )
p4b

I obtain this figure. The problem is that what I want is a separate legend element that represents the " " shape, without color. In the end, there should be 10 elements in the legend, the 9 actual coloured round shapes (but without the " " inside) and 1 " " alone.

I saw some people using override.aes in the guid_legend part, but I just wasn't able to make it work and I'm on this for too long now ... enter image description here

CodePudding user response:

I was working from the original post, where no points had associated signs with them. Assuming these are drawn by a geom_point layer with shape = 3, taken from a subset of the data frame dta we can do:

ggplot(dta, aes(TEST1, TEST, fill = Interaction))  
  annotation_custom(grob = g3, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)  
  geom_abline(intercept = 0, slope = 1, color="black", size = 1.0)  
  geom_abline(intercept = c(-0.05, 0.05),  slope = 1, 
              color = "black", linetype = "dotted", size = 0.25, alpha = 0.5)  
  geom_abline(intercept = c(-0.1, 0.1), slope = 1, color = "black", 
              linetype = "dashed", size = 0.25, alpha = 0.5)  
  geom_point(shape = 21, size = 4.5, stroke = 0.25, alpha = 0.9)  
  geom_point(aes(TEST1, TEST, shape = "Positive"), inherit.aes = FALSE,
             size = 1.5, data = dta[sample(nrow(dta), 12),])  
  scale_shape_manual(values = 3, name = "Special")  
  xlim(0.4, 1.0)  
  ylim(0.4, 1.0)  
  theme(legend.position="top",
        legend.key.height= unit(.4, 'cm'),
        plot.background = element_blank(),
        panel.background = element_rect(fill = "transparent", colour = "gray"),
        panel.border = element_rect(fill = "transparent", colour = "black"),
        axis.text = element_text(color = "black"),
        legend.background = element_blank(),
        legend.box.background = element_blank(),
        legend.key = element_blank(),
        legend.text = element_text(size = 9),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.x = element_text(color = "black",size = 13, face = "bold"),
        axis.title.y = element_text(color = "black", size=15, face = "bold")
       )  
     xlab("TEST1")   
  ylab("TEST")  
  coord_fixed()  
  guides(fill = guide_legend(ncol = 2, 
                             title = "Interactions", 
                             title.position = "top",
                             title.theme = 
                               element_text(
                                  size = 15,
                                  face = "bold",
                                  colour = "black",
                                  margin = margin(t = 0, r = 0, b = 0, l = 5, 
                                                  unit = "pt")
                                                ) 
                              ),
         shape = guide_legend(title = "Special", 
                             title.position = "top",
                             title.theme = element_text(
                                  size = 15,
                                  face = "bold",
                                  colour = "black",
                                  margin = margin(t = 0, r = 0, b = 0, l = 5, 
                                                  unit = "pt")
                                                ) 
                              ))

enter image description here

Note I also had to try to reconstruct the g3 object from an educated guess.

  • Related