R ggplot2如何分离图例元素
我将此示例数据以 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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是从原来的帖子开始工作的,其中没有任何点与它们相关联的 + 符号。假设这些是由形状= 3的
geom_point
层绘制的,取自数据框dta
的子集,我们可以这样做:注意 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 framedta
we can do:Note I also had to try to reconstruct the
g3
object from an educated guess.