代码之家  ›  专栏  ›  技术社区  ›  Konrad

在ggplot2中创建范围(道路)打印

  •  1
  • Konrad  · 技术社区  · 7 年前

    通过下面的例子 r-statistics 网站,我想修改它创建一个范围图

    例子

    # Notes -------------------------------------------------------------------
    
    # Stacked are chart
    # Source: http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html#Stacked%20Area%20Chart
    
    # Graphic -----------------------------------------------------------------
    
    library(ggplot2)
    library(lubridate)
    theme_set(theme_bw())
    
    df <- economics[, c("date", "psavert", "uempmed")]
    df <- df[lubridate::year(df$date) %in% c(1967:1981),]
    
    # labels and breaks for X axis text
    brks <- df$date[seq(1, length(df$date), 12)]
    lbls <- lubridate::year(brks)
    
    # plot
    ggplot(df, aes(x = date)) +
        geom_area(aes(y = psavert + uempmed, fill = "psavert")) +
        geom_area(aes(y = uempmed, fill = "uempmed")) +
        labs(
            title = "Area Chart of Returns Percentage",
            subtitle = "From Wide Data format",
            caption = "Source: Economics",
            y = "Returns %"
        ) +  # title and caption
        scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
        scale_fill_manual(name = "",
                          values = c("psavert" = "#00ba38", "uempmed" = "#f8766d")) +  # line color
        theme(panel.grid.minor = element_blank())  # turn off minor grid
    

    预期结果

    Example range plot

    尝试

    "white"

    设置一个系列的颜色为白色,是接近的,但我想使网格可见。以后可以很容易地控制图例。

    attempt 1

    1 回复  |  直到 7 年前
        1
  •  2
  •   liborm    7 年前

    你一定要用 geom_ribbon .

    文件采用:

    library(tidyverse)
    
    data.frame(year = 1875:1972, level = as.vector(LakeHuron)) %>%
      ggplot(aes(year)) +
      geom_ribbon(aes(ymin = level - 1, ymax = level + 1), fill = "grey70") +
      geom_line(aes(y = level))