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

如何为R语言maptree绘图函数添加点大小图例?

Adding a Legend to Your Western Hemlock Tree Map

Hey there! I see you're working on visualizing Western Hemlock tree sizes with your maptree.r function, and you're stuck on adding a legend to explain the different point sizes. No worries—this is a common first-time question, and we can fix it easily with R's built-in legend() function.

First, let's recap: your function uses three different point sizes to represent different DBH ranges. The legend needs to mirror those sizes and label them clearly. You'll want to add the legend after all your existing plotting commands (so it draws on top of your map, not before).

Step-by-Step Fix

Here's your modified function with the legend added, plus a couple of tiny fixes to clean up redundant code:

maptree.r = function(dat, spname = "HL") { 
  x = dat$x 
  y = dat$y 
  z = dat$z 
  sp = dat$sp 
  dbh = dat$dbh  # Added this line to make dbh accessible (you used it in points calls but didn't assign it!)
  
  xy.interp = interp(x,y,z) 
  image2D(main = "Different Sizes of Western Hemlock Trees", 
          xlab = "X Coordinate",  # Replaced "?" with meaningful labels
          ylab = "Y Coordinate", 
          xy.interp, col = topo.colors(12), sp = T) 
  contour2D(xy.interp$z, x = xy.interp$x, y = xy.interp$y, 
            nlevels = 15, col = "black", add = T) 
  
  # Plot points with consistent DBH conditions
  points(x[sp == spname & dbh < 10], y[sp == spname & dbh < 10], 
         col = "blue", pch = 1)  # Removed redundant xlab/ylab here
  points(x[sp == spname & dbh >= 10 & dbh < 25], y[sp == spname & dbh >= 10 & dbh < 25], 
         col = "blue", cex = 2, pch = 1) 
  points(x[sp == spname & dbh >= 25], y[sp == spname & dbh >= 25], 
         col = "blue", cex = 3, pch = 1)  # Fixed inconsistent condition (was >25 and >=25)
  
  # --- THIS IS THE NEW LEGEND CODE ---
  legend(
    x = "topright",  # Position: use named positions for ease, or exact coordinates like (100, 100)
    legend = c("DBH < 10 cm", "10 ≤ DBH < 25 cm", "DBH ≥ 25 cm"),  # Labels for each size
    pch = 1,  # Same point shape as your points() calls
    col = "blue",  # Same color as your tree points
    pt.cex = c(1, 2, 3),  # Match the cex values from your points
    cex = 0.8,  # Adjust text size if needed
    title = "Western Hemlock DBH",  # Optional title for the legend
    bty = "n"  # Remove legend box for a cleaner look (use "o" to keep it)
  )
} 

# Run the function as before
maptree.r(victoria.dat, "HL")

Key Explanations:

  • Where to put the legend: I added it at the end of the function, after all points() and contour2D() calls. This ensures it draws on top of your map, not behind it.
  • dbh = dat$dbh: I noticed you were using dat$dbh in your points filters but hadn't assigned it to a variable first—this fixes that so the function doesn't throw an error.
  • Legend parameters:
    • x = "topright": Uses a named position so you don't have to calculate exact coordinates (other options: "topleft", "bottomright", "bottomleft").
    • pt.cex = c(1,2,3): Directly matches the cex values you used for each point size, so the legend points look identical to your plot points.
    • bty = "n": Removes the box around the legend for a cleaner look—feel free to change this to "o" if you want a box.

Bonus Tips:

  • If the legend overlaps with your tree points, adjust the position (try "bottomleft" instead) or tweak the cex value to make the text smaller.
  • Double-check that your victoria.dat dataframe has a dbh column (it looks like it does from your original code, but this is a common source of errors!).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:48:22