简体   繁体   English

R 未绘制所有线/点

[英]R not plotting all lines/points

I have the following code:我有以下代码:

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)

This produces the linked image.这会产生链接的图像。 Everything works perfect but I'm not sure if some lines are not plotting or if they are just close together so that some don't show.一切都很完美,但我不确定是否有些线条没有绘制,或者它们是否只是靠得很近,以至于有些线条不显示。 If someone could clarify that would really help.如果有人能澄清这将真的有帮助。

在此处输入图像描述

It's all there, but the points are too close together, giving you overplotting.一切都在那里,但这些点太靠近了,让你过度绘制。 Consider the points where x==0.3 :考虑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

You can see, for example the overlap pattern is basically centered around taxations of 0.5 as the maximum and then in pairs around that.您可以看到,例如,重叠模式基本上以 0.5 为最大值的税收为中心,然后成对出现。 I would therefore expect that those points would overplot and you should see 6 lines in the plot when viewing a zoomed in portion around x=0.3, which is in fact what you see ( coord_cartesian() is required so that you don't loose the connection with the rest of the data for the lines):因此,我希望这些点会过度绘制,当查看 x=0.3 附近的放大部分时,您应该在 plot 中看到 6 行,这实际上就是您所看到的( coord_cartesian()是必需的,这样您就不会丢失与rest的连接数据为线):

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

在此处输入图像描述

If you want differentiation at all, you need to plot your data differently.如果您想要区分,您需要对您的数据进行不同的 plot。 The relationship of what to expect for overlap is even more apparent (centered with maxima around 0.5 and dropping off in each direction for "taxations") when you plot x=taxations and set color=x .当您 plot x=taxations并设置color=x时,重叠的预期关系更加明显(以最大值为中心,大约 0.5 并且在“税收”的每个方向上下降)。 (note you also need to set group=x for this to work as well to draw the lines properly among the points): (请注意,您还需要设置group=x才能在点之间正确绘制线条):

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM