当前位置: 首页> 健康> 科研 > 四川成都最新新闻事件今天_深圳网站开发哪个公司好_中国十大互联网公司排名_现在怎么做网络推广

四川成都最新新闻事件今天_深圳网站开发哪个公司好_中国十大互联网公司排名_现在怎么做网络推广

时间:2025/7/12 9:00:06来源:https://blog.csdn.net/wangjunliang/article/details/144294758 浏览次数:0次
四川成都最新新闻事件今天_深圳网站开发哪个公司好_中国十大互联网公司排名_现在怎么做网络推广

目的:为展示不同数据分布的差异。

1. ggplot2 实现

# 准备数据
dat=mtcars[, c("mpg", "cyl")]
colnames(dat)=c("value", "type")
head(dat)
#                  value type
#Mazda RX4         21.0   6
#Mazda RX4 Wag     21.0   6
#Datsun 710        22.8   4
cols=c("#F71480", "#76069A", "#FF8000")
#
p1=ggplot(dat, aes(x = value, fill = as.factor(type) ) ) +geom_density(alpha = 0.8) +scale_fill_manual(values = cols)+facet_wrap(~type, ncol=1) +  # 按气缸数分面labs(title = "Density of MPG by Cylinder Count-A",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14)+theme(strip.background = element_blank(),  # 去掉小标题背景strip.placement = "outside");p1  # 小标题外部显示
#
p2=ggplot(dat, aes(x = value, fill = as.factor(type) ) ) +geom_density(alpha = 0.8) +scale_fill_manual(values = cols)+facet_wrap(~type, ncol=1, scales="free_y") +  # 按气缸数分面labs(title = "Density of MPG by Cylinder Count-B",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14)+theme(strip.background = element_blank(),  # 去掉小标题背景strip.placement = "outside"); p2  # 小标题外部显示
#

在这里插入图片描述

2. 使用R包 ggridges

图放这里,方便和上图类似。

在这里插入图片描述

library(ggridges)
pB=ggplot(dat, aes(x = value, y = type, fill = factor(type, levels = c("4", "6", "8")) )) + ggridges::geom_density_ridges(alpha = 0.7, show.legend = T) +scale_fill_manual(values = cols)+#scale_y_continuous( expand = c(0,0) )+labs(title = "Density of MPG by Cylinder Count-C",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14); pB
#
pB2=ggplot(dat, aes(x = value, y = type, fill = factor(type, levels = c("4", "6", "8")) )) + ggridges::geom_density_ridges(alpha = 0.7, show.legend = T, stat="binline", bins=25) +scale_fill_manual(values = cols)+#scale_y_continuous( expand = c(0,0) )+labs(title = "Density of MPG by Cylinder Count-D",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14); pB2
#

3. 去掉底部的空隙

pB3=ggplot(dat, aes(x = value, y = type, fill = factor(type, levels = c("4", "6", "8")) )) + ggridges::geom_density_ridges(alpha = 0.7, show.legend = T, scale = 2) +scale_fill_manual(values = cols)+#scale_y_continuous( expand = c(0,0) )+labs(title = "Density of MPG by Cylinder Count-E\nset scale=2",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +# 去掉底部scale_y_discrete(expand = c(0, 0)) +     # will generally have to set the `expand` optionscale_x_continuous(expand = c(0, 0)) +   # for both axes to remove unneeded paddingcoord_cartesian(clip = "on") + # to avoid clipping of the very top of the top ridgelinetheme_classic(base_size = 14); pB3

在这里插入图片描述

Ref

  • https://zhuanlan.zhihu.com/p/560080959
  • https://wilkelab.org/ggridges/
  • https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html
关键字:四川成都最新新闻事件今天_深圳网站开发哪个公司好_中国十大互联网公司排名_现在怎么做网络推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: