ggplot2:没有填充的geom_polygon

时间:2014-08-22 14:07:59

标签: r ggplot2

我希望,你不需要这个问题的数据,因为我相信我只是犯了一个愚蠢的语法错误。以下代码:

ggplot()+
  geom_point(data=sites, aes(x=NMDS1, y=NMDS2, shape=group), colour="grey") +  
  geom_point(data=species, aes(x=NMDS1, y=NMDS2, color=phyla), size=3, shape=20) + scale_colour_manual(values=Pal1) +
  geom_segment(data = BiPlotscores, aes(x = 0, xend = NMDS1, y= 0, yend = NMDS2),
               arrow = arrow(length = unit(0.25, "cm")), colour = "black") +
  geom_text(data = BiPlotscores, aes(x = 1.1*NMDS1, y = 1.1*NMDS2, label = Parameters), size = 3) + coord_fixed()+
  theme(panel.background = element_blank()) +
  geom_polygon(data = hulls, aes(x=NMDS1, y=NMDS2, colour=phyla, alpha = 0.2))

导致以下结果:

enter image description here

(这不是最终产品:))。 我希望多边形没有填充,或者只是整齐地填充。我不希望它们是灰色的,当然。填充不会做任何事情,显然摆弄alpha也不会改变任何东西。

任何想法都是超级欢迎。非常感谢你!

"船体"来自以下代码(在这里找到):

#find hulls
library(plyr)
find_hull <- function(df) df[chull(df$NMDS1, df$NMDS2), ]
hulls <- ddply(species , "phyla", find_hull)

2 个答案:

答案 0 :(得分:29)

如果您想要透明填充,请在fill=NA - 规范之外执行aes()

library(ggplot2) data <- data.frame(y=c(2,2,1), x=c(1,2,1)) ggplot(data) + geom_polygon(aes(x=x, y=y), colour="black", fill=NA)

答案 1 :(得分:3)

its an old question but maybe if someone else runs into the same problem:

here is the code to go from a matrix to a nice nmds plot:

1) create dummy matrix

MAT <- matrix( sample( 1:200, 100), nrow = 10, 
           dimnames = list( LETTERS[ 1:10]))

2) calculate distance matrix

DIST <- dist(MAT)

3) calculate MDS

fit <- monoMDS(DIST, k=2)

4) extract points for plotting

fitp <- data.frame(fit$points)
fitp$sample <- rownames(fitp)

5) add grouping factor

fitp$group <- rep( c( "group1", "group2"), each=5)

6) define function to find hulls

find_hull <- function(df) df[chull(df$MDS1, df$MDS2), ]

7) find hulls

hulls <- ddply(fitp, .(group), find_hull)

8) plot data

ggplot( fitp, aes( x = MDS1, y = MDS2))+
  geom_point( data = fitp, aes( colour = group, shape = group, size = 4))+
  geom_text( data = fitp, aes( colour = group, label = sample, hjust = -0.7, size = 4))+
  geom_polygon( data = hulls, aes( alpha = 0.8, fill = group))+
  theme_bw()+
  guides(size=F,alpha=F)

enter image description here