ggplot2绘制箱线图

加载数据集

本文转载自Creating plots in R using ggplot2

> rm(list=ls())  ## 清空环境
> library(datasets)
> library(ggplot2)
> head(airquality)
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
> airquality$Month <- factor(airquality$Month,labels = c("May", "Jun", "Jul", "Aug", "Sep"))

最终效果图:
在这里插入图片描述

代码

一步步运行如下代码即可了解 box 图的做法:

## 最基础的箱线图
> p0 <- ggplot(airquality,aes(x=Month,y=Ozone)) +
				geom_boxplot( )
> p0

## 定制坐标轴
> p1 <- p0 + scale_x_discrete(name="Month") +
					scale_y_continuous(name="Mean ozone in parts per billion")
> p1

## 坐标轴的label是可以换行的
> p2 <- p0 +scale_y_continuous(name="Mean ozone in\nparts per billon")
> p2

## 改变坐标轴的tick
> p3 <- p0 +scale_y_continuons(name="Mean ozone in\nparts per billion",
													breaks=seq(0,175,25),
													limits=c(0,175))
> p3

## 加标题
> p4 <- p3 +ggtitle("Boxplot of mean ozone by month")
> p4

## 填充颜色和边框颜色,以及透明度、离群点颜色形状(更多色彩见附录)
> fill <- "gold1"
> line <- "goldenrod2"
> p5 <- ggplot(airquality,aes(x=Month,y=Ozone)) +
				geom_boxplot(fill=fill,
							 colour=line,
							 alpha=0.7,
							 outlier.colour="#1F3552",
							 outlier.shape=20) +
				scale_y_continuous(name = "Mean ozone in\nparts per billion",
		                           breaks = seq(0, 175, 25),
		                           limits=c(0, 175)) +
				scale_x_discrete(name = "Month") +
        	 	ggtitle("Boxplot of mean ozone by month")
> p5

## 使用主题
> p6 <- p5 +theme_tw()
> p6

## 同时显示散点
> p7 <- p6 +geom_jitter()
> p7

## 显示缺口
> p8 <- ggplot(airquality, aes(x = Month, y = Ozone)) +
       		geom_boxplot(colour = lines, fill = fill,
                     	 size = 1, notch = TRUE)
> p8

## 按照Temp进行分面
> airquality_trimmed <- airquality[which(airquality$Month == "Jul" |
                                       airquality$Month == "Aug" |
                                       airquality$Month == "Sep"), ]
> airquality_trimmed$Temp.f <- factor(ifelse(airquality_trimmed$Temp > mean(airquality_trimmed$Temp), 1, 0),
                                    labels = c("Low temp", "High temp"))

> p9 <- ggplot(airquality_trimmed, aes(x = Month, y = Ozone)) +
        geom_boxplot(fill = fill, colour = line,
                     alpha = 0.7) +
        scale_y_continuous(name = "Mean ozone in\nparts per billion",
                           breaks = seq(0, 175, 50),
                           limits=c(0, 175)) +
        scale_x_discrete(name = "Month") +
        ggtitle("Boxplot of mean ozone by month") +
        theme_bw() +
        facet_grid(. ~ Temp.f)
> p9

## 画在一张图中
> library(RColorBrewer)
> p10 <- ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
        geom_boxplot(alpha=0.7) +
        scale_y_continuous(name = "Mean ozone in\nparts per billion",
                           breaks = seq(0, 175, 25),
                           limits=c(0, 175)) +
        scale_x_discrete(name = "Month") +
        ggtitle("Boxplot of mean ozone by month") +
        theme_bw() +
        scale_fill_brewer(palette = "Accent")
> p10

附录:
Colors in R
Shape in R

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值