在geom_area中添加更多行

时间:2019-02-19 23:04:08

标签: r ggplot2

我正在计划每年使用不同年份的汽油和电动汽车的数量。每种颜色代表年份。当我使用实际数据集时,EV和汽油之间的线(图中的虚线)变得不可检测。我如何添加一条将所有年份所有年份的EV与汽油车分开的生产线?

ggplot(df, aes(x = y, y = number/1000, color=Type, fill=as.factor(desc(v)))) + 
  geom_area(stat="identity", linetype = 2, size =0.1 ) +
  geom_text(data=df, aes(x=2029, y=20000, label="EV"),size=3, color="black") +
  geom_text(data=df, aes(x=2027, y=5000, label="Gasoline"),size=5, color="black") +
  guides(fill=guide_legend(title="Vintages")) +
  guides(colour=FALSE) 

enter image description here

这是数据集的一部分:

df<- structure(list(v = c(2026, 2026, 2026, 2026, 2026, 2027, 2027, 
2027, 2027, 2027, 2028, 2028, 2028, 2028, 2028, 2029, 2029, 2029, 
2029, 2029, 2030, 2030, 2030, 2030, 2030, 2026, 2026, 2026, 2026, 
2026, 2027, 2027, 2027, 2027, 2027, 2028, 2028, 2028, 2028, 2028, 
2029, 2029, 2029, 2029, 2029, 2030, 2030, 2030, 2030, 2030), 
    y = c(2026L, 2027L, 2028L, 2029L, 2030L, 2026L, 2027L, 2028L, 
    2029L, 2030L, 2026L, 2027L, 2028L, 2029L, 2030L, 2026L, 2027L, 
    2028L, 2029L, 2030L, 2026L, 2027L, 2028L, 2029L, 2030L, 2026L, 
    2027L, 2028L, 2029L, 2030L, 2026L, 2027L, 2028L, 2029L, 2030L, 
    2026L, 2027L, 2028L, 2029L, 2030L, 2026L, 2027L, 2028L, 2029L, 
    2030L, 2026L, 2027L, 2028L, 2029L, 2030L), Type = c("EV", 
    "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", 
    "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", 
    "EV", "EV", "EV", "EV", "gasoline", "gasoline", "gasoline", 
    "gasoline", "gasoline", "gasoline", "gasoline", "gasoline", 
    "gasoline", "gasoline", "gasoline", "gasoline", "gasoline", 
    "gasoline", "gasoline", "gasoline", "gasoline", "gasoline", 
    "gasoline", "gasoline", "gasoline", "gasoline", "gasoline", 
    "gasoline", "gasoline"), number = c(213189.865946421, 213189.71590565, 
    213188.440559102, 213181.213595325, 213150.498999277, 0, 
    332143.919406143, 332143.685646769, 332141.698692092, 332130.439282254, 
    0, 0, 501331.161638226, 501330.808806663, 501327.809738373, 
    0, 0, 0, 724999.720261116, 724999.210013989, 0, 0, 0, 0, 
    994452.702960333, 2158367.51055841, 2158365.9915222, 2158353.07971449, 
    2158279.91280412, 2157968.95343502, 0, 2077292.93414104, 
    2077291.47216427, 2077279.0453617, 2077208.62681379, 0, 0, 
    1945136.30020633, 1945134.93124, 1945123.29502616, 0, 0, 
    0, 1756906.26442435, 1756905.02793232, 0, 0, 0, 0, 1521184.38412009
    )), row.names = c(NA, -50L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "y", drop = TRUE, indices = list(
    c(0L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L), c(1L, 
    6L, 11L, 16L, 21L, 26L, 31L, 36L, 41L, 46L), c(2L, 7L, 12L, 
    17L, 22L, 27L, 32L, 37L, 42L, 47L), c(3L, 8L, 13L, 18L, 23L, 
    28L, 33L, 38L, 43L, 48L), c(4L, 9L, 14L, 19L, 24L, 29L, 34L, 
    39L, 44L, 49L)), group_sizes = c(10L, 10L, 10L, 10L, 10L), biggest_group_size = 10L, labels = structure(list(
    y = 2026:2030), row.names = c(NA, -5L), class = "data.frame", vars = "y", drop = TRUE))

1 个答案:

答案 0 :(得分:1)

我通过计算汽油车的年度总数来添加一条线。 我还将在下面切换到annotate,以便仅写入一层文本。原来,它是在数据集中所有50行中的每一行的指定坐标处打印一次文本。

library(tidyverse)

ggplot(df, aes(x = y, y = number/1000, color=Type, 
               fill=as.character(v) %>% fct_rev())) + 
geom_area(stat="identity", linetype = 2, size =0.1 ) +
geom_line(data = df %>% ungroup() %>% 
            filter(Type == "gasoline") %>% 
            count(y, wt = number/1000),  
          aes(x = y, y = n), inherit.aes = F,  
          color = "black", size = 0.5, lty = "dashed") +
annotate("text", x=2029, y=10000, label="EV" ,size=5, color="black") +
annotate("text", x=2028.5, y=5000, label="Gasoline",size=5, color="black") +
guides(fill=guide_legend(title="Vintages")) +
guides(colour=FALSE) 

enter image description here