You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

  1. Pick fillable shapes: Shapes 21-25 are designed to let you set a separate colour (border) and fill (inner color). For example:

    • 21 = circle with fill/border
    • 24 = upward triangle with fill/border
    • 22 = square with fill/border
  2. Map your variables correctly:

    • Use colour=Location for border color (to distinguish locations)
    • Use shape=Insect to assign different fillable shapes to each insect type
    • Use fill=Fungi to control whether the shape is filled or hollow
  3. Define manual scales:

    • scale_shape_manual to link each insect type to a fillable shape
    • scale_fill_manual to set filled color for Fungi and transparent/hollow for NF

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 NF to be filled and Fungi to be hollow, just swap the values in fill_values.
  • Continuous Location: If Location is a continuous variable, use scale_colour_viridis_c() instead of scale_colour_viridis_d().

内容的提问来源于stack exchange,提问作者user2814482

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:21:35