本文介绍了将多个阴影/矩形添加到ggplot2图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图在ggplot2图上添加多个阴影/矩形。在这个可重复的例子中,我只添加了3个,但是我可能需要使用完整的数据添加到100个。 这是我原始数据的一个子集 - 在一个名为 temp 的数据框中 - dput 位于问题的底部: 季节层组值 NA NA NA NA< NA> NA 99 1948 2赢得20 101 1948 2损失17 NA.1 NA NA NA< NA> NA NA.2 NA NA NA< NA> NA 104 1951 2赢得21 1951 2损失18 107 1952 2赢得23 109 1952 2损失18 $ b $ 110 110 1953 2赢得25 112 1953 2损失18 113 1954 2赢得26 115 1954 2损失19 116 1955 2赢得26 118 1955 2损失19 119 1956 2赢得26 121 1956 2损失20 NA.3不适用NA< NA> NA 123 1958 1赢得27 125 1958 1损失20 126 1959 1赢得27 128 1959 1损失21 129 1960 1赢得28 131 1960 1损失21 132 1961 1赢得30 $ b $ 134 13461 1损失21 135 1962 1赢得30 137 1962 1损失23 138 1963 1赢得31 140 1963 1损失23 141 1964 1赢得32 143 1964 1损失23 144 1965 1赢得34 146 1965 1亏损23 不适用.4 NA NA NA< NA> NA 我可以制作像这样的ggplot: p scale_color_manual = c(red,gray55))p 现在,我想添加阴影。每个阴影将以Season开始,季节开始在季节列中进行日期运行,并以季节列中日期运行中的最后一个日期/季节结束。最后,每个阴影应该由'层'变量着色。绿色代表'tier == 2',蓝色代表'tier == 1'。 我已经使用了 rle 函数加上一些额外的脚本来提取Season变量中的NAs运行结束后的第一个观察结果,以及在运行NAs中的第一个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 t2.rect2 t1.rect1 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 stored geom_rects for(i in 1:nrow(indextemp)){ grect [[i]] } 这显然不是起步者。我怀疑是否有人有任何想法?对于临时数据框, dput : 结构(列表(季节= 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 1L,2L,2L,2L,2L,2L,2L, 2L,2L,2L,2L,2L,2L,2L,2L, 2L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L, , 1L,3L,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 ),值= 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,31 L,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, 112113115116118119121NA.3123125126 128,129,131,132,134,135,137,138,140,141,143,144 ,146,NA.4),class =data.frame) dput 用于tempindex数据框: structure(list(xmin = 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) 解决方案 使用适当的映射,最好只使用一个图层, 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) I am trying to add multiple shadows/rectangles over a ggplot2 graph. In this reproducible example, I am only adding 3, but I may need to add up to a hundred using the full data.Here is a subset of my raw data - in a dataframe called temp - the dput is at the bottom of the question: Season tier group valueNA NA NA <NA> NA99 1948 2 Wins 20101 1948 2 Losses 17NA.1 NA NA <NA> NANA.2 NA NA <NA> NA104 1951 2 Wins 21106 1951 2 Losses 18107 1952 2 Wins 23109 1952 2 Losses 18110 1953 2 Wins 25112 1953 2 Losses 18113 1954 2 Wins 26115 1954 2 Losses 19116 1955 2 Wins 26118 1955 2 Losses 19119 1956 2 Wins 26121 1956 2 Losses 20NA.3 NA NA <NA> NA123 1958 1 Wins 27125 1958 1 Losses 20126 1959 1 Wins 27128 1959 1 Losses 21129 1960 1 Wins 28131 1960 1 Losses 21132 1961 1 Wins 30134 1961 1 Losses 21135 1962 1 Wins 30137 1962 1 Losses 23138 1963 1 Wins 31140 1963 1 Losses 23141 1964 1 Wins 32143 1964 1 Losses 23144 1965 1 Wins 34146 1965 1 Losses 23NA.4 NA NA <NA> NAI can make a ggplot like this:p <- ggplot(temp, aes(Season,value, color=group)) + geom_point(size=4, shape=19) + scale_color_manual(values=c("red", "gray55"))pNow, I want to add shadows. Each shadow will begin with the Season that begins a run of dates in the Season column and will end with the date/Season that is the last in a run of dates in the Season column. Finally, each shadow should be colored by the 'tier' variable. Green for 'tier==2' and blue for 'tier==1'.I have used the rle function plus some extra script to extract the first observation after the end of a run of NAs in the Season variable, as well as to get the observation before the first NA in a run of NAs. I then just add and minus 0.5 respectively, which gives me another dataframe (tempindex) like this:# xmin xmax ymin ymax#5 1947.5 1948.5 -Inf Inf#6 1950.5 1956.5 -Inf Inf#7 1957.5 1965.5 -Inf InfI could add the shadows manually like this: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)which gives the desired output:However, I obviously don't want to be manually writing out this a hundred times for instances when I have lots of shadows/rectangles to apply. I am looking to see if I can do this in an automated fashion. I tried a for loop (without trying to adjust color by tier) very unsuccessfully...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) }This is obviously a non-starter. I wonder if anybody has any ideas?dput for temp dataframe: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 for tempindex dataframe: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") 解决方案 it's better to use only one layer, with suitable mapping,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") 这篇关于将多个阴影/矩形添加到ggplot2图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:27