[英]Scatter plot in ggplot2 with triple legend
I would like to make a scatter plot with shape, color and fill as coding for my data. 我想用形状,颜色和填充来绘制散点图,以作为我数据的编码。 Everything looks just fine with shape and color, however the fill option doesn't seem to respond. 一切看起来都很好,形状和颜色都很好,但是填充选项似乎没有响应。 I've checked already other similar posts to this topic, but still can't figure out how to deal with this. 我已经检查了与此主题相关的其他类似帖子,但仍然不知道如何处理。 What I want to have in my case is Filter column filled based on two different filter sizes (0.22=hollow, 0.45=solid) and everything according to my Time coloring. 在我的情况下,我想根据两个不同的过滤器大小(0.22 =空心,0.45 =实心)以及根据我的时间着色的所有内容填充“过滤器”列。 Please find my dataset and code that I was using, 请找到我正在使用的数据集和代码,
Samples Filter Type Time Coord1 Coord2
45P116 0.45 Oil Incubation 16 -0.177125327477 0.0329641384336
22P264 0.22 Oil Incubation 64 -0.036421737819 0.41278336259
45P113 0.45 Oil Incubation 13 -0.183501817053 -0.0333774595889
45BL30 0.45 Control 30 -0.0169216562011 -0.160467270544
22P231 0.22 Oil Incubation 30 -0.0742549515959 0.134945242558
22P116 0.22 Oil Incubation 16 -0.168257444167 0.0425318235085
22P164 0.22 Oil Incubation 64 -0.0995650111648 0.321208450137
22BL64 0.22 Control 64 0.344181403614 0.011496893707
45P29 0.45 Oil Incubation 9 -0.139337303592 -0.154032242023
22P216 0.22 Oil Incubation 16 -0.182624942475 0.036656544869
22P30 0.22 Oil Incubation 0 0.232890938751 -0.0365286035332
45P23 0.45 Oil Incubation 3 0.335212340096 0.00229714294784
22P364 0.22 Oil Incubation 64 -0.0265850037098 0.267838399813
45P26 0.45 Oil Incubation 6 -0.0663530024752 -0.123614294282
45P316 0.45 Oil Incubation 16 -0.180387066353 0.0244231332556
22P16 0.22 Oil Incubation 6 -0.0938690796601 -0.123292570064
22P13 0.22 Oil Incubation 3 -0.000929711149241 -0.0840936522112
22P36 0.22 Oil Incubation 6 -0.0846096566973 -0.126315336717
22P39 0.22 Oil Incubation 9 -0.145400678025 -0.171058987178
45P164 0.45 Oil Incubation 64 -0.160359423757 0.439794230436
22P19 0.22 Oil Incubation 9 -0.138575642598 -0.151960075733
45P313 0.45 Oil Incubation 13 -0.176079013758 0.050997652543
45BL64 0.45 Control 64 0.0288347544654 -0.1458771364
45P330 0.45 Oil Incubation 30 -0.11228777477 0.204236728786
45P36 0.45 Oil Incubation 6 -0.0761185616066 -0.121261486374
45P130 0.45 Oil Incubation 30 -0.094228858564 0.100652965768
22BL0 0.22 Control 0 0.239232948872 -0.036785772233
22P213 0.22 Oil Incubation 13 -0.18113904214 -0.0287178094404
22BL6 0.22 Control 6 -0.0600555194535 -0.194650371605
45P33 0.45 Oil Incubation 3 0.340399779328 0.0171852693215
45P30 0.45 Oil Incubation 0 0.431938929593 0.0196841881272
45BL6 0.45 Control 6 0.357256101911 -0.0188140900434
45BL9 0.45 Control 9 0.303457604982 -0.0443188527517
22P316 0.22 Oil Incubation 16 -0.164460159926 -0.0129812590712
22P313 0.22 Oil Incubation 13 -0.188614376178 0.00058901489101
45P39 0.45 Oil Incubation 9 -0.149217759045 -0.167311625113
22P131 0.22 Oil Incubation 30 -0.0651994575885 0.0656871734895
22BL31 0.22 Control 30 0.335856403002 0.0724160545763
45P19 0.45 Oil Incubation 9 -0.142421940226 -0.160880360066
22P331 0.22 Oil Incubation 30 -0.0500301022508 0.0416246477279
45BL0 0.45 Control 0 0.381611397751 0.0200030589204
22P113 0.22 Oil Incubation 13 -0.192581008392 -0.00938532222554
45P16 0.45 Oil Incubation 6 -0.085999339488 -0.121939087208
45BL3 0.45 Control 3 0.332023830601 0.036732406277
45P13 0.45 Oil Incubation 3 0.315664193595 0.0248256483492
45P216 0.45 Oil Incubation 16 -0.172909653606 0.030607355003
45P213 0.45 Oil Incubation 13 -0.18326004657 0.00570693606563
22P29 0.22 Oil Incubation 9 -0.150029591452 -0.16569360913
22P26 0.22 Oil Incubation 6 -0.110220935459 -0.134301793132
45P364 0.45 Oil Incubation 64 -0.100468231806 0.248528601323
45BL16 0.45 Control 16 0.341331109786 0.00915399605284
45BL13 0.45 Control 13 0.155782679403 -0.17960863481
45P230 0.45 Oil Incubation 30 -0.0452735875021 0.0316966419992
and the code: 和代码:
PCoA <- read.delim(file.choose(), header=T)
attach(PCoA)
library(ggplot2)
PCoA$Time <- as.character(PCoA$Time)
PCoA$Filter <- as.character(PCoA$Filter)
cols <- c("0" = "red","3" = "blue","6" = "brown", "9" = "hotpink", "13" = "orange","16" = "olivedrab","30" = "azure3", "64" = "purple")
ggplot(PCoA, aes(x=Coord1, y=Coord2, shape=Type, colour=Time)) +
scale_colour_manual(values = cols, breaks=c("0","3","6","9","13","16","30","64")) +
geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") +
geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) +
theme(legend.text = element_text(size = 16),
legend.title = element_text(size=18),
axis.title.x = element_text(size=18, vjust=-0.7),
axis.title.y = element_text(size=18, vjust=2)) +
theme(axis.text = element_text(colour = "black", size="16")) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "white"))
You need to use the right shapes (21:25) and to specify your fill. 您需要使用正确的形状 (21:25)并指定填充。 This post has some good tips. 这篇文章有一些很好的技巧。 Although at this point, your data are becoming more difficult to illustrate with a different outline and fill color. 尽管此时,使用不同的轮廓和填充颜色来说明数据变得越来越困难。 (Maybe instead code Time as your fill and use lighter hues for one filter and darker for the other filter -or- make Filter type your outline color). (也许改为将“ 时间”编码为填充,并为一个滤镜使用较浅的色相,为另一滤镜使用较深的色-或-使“ 滤镜”键入您的轮廓色)。
Here is your graph edited with: 这是用以下图形编辑的图形:
as.character()
within ggplot instead of in the original data frame. 在ggplot中而不是原始数据帧中编码为as.character()
数据格式。 scale_parameter_manual()
在scale_parameter_manual()
指定的图例名称 aes()
填充在aes()
定义 override.aes
使用override.aes
修复填充图例 ggplot(PCoA, aes(x=Coord1, y=Coord2, shape=as.character(Type), colour=as.character(Time), fill=as.character(Filter) )) + #Edit
scale_colour_manual(name="Time", values = cols, breaks=c("0","3","6","9","13","16","30","64")) + #Edit
geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") +
geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) +
theme(legend.text = element_text(size = 16),
legend.title = element_text(size=18),
axis.title.x = element_text(size=18, vjust=-0.7),
axis.title.y = element_text(size=18, vjust=2)) +
theme(axis.text = element_text(colour = "black", size="16")) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "white")) +
scale_shape_manual(name="Type", values=c(21,24)) + #Edit
scale_fill_manual(name="Filter", values=c("black", "white")) + #Edit
guides(fill = guide_legend(override.aes = list( #Edit
fill = c("black", "white"), #Edit
shape=22, #Edit
colour="black") )) #Edit
Your original plot + edited plot: 您的原始图+编辑后的图:
Edit 编辑
OP asks how the fill can be fine-tuned so that the fill defined by Variable 1 is determined by Variable 2, ie if Variable 2 is A, then the points are color-filled but if Variable 2 is B then all the points are white-filled. OP询问如何对填充进行微调,以使变量1定义的填充由变量2确定,即,如果变量2为A,则这些点是彩色填充的,但是如果变量2为B,则所有点都是白色的-填充。 Since a third Variable dictates the shape, this adds an additional complication. 由于第三个变量指示形状,因此增加了其他复杂性。
There are a couple potential solutions: 有两种潜在的解决方案:
aes(shape=interaction(Variable2, Variable3))
and specify shapes that allow only-colors (15:20) and colors+fill (21:25). 通过aes(shape=interaction(Variable2, Variable3))
并指定仅允许颜色(15:20)和颜色+填充(21:25)的形状。 fill
does not need to be defined. 然后,无需定义fill
。 scale_fill_manual(values = alpha(c("blue", "red"), .3))
(This solution is probably not feasible for this example.) 加载库(比例)并设置填充alpha属性( 此处为最后一个示例),即scale_fill_manual(values = alpha(c("blue", "red"), .3))
(此示例在该示例中可能不可行。) geom_point(data= subset, aes())
分别绘制点及其美学:即geom_point(data= subset, aes())
Approach #1: 方法1:
ggplot(PCoA, aes(x=Coord1, y=Coord2,
shape=interaction(as.character(Type), Filter),
colour=as.character(Time),
fill=as.character(Time) )) + #Edit
scale_colour_manual(name="Time", values = cols,
breaks=c("0","3","6","9","13","16","30","64")) + #Edit
geom_point(size=7) + xlab("PC1 (39.7%)") + ylab("PC2 (20.2%)") +
geom_hline(aes(yintercept=0)) + geom_vline(aes(xintercept=0)) +
theme(legend.text = element_text(size = 16),
legend.title = element_text(size=18),
axis.title.x = element_text(size=18, vjust=-0.7),
axis.title.y = element_text(size=18, vjust=2),
legend.key = element_rect(fill=NA)) + #Edit
theme(axis.text = element_text(colour = "black", size="16")) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "white")) +
scale_shape_manual(name="Type/Filter", values=c(21, 16, 24, 17),
labels=c("Control, 0.22", "Incubation, 0.22",
"Control, 0.45", "Incubation, 0.45") ) + #Edit
scale_fill_manual(name="Filter", values=rep("white", length(cols)), guide=FALSE ) #Edit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.