首页 > 解决方案 > 无法将图例面板添加到具有多个数据集的某个散点图

问题描述

我根本找不到在 R 上使用 ggplot2 在这个特定的 ggplot 中绘制图例面板的方法。只想让它出现。

对于上下文,我正在绘制样品的化学丰度与元素的原子序数。

对于背景,我尝试了这里描述的许多事情:

ggplot2图例不出现的原因

包括其中的链接,但是找不到我的特定数据集的解决方案。

我知道问题可能出在数据集的结构中,因为我已经能够用其他数据做到这一点,但我无法解决它。我也知道问题应该与下面代码中描述的主题()有关,因为当我使用默认的 ggplot 配置图例时,实际上会出现。我使用这个个性化的主题来保持我工作的一致性。

这是我到目前为止去除化妆品的方法:

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

为平均值绘制点

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

连接点以获得平均值

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

为样本中的实际值绘制点

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") +

编辑:base_breaks 的定义(在下面使用)

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))
}

问题可能在这里

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)

数据集如下

 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  

Z = 原子序数,名称 = 元素,HE12/HE22/SDSS = 样品,平均 = 样品的平均值。

我想知道如何添加与散点图颜色一致的图例面板。

太感谢了!希望我能正确描述问题。

标签: rggplot2

解决方案


这是我个人会做的。

我将数据从宽格式转换为长格式,因为这样操作颜色更容易(抱歉,我只使用了通用的“键”和“值”,因为我不确定您希望列的名称是什么)。希望这能让你至少部分地到达你想去的地方。如果您有任何问题,请告诉我!

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)

已编辑

我添加了geom_text()命令,以便显示标签。您可以调整参数,使标签看起来更好。我还听说包geom_text_repel()ggrepel有助于创建漂亮的标签:https ://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html#examples 我的输出


推荐阅读