代码之家  ›  专栏  ›  技术社区  ›  red-swan

如何构建GeaveMult/ReT框,其中渐变填充和点/线在X轴上?

  •  1
  • red-swan  · 技术社区  · 6 年前

    我对此相当困惑。我想用以下方式显示以下数据: The chart I think I want to make

    这个图表应该传达不同数据集的历史界限,同时也让我强调最后三个观察阶段。如果有更好的图表,请分享。如果没有,那么我如何使这个图表在ggplot2中工作?

    我试过用 geom_rect 无法使其适用于因子数据,所以我一直把希望寄托在 geom_tile ,看起来很有希望。但我总是会有一些隐晦的错误。让我演示一下:

    # set the seed so we all have the same data
    set.seed(20180702)
    
    # the data for the tiles of the plot
    tileData <-
        data.frame(
            Factor = as.factor( c("factor1", "factor2", "factor3") ),
            Heights = c(2, 5, 3)
        )
    
    # sample data we'll want to chart
    exampleFrame <-
        data.frame(
            Period = as.factor(rep(c("first", "second", "third"), n = 3)),
            Factor = as.factor(rep(c("factor1", "factor2", "factor3"), each = 3)),
            Data = unlist(lapply(tileData[["Heights"]],
                                 function(height) rnorm(3, 0, height)))
        )
    
    # create the plot object with our sample data
    ggplot(exampleFrame, aes(x = Factor, y = Data, col = Period)) +
        # add the points for each data point
        geom_point() +
        # now, attempt to add the tiles with a gradient color
        geom_tile(data = tileData,
                  mapping = aes(x = Factor, y = 0, height = Heights*2,
                  col = NULL, alpha = 0.5)) +
        # this does nothing (??)
        scale_fill_gradient2()
    

    这里是输出:

    GGplot from code example

    如您所见,渐变不应用。同样值得注意的是,在控制台中运行代码会发出警告: Warning: Ignoring unknown aesthetics: height 当它明确地实现了基于数据的平铺高度时。你知道如何使这个圆成正方形,同时也清理这个传说吗?

    1 回复  |  直到 6 年前
        1
  •  6
  •   Peter Ellis    6 年前

    我只关注如何制作这张精确的图像,而不是是否有更好的可视化效果。

    你做错的第一件事是你没有绘制地图 fill= 为了瓷砖什么都行。这就是为什么它是灰色的。

    那么棘手的是,你不可能在 ggplot2 (我的理解是这是潜在的 grid 系统)。所以你需要对你的 tileData 对象,该对象实际上允许您绘制多个不同填充的矩形,以给人以单个渐变填充矩形的印象。

    我想到的是:

    enter image description here

    library(ggplot2)
    
    # set the seed so we all have the same data
    set.seed(20180702)
    
    # the data for the tiles of the plot
    tileData <-
      data.frame(
        Factor = as.factor( rep(c("factor1", "factor2", "factor3") , each = 100)),
        Height = c(seq(from = -2, to = 2, length.out = 100),
                    seq(from = -5, to = 5, length.out = 100),
                    seq(from = -3, to = 3, length.out = 100)),
        Gradation = abs(seq(from = -1, to =1 , length.out = 100)))
    )
    
    
    
    # sample data we'll want to chart
    exampleFrame <-
      data.frame(
        Period = as.factor(rep(c("first", "second", "third"), n = 3)),
        Factor = as.factor(rep(c("factor1", "factor2", "factor3"), each = 3)),
        Data = unlist(lapply(c(2, 5, 3),
                             function(height) rnorm(3, 0, height)))
      )
    
    # define the half-width of the rectangles
    r <- 0.4
    
    ggplot() +
      # add the background first or it over-writes the lines
      geom_rect(data = tileData,
                mapping = aes(xmin = as.numeric(Factor) - r, 
                              xmax = as.numeric(Factor) + r,
                              ymin = Height - 0.1, 
                              ymax = Height + 0.1,
                              fill = Gradation)) +
      # add the lines for each data point
      geom_segment(data = exampleFrame, 
                   aes(x = as.numeric(Factor) - r * 1.1,
                       xend = as.numeric(Factor) + r * 1.1,
                       y = Data, yend = Data,
                       col = Period),
                   size = 3) +
      scale_fill_gradient2("Historic range\nof data", low = "white", high = "lightblue") +
      scale_colour_manual(values = c("first" = "hotpink", "second" = "darkgreen", "third" = "darkblue")) +
      scale_x_continuous("", breaks = unique(as.numeric(exampleFrame$Factor)), labels = levels(exampleFrame$Factor)) +
      theme_minimal()