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

jks*_*612 1 r ggplot2

我对此感到困惑.我想以下列方式显示以下数据: 我认为我想做的图表

该图表应该传达不同数据集的历史界限,同时也让我突出显示最后三个观察期.如果有更好的图表,请分享.如果没有,那么我将如何使这个图表在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()
Run Code Online (Sandbox Code Playgroud)

这是输出:

来自代码示例的GGplot

如您所见,不应用渐变.另外值得注意的是,在控制台中运行代码会发出警告:Warning: Ignoring unknown aesthetics: height当它显然根据数据实现切片高度时.你知道如何平整这个圆圈并清理传说吗?

Pet*_*lis 6

我只关注如何做这个精确的图像,而不是关于是否有更好的可视化.

你做错的第一件事是你没有fill=为瓷砖映射任何东西.这就是为什么它是灰色的.

然后棘手的是你不能在一个矩形中逐渐"填充" ggplot2(我的理解是这是对底层grid系统的限制).因此,您需要制作一个非常人为的tileData对象版本,实际上可以绘制许多不同填充的矩形,以给出单个渐变填充矩形的印象.

这是我想出的:

在此输入图像描述

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()
Run Code Online (Sandbox Code Playgroud)