本文介绍了ggridges下的阴影区域如何曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想用红色突出显示由ggridges创建的多个地块的20度以上的所有温度 library(tidyverse)库(ggridges) ggplot(t2,aes(x = t,y = year))+ stat_density_ridges(geom =density_ridges_gradient,quantile_lines = TRUE,quantiles = 2 )+ theme_ridges() 输出(t2)输出 structure(list(Date = c(1853-01,1853-02,1853-03,1853-04,1853-05,1853-06,1853-07, 1853-08,1853-09,1853-10,1853-11,1853-12,1854-01,1854-02,1854-03 ,1854-04,1854-05,1854-06,1854-07,1854-08,1854-09,1854-10,$ b $ ,t = c(-5.6,-5.3,-1.5,4.9,9.8,17.9, 18.5,19.9,14.8,6.2,3.1,-4.3, -5.9 ,-7,-1.3,4.1,10,16.8, 22,20,16.1,10.1,1.8,-5.6),year = c(1853,1853,1853,18531853185318531853185318531853185318541854 1854,1854,1854,1854,1854,1854,1854,1854,1854,1854)),row.names = c (NA,-24L ),class = c(tbl_df,tbl,data.frame),.Names = c(Date,t,year )) 解决方案我们可以做到以下几点: gg stat_density_ridges( geom =density_ridges_gradient, quantile_lines = TRUE, quantiles = 2)+ theme_ridges() #构建ggplot并提取数据d< ; - ggplot_build(gg)$ data [[1]] #为阴影区域添加geom_ribbon gg + geom_ribbon( data = transform(subset(d ,x> = 20),year = group), aes(x,ymin = ymin,ymax = yma x,group = group), fill =red, alpha = 0.2); 这个想法是从 ggplot 构建中提取绘图数据;我们然后 subset x> = 20 的数据,然后添加一个 geom_ribbon 在所有密度范围内遮蔽> = 20 区域。 没有 transform(...,year = group)),会出现错误找不到对象'year';我不确定这是为什么,但添加 transform(...,year = group)可以。 I would like to highlight in red all temperatures above 20 degrees in multiple plots created by ggridgeslibrary(tidyverse)library(ggridges)ggplot(t2, aes(x = t, y = year)) + stat_density_ridges(geom = "density_ridges_gradient", quantile_lines = TRUE, quantiles = 2) + theme_ridges()Output of dput(t2)structure(list(Date = c("1853-01", "1853-02", "1853-03", "1853-04","1853-05", "1853-06", "1853-07", "1853-08", "1853-09", "1853-10","1853-11", "1853-12", "1854-01", "1854-02", "1854-03", "1854-04","1854-05", "1854-06", "1854-07", "1854-08", "1854-09", "1854-10","1854-11", "1854-12"), t = c(-5.6, -5.3, -1.5, 4.9, 9.8, 17.9,18.5, 19.9, 14.8, 6.2, 3.1, -4.3, -5.9, -7, -1.3, 4.1, 10, 16.8,22, 20, 16.1, 10.1, 1.8, -5.6), year = c("1853", "1853", "1853","1853", "1853", "1853", "1853", "1853", "1853", "1853", "1853","1853", "1854", "1854", "1854", "1854", "1854", "1854", "1854","1854", "1854", "1854", "1854", "1854")), row.names = c(NA, -24L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date","t", "year")) 解决方案 We can do the following:gg <- ggplot(t2, aes(x = t, y = year)) + stat_density_ridges( geom = "density_ridges_gradient", quantile_lines = TRUE, quantiles = 2) + theme_ridges()# Build ggplot and extract datad <- ggplot_build(gg)$data[[1]]# Add geom_ribbon for shaded areagg + geom_ribbon( data = transform(subset(d, x >= 20), year = group), aes(x, ymin = ymin, ymax = ymax, group = group), fill = "red", alpha = 0.2);The idea is to pull out the plot data from the ggplot build; we then subset the data for x >= 20, and add a geom_ribbon to shade the regions >=20 in all density ridges.Without transform(..., year = group)), there will be an error object 'year' not found; I'm not sure why this is, but adding transform(..., year = group) works. 这篇关于ggridges下的阴影区域如何曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 01:45