0

我有一个由因子变量(男性和女性)填充的洛伦兹曲线图。这样做很简单,重叠不是问题,因为只有两个因素。

Wage %>%
  ggplot(aes(x = salary, fill = gender)) +
  stat_lorenz(geom = "polygon", alpha = 0.65) +
  geom_abline(linetype = "dashed") +
  coord_fixed() +
  scale_fill_hue() +
  theme(legend.title = element_blank()) +
  labs(x = "Cumulative Percentage of Observations",
       y = "Cumulative Percentage of Wages",
       title = "Lorenz curve by sex")

这提供了以下图表: 由两个因素填充

但是,当我有两个以上的因素(在这种情况下是四个)时,即使我使用对比色,重叠也会成为一个严重的问题。在这个阶段改变alpha并没有多大作用。看一看:

Wage %>%
  ggplot(aes(x = salary, fill = Diploma)) +
  stat_lorenz(geom = "polygon", alpha = 0.8) +
  geom_abline(linetype = "dashed") +
  coord_fixed() +
  scale_fill_manual(values = c("green", "blue", "black", "white")) +
  theme(legend.title = element_blank()) +
  labs(x = "Cumulative Percentage of Observations",
       y = "Cumulative Percentage of Wages",
       title = "Lorenz curve by diploma")

由四个因素填充

在这一点上,我已经尝试了所有不同的调色板、色调、啤酒、手册等。我也尝试过重新排序这些因素,但正如你可以想象的那样,这并不奏效。

我需要的可能是一个参数或函数将所有这些区域堆叠在一起,以便它们都有不同的颜色。有趣的是,我没能找到我要找的东西,并决定寻求帮助。

非常感谢。

4

1 回答 1

0

这个问题被一位亲爱的朋友解决了。这是通过逐层添加分类变量来完成的,而没有将洛伦兹曲线定义为一个整体。

ggplot() + scale_fill_manual(values = wes_palette("GrandBudapest2", n = 4)) +
  stat_lorenz(aes(x=Wage[Wage$Diploma==levels(Wage$Diploma)[3],]$salary, fill=Wage[Wage$Diploma==levels(Wage$Diploma)[3],]$Diploma), geom = "polygon") +
  stat_lorenz(aes(x=Wage[Wage$Diploma==levels(Wage$Diploma)[4],]$salary, fill=Wage[Wage$Diploma==levels(Wage$Diploma)[4],]$Diploma), geom = "polygon") +
  stat_lorenz(aes(x=Wage[Wage$Diploma==levels(Wage$Diploma)[2],]$salary, fill=Wage[Wage$Diploma==levels(Wage$Diploma)[2],]$Diploma), geom = "polygon") +
  stat_lorenz(aes(x=Wage[Wage$Diploma==levels(Wage$Diploma)[1],]$salary, fill=Wage[Wage$Diploma==levels(Wage$Diploma)[1],]$Diploma), geom = "polygon") +
  geom_abline(linetype = "dashed") +
  coord_fixed() +
  
  theme(legend.title = element_blank()) +
  labs(x = "Cumulative Percentage of Observations",
       y = "Cumulative Percentage of Wages",
       title = "Lorenz curve by diploma")

产生: 堆叠因素

于 2020-11-14T17:38:57.957 回答