如何用geom_point在NMDS图可视化三因子并设置填充/空心形状?
Fixing NMDS Plot with Three Factors (Fill/Hollow Shapes for Fungi/NF)
Got it, let's sort out this NMDS plot styling issue for you! The core problem here is that most default ggplot shapes (0-20) don't support separate fill and border color controls—you need to use fillable shapes (numbers 21-25) to make the hollow/filled style work with your Fungi variable.
Step-by-Step Solution
Pick fillable shapes: Shapes 21-25 are designed to let you set a separate
colour(border) andfill(inner color). For example:- 21 = circle with fill/border
- 24 = upward triangle with fill/border
- 22 = square with fill/border
Map your variables correctly:
- Use
colour=Locationfor border color (to distinguish locations) - Use
shape=Insectto assign different fillable shapes to each insect type - Use
fill=Fungito control whether the shape is filled or hollow
- Use
Define manual scales:
scale_shape_manualto link each insect type to a fillable shapescale_fill_manualto set filled color forFungiand transparent/hollow forNF
Full Working Code
# 1. Define shape mappings (match your actual Insect category names) shape_values <- c( "Aphid" = 21, # Circle with fill/border "Beetle" = 24, # Upward triangle with fill/border "Moth" = 22 # Square with fill/border ) # 2. Define fill mappings: Fungi gets a solid color, NF gets transparent (hollow) fill_values <- c( "Fungi" = "#2E8B57", # Dark green fill "NF" = NA # Transparent (hollow) ) # 3. Build the NMDS plot ggplot(data = NMDS2_plsMet, aes(x = x, y = y)) + geom_point( aes(colour = Location, shape = Insect, fill = Fungi), size = 3 # Adjust size for better visibility ) + scale_shape_manual(values = shape_values) + scale_fill_manual( values = fill_values, na.value = "transparent" # Ensure NA shows as hollow ) + scale_colour_viridis_d() + # Optional: Prettier color scale for Location theme_bw() + labs( title = "NMDS Plot with Three Factors", x = "NMDS Axis 1", y = "NMDS Axis 2" )
Key Notes
- Adjust for your data: Replace the insect names (
Aphid,Beetle, etc.) and color values with your actual dataset's categories and preferred colors. - Reverse hollow/filled: If you want
NFto be filled andFungito be hollow, just swap the values infill_values. - Continuous Location: If
Locationis a continuous variable, usescale_colour_viridis_c()instead ofscale_colour_viridis_d().
内容的提问来源于stack exchange,提问作者user2814482
相关产品推荐
相关产品推荐

