如何使用因子x轴在顶部构建带有渐变填充和点/线的geom_tile / rect框?

时间:2018-07-03 00:31:02

标签: r ggplot2

我对此很困惑。我想通过以下方式显示以下数据: 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 个答案:

答案 0 :(得分: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()