【问题标题】:Adding multiple shadows/rectangles to ggplot2 graph向ggplot2图形添加多个阴影/矩形
【发布时间】:2014-11-04 17:51:03
【问题描述】:

我正在尝试在 ggplot2 图形上添加多个阴影/矩形。在这个可重现的示例中,我只添加了 3 个,但使用完整数据可能需要添加多达 100 个。

这是我的原始数据的一个子集 - 在一个名为 temp 的数据框中 - dput 位于问题的底部:

     Season tier  group value
NA       NA   NA   <NA>    NA
99     1948    2   Wins    20
101    1948    2 Losses    17
NA.1     NA   NA   <NA>    NA
NA.2     NA   NA   <NA>    NA
104    1951    2   Wins    21
106    1951    2 Losses    18
107    1952    2   Wins    23
109    1952    2 Losses    18
110    1953    2   Wins    25
112    1953    2 Losses    18
113    1954    2   Wins    26
115    1954    2 Losses    19
116    1955    2   Wins    26
118    1955    2 Losses    19
119    1956    2   Wins    26
121    1956    2 Losses    20
NA.3     NA   NA   <NA>    NA
123    1958    1   Wins    27
125    1958    1 Losses    20
126    1959    1   Wins    27
128    1959    1 Losses    21
129    1960    1   Wins    28
131    1960    1 Losses    21
132    1961    1   Wins    30
134    1961    1 Losses    21
135    1962    1   Wins    30
137    1962    1 Losses    23
138    1963    1   Wins    31
140    1963    1 Losses    23
141    1964    1   Wins    32
143    1964    1 Losses    23
144    1965    1   Wins    34
146    1965    1 Losses    23
NA.4     NA   NA   <NA>    NA

我可以像这样制作一个ggplot:

p <- ggplot(temp, aes(Season,value, color=group)) + geom_point(size=4, shape=19) +
  scale_color_manual(values=c("red", "gray55"))
p

现在,我想添加阴影。每个阴影都将从季节列中开始一系列日期的季节开始,并以季节列中一系列日期中的最后一个日期/季节结束。最后,每个阴影都应该由 'tier' 变量着色。绿色代表“tier==2”,蓝色代表“tier==1”。

我使用rle 函数和一些额外的脚本来提取Season 变量中的NA 运行结束后的第一个观测值,以及在NA 运行中的第一个NA 之前获取观测值。然后我分别加和减 0.5,这给了我另一个像这样的数据帧(tempindex):

#    xmin   xmax ymin ymax
#5 1947.5 1948.5 -Inf  Inf
#6 1950.5 1956.5 -Inf  Inf
#7 1957.5 1965.5 -Inf  Inf

我可以像这样手动添加阴影:

t2.rect1 <- data.frame (xmin=1947.5, xmax=1948.5, ymin=-Inf, ymax=Inf)
t2.rect2 <- data.frame (xmin=1950.5, xmax=1956.5, ymin=-Inf, ymax=Inf)
t1.rect1 <- data.frame (xmin=1957.5, xmax=1965.5, ymin=-Inf, ymax=Inf)

p + 
  geom_rect(data=t2.rect1, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill="green", alpha=0.1, inherit.aes = FALSE) +
  geom_rect(data=t2.rect2, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill="green", alpha=0.1, inherit.aes = FALSE) +
  geom_rect(data=t1.rect1, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill="blue", alpha=0.1, inherit.aes = FALSE)

给出所需的输出:

但是,当我有很多阴影/矩形要应用时,我显然不想手动写出一百次。我正在寻找是否可以以自动化方式执行此操作。我尝试了一个for 循环(没有尝试按层调整颜色)非常不成功......

grect <-vector("list", nrow(indextemp)) #vector for storing geom_rects

 for (i in 1:nrow(indextemp)){
 grect[[i]] <- geom_rect(data=temp[i], aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), alpha=0.1, inherit.aes = FALSE)
 }

这显然不是首发。不知道有没有人有什么想法?

dput 用于临时数据帧:

structure(list(Season = c(NA, 1948L, 1948L, NA, NA, 1951L, 1951L, 
1952L, 1952L, 1953L, 1953L, 1954L, 1954L, 1955L, 1955L, 1956L, 
1956L, NA, 1958L, 1958L, 1959L, 1959L, 1960L, 1960L, 1961L, 1961L, 
1962L, 1962L, 1963L, 1963L, 1964L, 1964L, 1965L, 1965L, NA), 
    tier = c(NA, 2L, 2L, NA, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, NA), group = structure(c(NA, 
    1L, 3L, NA, NA, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 
    3L, NA, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 
    3L, 1L, 3L, NA), .Label = c("Wins", "Draws", "Losses"), class = "factor"), 
    value = c(NA, 20L, 17L, NA, NA, 21L, 18L, 23L, 18L, 25L, 
    18L, 26L, 19L, 26L, 19L, 26L, 20L, NA, 27L, 20L, 27L, 21L, 
    28L, 21L, 30L, 21L, 30L, 23L, 31L, 23L, 32L, 23L, 34L, 23L, 
    NA)), .Names = c("Season", "tier", "group", "value"), row.names = c("NA", 
"99", "101", "NA.1", "NA.2", "104", "106", "107", "109", "110", 
"112", "113", "115", "116", "118", "119", "121", "NA.3", "123", 
"125", "126", "128", "129", "131", "132", "134", "135", "137", 
"138", "140", "141", "143", "144", "146", "NA.4"), class = "data.frame")

dput 用于 tempindex 数据帧:

structure(list(xmin = c(1947.5, 1950.5, 1957.5), xmax = c(1948.5, 
1956.5, 1965.5), ymin = c(-Inf, -Inf, -Inf), ymax = c(Inf, Inf, 
Inf)), .Names = c("xmin", "xmax", "ymin", "ymax"), row.names = 5:7, class = "data.frame")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    最好只使用一层,有合适的映射,

    tempindex <- transform(tempindex, 
                           id = 1:3,
                           tier = c(1,1,2))
    
    
    ggplot(temp, aes(Season,value, color=group)) + 
      geom_rect(data=tempindex, inherit.aes=FALSE,
                aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,
                    group=id, fill = factor(tier)), alpha=0.2)+
      geom_point(size=4, shape=19) +
      scale_color_manual(values=c("red", "gray55"))+
      scale_fill_manual(values=c("green", "blue")) +
      guides(fill="none")
    

    【讨论】:

      【解决方案2】:

      另一种选择(更直接地避免创建辅助data.frames)可能是那个:

      ggplot(tmp, aes(Season,value, color = group)) + geom_point(size = 4, shape = 19) +
        scale_color_manual(values = c("red", "gray55")) + 
        annotate("rect", xmin = c(1947.5, 1950.5, 1957.5), xmax = c(1948.5, 1956.5, 1965.5), 
                 ymin = -Inf, ymax = Inf, alpha = .1, fill = c("green", "green", "blue"))
        
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-12
        • 2013-08-26
        相关资源
        最近更新 更多