我有来自两个不同数据集的数据。第一个,dat1
包含多个点。第二个dat2
仅包含dat1
中每个Season-Species组的最大值。我试图绘制dat1
,然后想要绘制更大的形状,突出显示每个Season-Species组的最大值,即dat2
。
数据:
library(ggplot2)
library(dplyr)
dat1 <- structure(list(Season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Summer", "Winter"), class = "factor"),
Species = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L), .Label = c("BHS", "MTG"), class = "factor"),
CovGrain = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L), .Label = c("CanCov_30", "CanCov_500", "CanCov_1000",
"NDVI_30", "NDVI_500", "NDVI_1000", "Slope_30", "Slope_500",
"Slope_1000", "SlopeVar_30", "SlopeVar_500", "SlopeVar_1000"
), class = "factor"), Count = c(4L, 19L, 4L, 5L, 3L, 14L,
14L, 9L, 9L, 4L, 10L, 9L)), .Names = c("Season", "Species",
"CovGrain", "Count"), class = "data.frame", row.names = c(1L,
2L, 3L, 14L, 15L, 16L, 30L, 31L, 32L, 45L, 46L, 47L))
dat2 <- dat1 %>% group_by(Season, Species) %>%
filter(Count == max(Count)) %>% as.data.frame()
ggplot(dat1, aes(x = CovGrain, y = Count)) +
geom_point(aes(fill = Species, color = Species),
alpha = 0.5, stroke = 3, size = 3, position=position_dodge(0.5)) +
facet_wrap(~Season, scales = "free_x") +
scale_shape_manual(values = c(21,22,24)) +
scale_fill_manual(values=c("blue", "red")) +
geom_point(data = dat2, aes(x = CovGrain, y = Count), shape = 23,
stroke = 2, size = 6, position=position_dodge(0.5)) +
theme_bw()
在下图中,我想要正确地避开黑色三角形,以便勾勒出每组的最大点。
任何建议都表示赞赏!
答案 0 :(得分:1)
这有点hacky,并且可能有更好的解决方案,但一种方法是使用您自己的随机性创建一个新的x变量。 hacky部分来自于首先执行geom_point(size = -1)
以使其保持x轴。所以,不管怎么说,不是优雅的,但是我想你会得到你想要的东西。
dat1$id <- 1:nrow(dat1)
dat2 <- dat1 %>%
group_by(Season, Species, Cov) %>%
filter(Count == max(Count)) %>%
as.data.frame()
randomness <- rnorm(nrow(dat1), 0, 0.5)
dat1$new_x <- as.integer(dat1$CovGrain) + randomness
dat2$new_x <- as.integer(dat2$CovGrain) + randomness[dat2$id]
ggplot(dat1, aes(x = CovGrain, y = Count)) +
geom_point(size = -1) +
geom_point(aes(x = new_x, fill = Species, color = Species),
alpha = 0.5, stroke = 3, size = 3) +
facet_wrap(~Season, scales = "free_x") +
scale_shape_manual(values = c(21,22,24)) +
scale_fill_manual(values=c("blue", "red")) +
geom_point(data = dat2, aes(x = new_x, y = Count), shape = 23,
stroke = 2, size = 6) +
theme_bw()
答案 1 :(得分:1)
在原始数据中创建一个布尔变量,指示'Count'是否最大,按'Season'和'Species'分组。使用scale_alpha_manual
将alpha
设置为0
FALSE
(即“计数”不在max
)。使用group = Species
以“物种”躲闪。
dat1 <- dat1 %>% group_by(Season, Species) %>%
mutate(max_count = Count == max(Count))
pos <- position_dodge(0.5)
ggplot(dat1, aes(x = CovGrain, y = Count)) +
geom_point(aes(color = Species), position = pos) +
geom_point(aes(alpha = max_count, group = Species), shape = 23, size = 6, position = pos) +
facet_wrap(~ Season) +
scale_alpha_manual(values = c(0, 1), guide = "none") +
theme_bw()