0
我的數據集由36個「站點」組成,其中12 x 3個站點是一個複製三元組。數據集有兩個系列「R」和「D」。一些R和D - 三元組相互關聯,由字母后的數字索引表示:因此,R2i和D2i系列屬於R3i和D3i等等。作爲一個轉折R7i和R1i在D世界中沒有相同的東西。 在圖中,所有四個網站複製品的着色方式不同,但我想對相關的組進行相同的着色,以使相關的三角形顯示爲相同的顏色。根據不同的因素類別分配單獨的色板
在這個例子中,三角形D2和R2應該是相同的顏色,D3和R3也是一樣的。
下面是代碼:
sites <- structure(list(Sample = c("R21", "R22", "R23", "R31", "R32",
"R33", "D21", "D22", "D23", "D31", "D32", "D33"), X = c(-0.00591212751574749,
0.341048420056647, 0.430793063675178, 0.432479460946573, 0.239326674010454,
0.202491749301479, -0.951185318446942, -0.596668772966298, -0.939366882995036,
-0.522651768953026, -0.23338622249853, -0.176826307377661), Y = c(-0.0742136034318636,
-0.345049510288858, 0.183433103229042, -0.108409938703458, -0.0276483081985604,
-0.129547387046024, 0.26657938925131, 0.759126587423588, 0.103436047537972,
-0.178345595609023, -0.116710668776298, -0.0292021298523572),
Treatment = c("B", "B", "B", "C", "C", "C", "H", "H", "H",
"I", "I", "I"), Group = structure(c(2L, 2L, 2L, 3L, 3L, 3L,
2L, 2L, 2L, 3L, 3L, 3L), .Label = c("A", "B", "C", "D", "E",
"F", "G"), class = "factor")), .Names = c("Sample", "X",
"Y", "Treatment", "Group"), row.names = c(4L, 5L, 6L, 7L, 8L,
9L, 22L, 23L, 24L, 25L, 26L, 27L), class = "data.frame")
library(plyr)
find_hull <- function(df) df[chull(df$X, df$Y), ]
hulls <- ddply(sites , "Treatment", find_hull)
ggplot()+
geom_point(data=sites, aes(X, Y, col=Treatment), alpha=1,show_guide=FALSE) +
geom_text(data=sites, aes(X, Y, label=Sample), size=3, show_guide=FALSE) +
geom_polygon(data = hulls, aes(X, Y, colour=Treatment, fill=Treatment), lty="dashed", alpha = 0.2, show_guide=FALSE) +
theme_bw()+
coord_fixed()
Treatment
給網站三重和Group
指示相關羣體。 Dataframe hulls
需要Treatment
中的因子水平才能正確連接點:如果我將Group
傳遞給顏色參數,所有點將被連接。
ggplot()+
geom_point(data=sites, aes(X, Y, col=Treatment), alpha=1,show_guide=FALSE) +
geom_text(data=sites, aes(X, Y, label=Sample), size=3, show_guide=FALSE) +
geom_polygon(data = hulls, aes(X, Y, colour=Group, fill=Group), lty="dashed", alpha = 0.2, show_guide=FALSE) +
theme_bw()+
coord_fixed()
所以我想,如果我可以爲單個colorpalettes到每個因素,所以我可以分配相同的顏色,如果需要的因子水平。
任何想法表示讚賞,謝謝!