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

如何在R中将绘图图例置于图表上方?求代码修改方案

如何将R绘图的图例移至图表上方?

现有R代码绘制利润曲线时,图例出现在图表下方,需要调整为居中图例在图表上方的效果。

现有代码

#data
tot_profit_without = c(5.67856,5.19891,5.6146,5.91454,5.10273,5.13607,4.7871,4.96507,4.43748,4.02154,3.21159,2.97219,2.27968,2.06904,1.95625,1.86762,1.25334,1.30521,0.65476,0.79812,0.91899)

tot_profit_with = c(2*10^(-7),8*10^(-7),4.5*10^(-6),2.08*10^(-5),5.81*10^(-5),0.000201,0.000436,0.001166,0.00218,0.003718,0.004665,0.007423,0.008421,0.011651,0.015575,0.021022,0.044973,0.385499,0.358526,0.608648,0.790532)

app2 = data.frame(tot_profit_without,tot_profit_with)

#Let's plot
plot(x = c(seq(0.0001,0.0016,by = 0.0001),seq(0.002,0.006,by = 0.001)),y = app2$tot_profit_without, type = "l", ylim = c(0,6), xaxt = "n", yaxt = "n", xlab = "k", ylab = "Profit",lwd = 2)
axis(2, at = seq(0,6,by = 1),las = 2)
axis(1, at = seq(0, 0.006, by = 0.001))

lines(c(seq(0.0001,0.0016,by = 0.0001),seq(0.002,0.006,by = 0.001)), app2$tot_profit_with, col = "black",lty = 2, lwd = 2)

abline(h = 1, lty = "9222", col = "black", lwd = 2) # lty = 6

legend("topleft", legend = c("without transaction costs taken into consideration", "with transaction costs taken into consideration"),
   col = c("black", "black"), lty = c(1, 2),
   lwd = 2, y.intersp = 0.11, horiz = FALSE, text.height = 0.8,
   inset = c(0, -0.3), xpd = TRUE, x.intersp = 0.5,
   seg.len = 0.5, text.width = 5,xjust = "left", yjust = 0.5,cex = 0.5)

修改方案

需要调整绘图边距和图例参数,以下是修改后的完整代码:

#data
tot_profit_without = c(5.67856,5.19891,5.6146,5.91454,5.10273,5.13607,4.7871,4.96507,4.43748,4.02154,3.21159,2.97219,2.27968,2.06904,1.95625,1.86762,1.25334,1.30521,0.65476,0.79812,0.91899)

tot_profit_with = c(2*10^(-7),8*10^(-7),4.5*10^(-6),2.08*10^(-5),5.81*10^(-5),0.000201,0.000436,0.001166,0.00218,0.003718,0.004665,0.007423,0.008421,0.011651,0.015575,0.021022,0.044973,0.385499,0.358526,0.608648,0.790532)

app2 = data.frame(tot_profit_without,tot_profit_with)

# 修改:增加绘图上边距,为图例预留空间
par(mar = c(5, 4, 5, 2) + 0.1)

#Let's plot
plot(x = c(seq(0.0001,0.0016,by = 0.0001),seq(0.002,0.006,by = 0.001)),y = app2$tot_profit_without, type = "l", ylim = c(0,6), xaxt = "n", yaxt = "n", xlab = "k", ylab = "Profit",lwd = 2)
axis(2, at = seq(0,6,by = 1),las = 2)
axis(1, at = seq(0, 0.006, by = 0.001))

lines(c(seq(0.0001,0.0016,by = 0.0001),seq(0.002,0.006,by = 0.001)), app2$tot_profit_with, col = "black",lty = 2, lwd = 2)

abline(h = 1, lty = "9222", col = "black", lwd = 2) # lty = 6

# 修改:调整图例位置、排列方式及相关参数
legend("top", legend = c("without transaction costs taken into consideration", "with transaction costs taken into consideration"),
   col = c("black", "black"), lty = c(1, 2),
   lwd = 2, y.intersp = 0.8, horiz = TRUE, 
   x.intersp = 0.5, seg.len = 0.5, cex = 0.7)

修改说明

  1. 绘图边距调整:通过par(mar = c(5, 4, 5, 2) + 0.1)增加了上边距,确保图例有足够的显示空间,不会和图表内容重叠。
  2. 图例参数调整:
    • 将位置从"topleft"改为"top",让图例居中显示在图表上方
    • 设置horiz = TRUE,让两个图例项横向排列,匹配目标效果
    • 移除了inset和xpd = TRUE参数,因为现在图例在预留的边距内,不需要向外偏移或超出绘图区域
    • 调整了cex(字体大小)和y.intersp(行间距)参数,优化图例的显示美观度

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 01:58:11