繁体   English   中英

R ggplot2 如何分离图例元素

[英]R ggplot2 how to separate legend elements

我将此示例数据作为 CSV 读入 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"

然后我plot下图使用ggplot和下面的代码:

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

我得到这个数字。 问题是我想要的是一个单独的图例元素,它表示“+”形状,没有颜色。 最后,图例中应该有 10 个元素,9 个实际的彩色圆形(但里面没有“+”)和 1 个“+”。

我看到有些人在guid_legend部分使用override.aes ,但我就是没能让它工作,而且我在这上面待了太久了...... 在此处输入图像描述

我是从原来的帖子开始工作的,那里没有点与 + 号相关联。 假设这些是由 shape = 3 的geom_point层绘制的,取自数据框dta的子集,我们可以这样做:

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

在此处输入图像描述

请注意,我还必须尝试根据有根据的猜测重建g3 object。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM