【问题标题】:R not plotting all lines/pointsR没有绘制所有线/点
【发布时间】:2020-10-19 19:50:12
【问题描述】:

我有以下代码:

library(purrr)
library(dplyr)
library(ineq)
library(ggplot2)
taxations <- c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)
N = 1000000
gamma <- 1/12 #scale parameter
beta <- 0.95#shape parameter
u <- runif(N)
v <- runif(N)
tau <- -gamma*log(u)*(sin(beta*pi)/tan(beta*pi*v)-cos(beta*pi))^(1/beta)

make_lorenz_2 <- function(i, tau) {
  newtau = sort(tau)
  transfers = i * sort(tau, decreasing = T)
  newtau = ((1 - i) * tau) + transfers
  OX <- sort(newtau)
  CumWealth <- cumsum(OX)/sum(newtau)
  PoorPopulation <- c(1:N)/N
  index <- c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.99,0.999,0.9999,0.99999,0.999999,1)*N
  QQth <- CumWealth[index]
  x <- PoorPopulation[index]
  data.frame(x,QQth, Gini(newtau))
}


Lorenzdf1 <- purrr::map(taxations, tau = tau, make_lorenz_2) %>% 
  setNames(taxations) %>% 
  bind_rows(.id = "taxations")

Lorenzdf1
cols <- c("0" = "black", "0.1"="blue","0.2"="green","0.3"="red", "0.4" = "grey", "0.5" = "pink", "0.6"="yellow","0.7"="brown","0.8"="orange", "0.9" = "purple", "1" = "darkgreen")
g <- ggplot(data=Lorenzdf1, aes(x=x, y=QQth, colour = taxations)) +
  geom_point() + 
  geom_line() +
  ggtitle("Lorenz curves after flat taxation") + 
  xlab("Cumulative share of people from lowest to highest wealth") +
  ylab("Cumulative share of wealth") +
  scale_color_manual(name="Rate of taxation",values=cols)

这会生成链接图像。一切都很完美,但我不确定是否有些线条没有绘制,或者它们是否只是靠得很近,以至于有些线条不显示。如果有人能澄清一下,那真的很有帮助。

【问题讨论】:

    标签: r ggplot2 dplyr purrr


    【解决方案1】:

    一切都在那里,但是这些点太靠近了,让您过度绘制。考虑x==0.3:

    > Lorenzdf1[which(Lorenzdf1$x==0.3),]
    
        taxations   x       QQth Gini.newtau.
    3           0 0.3 0.02526036    0.7225117
    19        0.1 0.3 0.03738772    0.6916739
    35        0.2 0.3 0.04451107    0.6708302
    51        0.3 0.3 0.04888652    0.6566574
    67        0.4 0.3 0.05129901    0.6483685
    83        0.5 0.3 0.05207639    0.6456338
    99        0.6 0.3 0.05131045    0.6483590
    115       0.7 0.3 0.04888973    0.6566413
    131       0.8 0.3 0.04451143    0.6708137
    147       0.9 0.3 0.03738566    0.6916665
    163         1 0.3 0.02526036    0.7225117
    

    您可以看到,例如,重叠模式基本上以 0.5 为最大值的税收为中心,然后在此附近成对出现。因此,我希望这些点会过度绘制,当查看 x=0.3 附近的放大部分时,您应该会在图中看到 6 条线,这实际上就是您所看到的(需要coord_cartesian(),这样您就不会失去与行的其余数据的连接):

    # where p == your plot
    p + coord_cartesian(xlim=c(0.25,0.35), ylim=c(0.02,0.055))
    

    如果您想要区分,您需要以不同的方式绘制数据。当您绘制x=taxations 并设置color=x 时,重叠的预期关系更加明显(以最大值0.5 左右为中心,并在“税收”的每个方向上下降)。 (请注意,您还需要设置 group=x 才能正常工作,以便在点之间正确绘制线条):

    ggplot(Lorenzdf1, aes(x=taxations, y=QQth, color=factor(x), group=factor(x))) +
     geom_line() + geom_point()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2015-08-03
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      相关资源
      最近更新 更多