第 3 章 箱型图

您所在的位置:网站首页 r语言做分组箱型图 第 3 章 箱型图

第 3 章 箱型图

2024-07-16 04:34| 来源: 网络整理| 查看: 265

第 3 章 箱型图 knitr::include_graphics("img/BoxPlotAnatomy.png")

图 3.1: BoxPlotAnatomy

3.1 基本箱型图 ggplot(mtcars, aes(x=as.factor(cyl), y=mpg)) + geom_boxplot(fill="slateblue", alpha=0.2) + xlab("cyl")

3.1.1 添加观测点 data3 % ggplot( aes(x=name, y=value, fill=name)) + geom_boxplot() + scale_fill_viridis(discrete = TRUE, alpha=0.6) + geom_jitter(color="black", size=0.4, alpha=0.9) + theme_ipsum() + theme( legend.position="none", plot.title = element_text(size=11) ) + ggtitle("A boxplot with jitter") + xlab("")

3.1.2 geom_boxplot()设置 # Dataset 1: one value per group data % mutate(name = fct_reorder(name, val)) %>% ggplot( aes(x=name, y=val)) + geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) + coord_flip() + xlab("") + theme_bw()-> p1 # Reverse side data %>% mutate(name = fct_reorder(name, desc(val))) %>% ggplot( aes(x=name, y=val)) + geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) + coord_flip() + xlab("") + theme_bw()-> p2 ggarrange(p1,p2,labels=c("p1","p2"))

# Using median mpg %>% mutate(class = fct_reorder(class, hwy, .fun='median')) %>% ggplot( aes(x=reorder(class, hwy), y=hwy, fill=class)) + geom_boxplot() + xlab("class") + theme(legend.position="none") + xlab("") -> p3 # Using number of observation per group mpg %>% mutate(class = fct_reorder(class, hwy, .fun='length' )) %>% ggplot( aes(x=class, y=hwy, fill=class)) + geom_boxplot() + xlab("class") + theme(legend.position="none") + xlab("") + xlab("") -> p4 ggarrange(p3,p4,labels=c("p3","p4"))

3.1.4 配色 (RcolorBrewer包) # Top Left: Set a unique color with fill, colour, and alpha ggplot(mpg, aes(x=class, y=hwy)) + geom_boxplot(color="red", fill="orange", alpha=0.2) -> p1 # Top Right: Set a different color for each group ggplot(mpg, aes(x=class, y=hwy, fill=class)) + geom_boxplot(alpha=0.3) + theme(legend.position="none")-> p2 # Bottom Left ggplot(mpg, aes(x=class, y=hwy, fill=class)) + geom_boxplot(alpha=0.3) + theme(legend.position="none") + scale_fill_brewer(palette="BuPu")-> p3 # Bottom Right ggplot(mpg, aes(x=class, y=hwy, fill=class)) + geom_boxplot(alpha=0.3) + theme(legend.position="none") + scale_fill_brewer(palette="Dark2") -> p4 ggarrange(p1,p2,p3,p4,labels=c("p1","p2","p3","p4"))

3.1.5 连续变量cut diamonds %>% # Add a new column called 'bin': cut the initial 'carat' in bins mutate( bin=cut_width(carat, width=0.5, boundary=0) ) %>% # plot ggplot( aes(x=bin, y=price) ) + geom_boxplot(fill="#69b3a2") + theme_ipsum() + xlab("Carat")

3.1.6 箱型图添加均值 names % # Add a column called 'type': do we want to highlight the group or not? mutate( type=ifelse(class=="subcompact","Highlighted","Normal")) %>% # Build the boxplot. In the 'fill' argument, give this column ggplot( aes(x=class, y=hwy, fill=type, alpha=type)) + geom_boxplot() + scale_fill_manual(values=c("#69b3a2", "grey")) + scale_alpha_manual(values=c(1,0.1)) + theme_ipsum() + theme(legend.position = "none") + xlab("")

3.2 分组箱型图 # dataset variety


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3