簡體   English   中英

如何在ggplot2的區域圖中使投影區域透明?

[英]How to make projected areas transparent in area plot in ggplot2?

我有以下圖,我想讓 2020 年后的區域透明以說明預測。 有沒有一種簡單的方法可以做到這一點?

labourforce<-ggplot(potr, aes(x=Year, y=share, fill=Generation)) + 
  theme_bw()+
  geom_area(alpha=1.6 , size=1, colour="black") + 
  ggtitle("US Labor Force Participation, Aged 20 -65 ") +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

輸出

在此處輸入圖片說明

好吧,這並不像我希望的那么容易。 似乎您必須拆分數據集:(我還制作了模型數據):

library(tidyverse)
potr <- data.frame(
  Year = rep(
    seq(as.Date("2000-01-01"), as.Date("2039-01-01"), length.out = 40),
    4
  ),
  Generation = c(rep("a", 40), rep("b", 40), rep("c", 40), rep("d", 40)),
  share = c(
    rep(0.1, 40), seq(0.0, 0.2, length.out = 40),
    seq(0.4, 0.2, length.out = 40), rep(0.5, 40)
  ),
  stringsAsFactors = F
) %>% mutate(alpha2 = ifelse(Year <= as.Date("2020-01-01"), 1, 0.1))


potr1 <- potr %>% filter(Year <= as.Date("2020-01-02"))
potr2 <- potr %>% filter(Year >= as.Date("2020-01-01"))


ggplot(potr1) +
  theme_bw() +
  geom_area(aes(fill = Generation, y = share, x = Year), size = 1, colour = "black") +
  geom_area(aes(fill = Generation, y = share, x = Year),
    data = potr2, size = 1, colour = "black",
    alpha = 0.4
  ) +
  ggtitle("US Labor Force Participation, Aged 20 -65 ") +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

在此處輸入圖片說明

暫無
暫無

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

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