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

如何在R语言洛伦兹曲线图右下角添加Gini指数文本?

Fixing Gini Text Placement on Your Lorenz Curve

Hey there! The issue you’re facing with the text() function is likely because the Lorenz curve plot (from plot.Lc()) uses cumulative proportion axes (0 to 1 for both x and y) instead of your raw population/income values. If you tried using raw numbers like x=70093, that’s way outside the plot’s visible range, so the text never shows up.

Here are two reliable ways to add the Gini coefficient to the bottom-right corner of your plot:

Method 1: Use Relative Coordinates (Simplest)

Since the plot’s axes are scaled to 0-1, you can pick a relative position near the bottom-right (e.g., x=0.9, y=0.1) and align the text properly:

# Load required package and create your data frame
library(ineq)

POPULATION = c(0,7009,14019,21028,28037,35047,42056,49065,56074,63084,70093)
INCOME = c(0,0,0,0,0,195680.26,550667.039999996,1034464.62,1821489.83,3360160.17999999,18979682.83)
DF = data.frame(POPULATION, INCOME)

# Calculate Gini coefficient
G = round(ineq(DF$INCOME, type="Gini"), 3)

# Plot the Lorenz curve
plot(Lc(DF$INCOME), col="darkred", lwd=2, main="Lorenz Curve", 
     xlab="Cumulative Population Proportion", ylab="Cumulative Income Proportion")

# Add Gini text to the bottom-right corner
text(x = 0.9, y = 0.1, labels = paste("Gini =", G), 
     adj = c(1, 0),  # Right-align x, bottom-align y for clean corner placement
     cex = 0.8)      # Adjust text size if needed

Method 2: Use Plot Boundaries (More Robust)

If you want the text to stay anchored in the corner even if you adjust the plot’s axes, use par("usr") to get the exact boundaries of the plot area:

# After plotting the Lorenz curve:
usr_range = par("usr")  # Returns a vector: c(xmin, xmax, ymin, ymax)

# Place text at 90% of the x-axis width, 10% of the y-axis height from the bottom
text(x = usr_range[2] * 0.9, 
     y = usr_range[3] + (usr_range[4] - usr_range[3]) * 0.1, 
     labels = paste("Gini =", G), 
     adj = c(1, 0), 
     cex = 0.8)

Quick Notes:

  • The adj = c(1,0) parameter ensures the text sits neatly in the corner without overflowing the plot area.
  • I updated the axis labels to reflect that they show cumulative proportions (standard for Lorenz curves) instead of raw population/income values, which makes the plot more interpretable.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:39:06