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

如何在R语言coefplot中移除零值水平线或将其设为虚线?

问题:修改coefplot中的零值水平线

我使用以下代码生成了该coefplot图:

coefplot(list(
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ > 9 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh),
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ > 5 & educ < 10 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh),
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ < 6 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh)), main = "Effect of BTA on women's relative income \n(by eduction level)")
legend("bottomleft", col = 1:3, pch = 1, lwd = 2, cex = 0.7, bty = "n", 
       legend = c("Agriculture", "Wearing apparel and leather", "Manufacturing"))

生成的coefplot图

请问是否可以移除图中的零值水平线,或者将该水平线设置为虚线?


解决方案

针对fixest包的coefplot()函数,你可以通过内置参数直接调整零值水平线:

1. 移除零值水平线

在coefplot()调用中添加zero_line = FALSE参数即可关闭这条线:

coefplot(list(
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ > 9 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh),
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ > 5 & educ < 10 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh),
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ < 6 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh)), 
  main = "Effect of BTA on women's relative income \n(by eduction level)",
  zero_line = FALSE) # 移除零水平线
legend("bottomleft", col = 1:3, pch = 1, lwd = 2, cex = 0.7, bty = "n", 
       legend = c("Agriculture", "Wearing apparel and leather", "Manufacturing"))

2. 将水平线设置为虚线

添加zero_line_lty = "dashed"(或用数字2,R中线条类型编码:1=实线,2=虚线):

coefplot(list(
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ > 9 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh),
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ > 5 & educ < 10 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh),
  feols(inc_ratio ~ provtariff | hhid02 + year,
        subset(inc_0206_spouse_p, Female == 1 & educ < 6 & (year == 2002 | year == 2006)),
        weights = ~hhwt, 
        vcov = ~tinh)), 
  main = "Effect of BTA on women's relative income \n(by eduction level)",
  zero_line_lty = "dashed") # 设置为虚线
legend("bottomleft", col = 1:3, pch = 1, lwd = 2, cex = 0.7, bty = "n", 
       legend = c("Agriculture", "Wearing apparel and leather", "Manufacturing"))

额外说明:你还可以通过zero_line_col调整线条颜色,zero_line_lwd调整线条粗细,按需自定义样式。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 13:21:27