ggplot2 그래프 위에 여러 그림자/사각형을 추가하려고합니다. 이 재현 가능한 예제에서는 3을 더하고 있지만 전체 데이터를 사용하여 최대 100을 더할 수 있습니다. 여기 ggplot2 그래프에 여러 그림자/사각형 추가하기
내 원시 데이터의 일부는 - dataframe에temp
사람을 불렀습니다 -
dput
이 질문의 하단에 :
p <- ggplot(temp, aes(Season,value, color=group)) + geom_point(size=4, shape=19) +
scale_color_manual(values=c("red", "gray55"))
p
:이 같은 ggplot을 할 수
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
이제 그림자를 추가하고 싶습니다. 각 그림자는 시즌 열에서 날짜 실행을 시작하는 시즌으로 시작하여 시즌 열의 날짜 중 마지막 날짜/계절으로 끝납니다. 마지막으로, 각 그림자는 '계층'변수로 채색되어야합니다. 녹색은 'tier == 2'이고 파랑은 'tier == 1'입니다.
나는 시즌 변수에서 NAs가 끝난 후 첫 번째 관찰을 추출하고 NAs가 실행될 때 첫 번째 관찰 전에 관찰을 얻으려는 목적으로 rle
함수와 몇 가지 추가 스크립트를 사용했습니다.
# 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
I는 다음과 같이 수동으로 그림자를 추가 할 수 있습니다 :
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)
원하는 출력을 제공합니다 : 나는 다음 날이 같은 다른 dataframe (tempindex)를 제공하는 각각 추가하고 마이너스 0.5
을그러나, 나는 분명히 내가 그림자가 많이있을 때 인스턴스에 대한이 백 번을 작성 수동 싶지 않아/R 적용 할 ectangles. 나는 이것을 자동화 된 방식으로 할 수 있는지보고 싶다. 나는
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)
}
이 분명 비 스타터입니다 ... 아주 실패 (계층으로 색상을 조정하려고하지 않음) for
루프를 시도했다. 아무도 아이디어가 있다면 궁금해하니? 임시 dataframe에 대한
dput
: tempindex 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
: 적합 매핑, 하나 개의 레이어를 사용하는 것이 좋습니다
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")