如何在R(ggplot?)中为因子绘制一条漂亮的洛伦兹曲线
我需要一个关于不同因素的不同分布的好图来写我的论文。似乎只有如何在R(ggplot?)中为因子绘制一条漂亮的洛伦兹曲线,r,plot,ggplot2,distribution,R,Plot,Ggplot2,Distribution,我需要一个关于不同因素的不同分布的好图来写我的论文。似乎只有包(ineq)的标准方法足够灵活 然而,它不允许我在课堂上加点(见下面的评论)。看到它们很重要,最好是单独命名。这可能吗 Distr1 <- c( A=137, B=499, C=311, D=173, E=219, F=81) Distr2 <- c( G=123, H=400, I=250, J=16) Distr3 <- c( K=145, L=600, M=120) library(ineq) Distr1
包(ineq)
的标准方法足够灵活
然而,它不允许我在课堂上加点(见下面的评论)。看到它们很重要,最好是单独命名。这可能吗
Distr1 <- c( A=137, B=499, C=311, D=173, E=219, F=81)
Distr2 <- c( G=123, H=400, I=250, J=16)
Distr3 <- c( K=145, L=600, M=120)
library(ineq)
Distr1 <- Lc(Distr1, n = rep(1,length(Distr1)), plot =F)
Distr2 <- Lc(Distr2, n = rep(1,length(Distr2)), plot =F)
Distr3 <- Lc(Distr3, n = rep(1,length(Distr3)), plot =F)
plot(Distr1,
col="black",
#type="b", # !is not working
lty=1,
lwd=3,
main="Lorenz Curve for My Distributions"
)
lines(Distr2, lty=2, lwd=3)
lines(Distr3, lty=3, lwd=3)
legend("topleft",
c("Distr1", "Distr2", "Distr3"),
lty=c(1,2,3),
lwd=3)
Distr1要显示问题,只需要Distr1
;在发帖前脱掉衣服很好
library(ineq)
Distr1 <- c( A=137, B=499, C=311, D=173, E=219, F=81)
Distr1 <- Lc(Distr1, n = rep(1,length(Distr1)), plot =F)
plot(Distr1$p,Distr1$L,
col="black",
type="b", # it should be "b"
lty=1,
lwd=3,
main="Lorenz Curve for My Distributions"
)
库(ineq)
发行版1如果您真的想使用ggplot
,这里有一个简单的解决方案
# Compute the Lorenz curve Lc{ineq}
library(ineq)
Distr1 <- c( A=100, B=900, C=230, D=160, E=190, F=40, G=5,H=30,J=60, K=500)
Distr1 <- Lc(Distr1, n = rep(1,length(Distr1)), plot =F)
# create data.frame from LC
p <- Distr1[1]
L <- Distr1[2]
Distr1_df <- data.frame(p,L)
# plot
ggplot(data=Distr1_df) +
geom_point(aes(x=p, y=L)) +
geom_line(aes(x=p, y=L), color="#990000") +
scale_x_continuous(name="Cumulative share of X", limits=c(0,1)) +
scale_y_continuous(name="Cumulative share of Y", limits=c(0,1)) +
geom_abline()
#计算洛伦兹曲线Lc{ineq}
图书馆(ineq)
Distrit1由于有一个包(GGORENZ
)自动处理ggplot的洛伦兹曲线,我添加了以下内容:
library(ggplot2)
library(gglorenz)
Distr1 <- c( A=137, B=499, C=311, D=173, E=219, F=81)
x <- data.frame(Distr1)
ggplot(x, aes(Distr1)) +
stat_lorenz() +
geom_abline(color = "grey")
库(ggplot2)
图书馆(新西兰)
谢谢你。我在帖子里加错了一行。对我来说,即使加上引号也不行。执行代码了吗?嗯,是的,因为我在你的代码中发现了错误。“不起作用”:我推荐财富::财富(“爆炸”)
(您需要财富套餐)。谢谢。你能解释一下如何使用它吗?谢谢你:)!祝您有个美好的一天。