ggridges包提供了geom_density_ridges_gradient()函数,用于画核密度估计峰峦图
这里我们用到的是ggridges内了数据lincoln_weather,该数据是关于每个月各种天气指标,包括温度湿度等等,其中我们要用到的两列为平均温度mt和月份mon,这是我简化后的数据,便于展示
与单数据系列不同的是这里要提供两个变量,x轴对应温度,即统计变量,y轴为分类变量
library(ggplot2)
library(ggridges)
library(RColorBrewer)
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = ..density..)) + # ..density.. 指按照计算出来的density填充颜色
#rel_min_height:relative to heightest point,指定去掉尾部的范围,一般0.01会比较好
# scale ;The extent to which the different densities overlap can be controlled with the parameter.该参数控制的是密度图之间重叠的程度,值越小越分开,越大月重叠
geom_density_ridges_gradient(scale =3, rel_min_height = 0.00,size = 0.3) +
scale_fill_gradientn(colours = colorRampPalette(rev(brewer.pal(11,'Spectral')))(32))
library(ggplot2)
library(ggridges)
library(RColorBrewer)
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = stat(x)) + # 指定按照x轴大小填充
geom_density_ridges_gradient(scale =3, rel_min_height = 0.00,size = 0.3) +
scale_fill_gradientn(colours = colorRampPalette(rev(brewer.pal(11,'Spectral')))(32))
library(ggplot2)
library(ggridges)
library(RColorBrewer)
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = factor(stat(quantile)))) +
stat_density_ridges(
geom = "density_ridges_gradient",
calc_ecdf = TRUE,
quantiles = c(0.025, 0.975)
) +
scale_fill_manual(
name = "Probability", values = c("#FF0000A0", "#A0A0A0A0", "#0000FFA0"),
labels = c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]")
)
library(ggplot2)
library(ggridges)
library(RColorBrewer)
panel_scaling = T
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = 0.5-abs(0.5-stat(ecdf)))) +
# fill = 0.5-abs(0.5-stat(ecdf)))图形在累积概率达到50%颜色最深,并且两边颜色对称
stat_density_ridges(
geom = "density_ridges_gradient",
calc_ecdf = TRUE
) +
scale_fill_viridis_c(
name = "Probability", direction=-1)
library(ggplot2)
library(ggridges)
library(RColorBrewer)
panel_scaling = T
ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = 0.5-abs(0.5-stat(ecdf)))) +
stat_density_ridges(
geom = "density_ridges_gradient",
calc_ecdf = TRUE,
jittered_points = T,
point_size=0.5,
point_shape=19
) +
scale_fill_viridis_c(
name = "Probability", direction=-1)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。