ggplot2: geom_polygon with no fill

2020-02-26 05:28发布

I hope, you dont need data for this problem, because I believe I am just making a stupid syntax error. The following code:

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

leads to the following result:

enter image description here

(This is not the final product :)). I would like to have the polygons unfilled, or very just neatly filled. I do not want them to be greyish, for sure. Fill doesnt do anything, and apparently fiddling with alpha doesnt change anything, either.

Any ideas are superwelcome. Thank you very much!

"Hulls" is coming from the following code (as found here somewhere):

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

标签: r ggplot2
2条回答
狗以群分
2楼-- · 2020-02-26 06:10

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

查看更多
We Are One
3楼-- · 2020-02-26 06:28

If you want transparent fill, do fill=NA outside the aes()-specification.

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)

查看更多
登录 后发表回答