R ggplot2如何分离图例元素

发布于 2025-01-10 17:57:39 字数 3621 浏览 0 评论 0原文

我将此示例数据以 CSV 形式读取到 R 数据框中,

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

然后使用 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,但我就是无法让它工作,而且我已经在这个问题上呆了太久了.. 。 输入图片此处描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2025-01-17 17:57:40

我是从原来的帖子开始工作的,其中没有任何点与它们相关联的 + 符号。假设这些是由形状= 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")
                                                ) 
                              ))

在此处输入图像描述

注意 I也有过尝试根据有根据的猜测重建 g3 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文