2

I simply can't find a way to plot legends panel in this specific ggplot with ggplot2 on R. Just want to make it appear.

For context, I'm plotting chemical abundances of sample versus the atomic number of the elements.

For background, I tried many things that are described here:

Reasons that ggplot2 legend does not appear

including links therein, however could not find a solution for my specific data set.

I know the problem could be within the structure of the data set, since I've been able to do that with other data, but I can't solve it. I also know that the problem should have to do with the theme() described in the code below, because when I use default ggplot configuration legends actually appear. I use this personalized theme for consistency trough out my work.

This is what I have so far removing cosmetics:

ggplot(atomic, aes(x=atomic$Z, y = atomic$avg, group=1), fill = atomic$Z) + 

plot dots for average of values

geom_point(data=atomic, aes(x=atomic$Z, y=atomic$avg, group=1, color="black"), size=0.5, alpha=1, shape=16 ) +

connect dots for average of values

geom_line(data=atomic, aes(x=atomic$Z, y=atomic$avg, group=1), color="black", linetype= "dashed") +

plot dots for actual values from the samples

geom_point(data=atomic, aes(x=atomic$Z, y=atomic$SDSS, group=1, color="#00ba38"), size=5, alpha=1, shape=16, color="#00ba38") +

geom_point(data=atomic, aes(x=atomic$Z, y=atomic$HE22, group=1, color="#619cff"), size=5, alpha=1, shape=16, color="#619cff") +

geom_point(data=atomic, aes(x=atomic$Z, y=atomic$HE12, group=1, color="#F8766D"), size=5, alpha=1, shape=16, color="#F8766D") +

EDIT: the Definition of base_breaks (used below)

base_breaks_x <- function(x){
  b <- pretty(x)
  d <- data.frame(y=-Inf, yend=-Inf, x=min(b), xend=max(b))
  list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend), inherit.aes=FALSE),
       scale_x_continuous(breaks=b))
}
base_breaks_y <- function(x){
  b <- pretty(x)
  d <- data.frame(x=-Inf, xend=-Inf, y=min(b), yend=max(b))
  list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend), inherit.aes=FALSE),
       scale_y_continuous(breaks=b))
}

the problem might be here

theme_bw() +

theme(plot.title = element_text(hjust = 0.5),
    text = element_text(size=20),
    legend.position="bottom",
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()) +
base_breaks_x(atomic$Z) +
base_breaks_y(atomic$HE22)

The data set is the following

 Z Name  HE22  SDSS  HE12   avg
1   3   Li    NA  1.00    NA  1.00        
2   6    C  6.16  5.50  6.06  5.91              
3   7    N    NA    NA  6.49  6.49           
4  11   Na    NA    NA  3.53  3.53         
5  12   Mg  5.32  4.43  4.99  4.91         
6  13   Al  2.90    NA  3.08  2.99           
7  14   Si    NA  4.90  4.89  4.90              
8  20   Ca  4.07  3.37  3.56  3.67             
9  21   Sc  0.72 -0.07  0.24  0.30            
10 22   Ti  2.74  1.79  2.47  2.33             
11 23    V    NA    NA  1.18  1.18            
12 24   Cr  2.88  2.14  2.67  2.56              
13 25   Mn  2.34  1.59  2.44  2.12        
14 26   Fe  4.92  4.14  4.59  4.55           
15 27   Co  2.57  1.72  2.36  2.22           
16 28   Ni  3.63  2.96  3.51  3.37               
17 29   Cu    NA    NA  0.31  0.31              
18 30   Zn  2.29    NA  2.44  2.37           
19 38   Sr  0.62  0.29  0.41  0.44           
20 39    Y -0.22 -0.44 -0.33 -0.33             
21 40   Zr  0.60    NA  0.30  0.45             
22 56   Ba  0.13 -0.10  0.12  0.05            
23 57   La -0.77 -0.49 -0.77 -0.68         
24 58   Ce    NA    NA -0.39 -0.39            
25 59   Pr    NA    NA -0.78 -0.78               
26 60   Nd -0.47    NA -0.37 -0.42              
27 62   Sm    NA    NA -0.57 -0.57         
28 63   Eu -1.02 -0.92 -0.85 -0.93               
29 64   Gd    NA    NA -0.39 -0.39             
30 66   Dy    NA    NA -0.16 -0.16               
31 68   Er    NA -0.40    NA -0.40               
32 70   Yb    NA -0.60    NA -0.60                
33 90   Th    NA -0.60    NA -0.60  

as Z = atomic number, Name = element, HE12/HE22/SDSS = samples, avg = average of the samples.

I would like to know how I can add legend panel coherent with the colors of my scatter plots.

Thank you so much! Hope I could describe the problem properly.

guilimberg
  • 113
  • 1
  • 8
  • How are you defining `base_breaks_x()` and `base_breaks_y()`? They are not `ggplot2` functions – ladylala Mar 29 '19 at 04:14
  • Thank you for pointing that out, gonna edit it right away. It was stupid not to have included that in the first place. – guilimberg Mar 29 '19 at 04:21

1 Answers1

5

This is personally what I would do.

I converted the data from wide format to long format since it's easier to manipulate colors that way (Sorry I just used generic "key" and "value" since I'm not sure what you would want your columns to be named). Hopefully this will get you at least part of the way to where you want to go. Let me know if you have questions!

library(ggplot2)
library(tidyr)
p <- atomic %>% 
  gather(key = "key", value = "value", SDSS, HE22, HE12) %>% 
  ggplot(aes(Z, value, color = key))+
  geom_point() +
  geom_text(aes(x = Z, y = avg, label = Name), # EDITED
            color = "black")
  scale_color_manual(values = c("#00ba38", "#619cff", "#F8766D"))
p +
  geom_line(data=atomic, aes(x=atomic$Z, y=atomic$avg, group=1), color="black", 
            linetype= "dashed") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        text = element_text(size=20),
        legend.position="bottom",
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
base_breaks_x(atomic$Z) +
base_breaks_y(atomic$HE22)

EDITED

I added the geom_text() command so labels show up. You can adjust the arguments so the labels look better. I've also heard geom_text_repel() in the ggrepel package is helpful for creating nice labels: https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html#examples My output

ladylala
  • 223
  • 1
  • 6
  • Thank you so much! That was, actually, exactly what I was looking for. Now, what I'm trying to do is adding labels to the plot for each chemical element, like this: [link](https://imgur.com/a/6797DLP) (that's what I had before posting here). – guilimberg Mar 29 '19 at 05:15
  • I edited the code above^. Basically I just added one command – ladylala Mar 31 '19 at 02:40
  • Thank you so so much! You actually helped me even more with this implementation. I had stopped working on this project and now I'm back to it with a complete solution from you. I really appreciate it! – guilimberg Apr 11 '19 at 17:27