我想使用ggplot
和stat_contour
为的两类数据显示等高线图facet_grid
。我想根据数据突出显示特定级别。这是一个使用常规volcano
数据的类似虚拟示例。
library(dplyr)
library(ggplot2)
v.plot <- volcano %>% reshape2::melt(.) %>%
mutate(dummy = Var1 > median(Var1)) %>%
ggplot(aes(Var1, Var2, z = value)) +
stat_contour(breaks = seq(90, 200, 12)) +
facet_grid(~dummy)
假设在每个因子水平(我想是东半和西半)内,我想找到火山的平均高度并将其显示出来。我可以手动计算:
volcano %>% reshape2::melt(.) %>%
mutate(dummy = Var1 > median(Var1)) %>%
group_by(dummy) %>%
summarise(h.bar = mean(value))
# A tibble: 2 × 2
dummy h.bar
<lgl> <dbl>
1 FALSE 140.7582
2 TRUE 119.3717
这告诉我,每半部分的平均高度分别为141和119。我可以在两个面上都画出这两个高度,而不仅仅是在每侧上画出适当的高度。
v.plot + stat_contour(breaks = c(141, 119), colour = "red", size = 2)
And you can't put breaks=
inside an aes()
statement, so passing it in as a column in the original dataframe is out. I realize with this dummy example I could probably just do something like bins=2
but in my actual data I don't want the mean of the data, I want something else altogether.
Thanks!
I made another attempt at this problem and came up with a partial solution, but I'm forced to use a different geom
.
volcano %>% reshape2::melt(.) %>%
mutate(dummy = Var1 > median(Var1)) %>%
group_by(dummy) %>%
mutate(h.bar = mean(value), # edit1
is.close = round(h.bar) == value) %>% #
ggplot(aes(Var1, Var2, z = value)) +
stat_contour(breaks = seq(90, 200, 12)) +
geom_point(colour = "red", size = 3, # edit 2
aes(alpha = is.close)) + #
scale_alpha_discrete(range = c(0,1)) + #
facet_grid(~dummy)
In edit 1
I added a mutate()
to the above block to generate a variable identifying where value
was "close enough" (rounded to the nearest integer) to the desired highlight point (the mean of the data for this example).
在中,edit2
我添加了geom_point
s以显示具有所需值的网格位置,并使用alpha
0或完全透明的隐藏了不需要的网格位置。
这种解决方案的问题在于,它非常松散,试图将它们与之桥接geom_path
是一团混乱。我也尝试了更粗略的舍入,这只会使事情变得混乱。
很想听听其他想法!谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句