如何为R语言maptree绘图函数添加点大小图例?
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()andcontour2D()calls. This ensures it draws on top of your map, not behind it. dbh = dat$dbh: I noticed you were usingdat$dbhin 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 thecexvalues 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 thecexvalue to make the text smaller. - Double-check that your
victoria.datdataframe has adbhcolumn (it looks like it does from your original code, but this is a common source of errors!).
内容的提问来源于stack exchange,提问作者Student

