R 如何为因子x轴构建带有渐变填充和顶部点/线的几何/矩形框?

R 如何为因子x轴构建带有渐变填充和顶部点/线的几何/矩形框?,r,ggplot2,R,Ggplot2,我对此感到相当困惑。我希望以以下方式显示以下数据: 这个图表应该传达不同数据集的历史界限,同时也让我强调最后三个观察期。如果有更好的图表,请分享。如果没有,那么如何使此图表在ggplot2中工作 我尝试过使用geom\u rect,但无法将其用于因子数据,因此我一直希望使用geom\u tile,这看起来很有希望。但我总是会犯一些神秘的错误。让我示范一下: # set the seed so we all have the same data set.seed(20180702) # the

我对此感到相当困惑。我希望以以下方式显示以下数据:

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

我尝试过使用
geom\u rect
,但无法将其用于因子数据,因此我一直希望使用
geom\u 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()
#设置种子,以便我们都有相同的数据
种子集(20180702)
#绘图分幅的数据

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

你做错的第一件事是没有将
fill=
映射到磁贴的任何内容。这就是为什么它是灰色的

然后,棘手的事情是,您不能在
ggplot2
中对矩形进行分级“填充”(我的理解是,这是基础
网格
系统的一个限制)。因此,您需要为
tileData
对象制作一个精心设计的版本,让您可以绘制多个不同填充的矩形,以形成单个渐变填充矩形的效果

以下是我的想法:

库(ggplot2)
#设定种子,使我们都有相同的数据
种子集(20180702)
#绘图分幅的数据

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

你做错的第一件事是没有将
fill=
映射到磁贴的任何内容。这就是为什么它是灰色的

然后,棘手的事情是,您不能在
ggplot2
中对矩形进行分级“填充”(我的理解是,这是基础
网格
系统的一个限制)。因此,您需要为
tileData
对象制作一个精心设计的版本,让您可以绘制多个不同填充的矩形,以形成单个渐变填充矩形的效果

以下是我的想法:

库(ggplot2)
#设定种子,使我们都有相同的数据
种子集(20180702)
#绘图分幅的数据

tileData您还没有设置填充美学,我无法告诉您实际希望填充映射到什么,您能澄清吗?如果将
颜色
alpha
移动到
aes()
之外的
geom\u tile
中,图例将更干净,它们是固定的美学,因此您不希望它们出现在
aes()
中。您实际上没有设置填充美学,我无法告诉您实际希望填充映射到什么,您能澄清一下吗?如果将
颜色
alpha
移动到
aes()
geom_tile
的外部,图例会更干净,因为它们是固定的,所以您不希望它们出现在
aes()
中。
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()