簡體   English   中英

R 未繪制所有線/點

[英]R not plotting all lines/points

我有以下代碼:

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)

這會產生鏈接的圖像。 一切都很完美,但我不確定是否有些線條沒有繪制,或者它們是否只是靠得很近,以至於有些線條不顯示。 如果有人能澄清這將真的有幫助。

在此處輸入圖像描述

一切都在那里,但這些點太靠近了,讓你過度繪制。 考慮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 附近的放大部分時,您應該在 plot 中看到 6 行,這實際上就是您所看到的( coord_cartesian()是必需的,這樣您就不會丟失與rest的連接數據為線):

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

在此處輸入圖像描述

如果您想要區分,您需要對您的數據進行不同的 plot。 當您 plot 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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM