本文更新地址:http://blog.csdn.net/tanzuozhev/article/details/51107583

本文在 http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/ 的基础上增加了自己的理解

基本箱线图

library(ggplot2)

bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) +
    geom_boxplot()
bp

ggplot2-设置坐标轴

反转 x轴 与 y轴

bp + coord_flip()

ggplot2-设置坐标轴

离散型数据的坐标轴

改变坐标轴中各项目的顺序 > 特别注意, 离散数据的坐标轴中数据做为 factor 变量处理,他的位置取决于 level的顺序

# 手动设置x轴的位置
bp + scale_x_discrete(limits=c("trt1","trt2","ctrl"))

ggplot2-设置坐标轴

# 逆转顺序
# 得到 factor 变量的 level
flevels <- levels(PlantGrowth$group)
flevels
## [1] "ctrl" "trt1" "trt2"
# 逆转了 level 的顺序
flevels <- rev(flevels)
flevels
## [1] "trt2" "trt1" "ctrl"
bp + scale_x_discrete(limits=flevels)

ggplot2-设置坐标轴

# 或者写到一行里面
bp + scale_x_discrete(limits = rev(levels(PlantGrowth$group)))

ggplot2-设置坐标轴

scale_x_discrete 能够设置离散型(discrete)数据, 中间的 x 表示处理x轴,假设是 fill 则能够改动填充颜色, color 改动边框颜色, shape 改动形状……

设置坐标轴的标签

# 将原有的 "ctrl", "trt1", "trt2" 改动为 "Control", "Treat 1", "Treat 2"
bp + scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"),
                      labels=c("Control", "Treat 1", "Treat 2"))

ggplot2-设置坐标轴

# 隐藏
bp + scale_x_discrete(breaks=NULL)

ggplot2-设置坐标轴

# 也能够这样通过设置 theme 实现
bp + theme(axis.ticks = element_blank(), axis.text.x = element_blank())

ggplot2-设置坐标轴

连续型数据的坐标轴

设置坐标轴的范围和颠倒

# Make sure to include 0 in the y axis
bp + expand_limits(y=0) # y轴从0開始

ggplot2-设置坐标轴

# 设置y轴的范围
bp + expand_limits(y=c(0,8))

ggplot2-设置坐标轴

我们能够通过expand_limits设置坐标轴的范围, 可是假设 scale_y_continuous 被使用, 那么就会覆盖ylim的设置.

# 设置y轴的范围
bp + ylim(0, 8)

ggplot2-设置坐标轴

# 相同能够这样
bp + scale_y_continuous(limits=c(0, 8))

ggplot2-设置坐标轴

# 假设同一时候设置 scale_y_continuous 和 ylim那么ylim会被覆盖,首先运行scale_y_continuous
bp + scale_y_continuous(limits=c(0, 8))+
  ylim(0,10)
## Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.

ggplot2-设置坐标轴

假设 y 的范围使用上面的方法被截取, 那么这个范围以外的数据会被忽略,原始数据中的数据相同会被忽略,比方设置了ylim(5,8),那么小于5和大于8的原始数据相同会被忽略,当然散点图没有问题,可是箱线图会出错.

为了避免这个问题能够使用coord_cartesian来设置范围.

能够看以下的样例, 第一个出错了, 第二个使用了coord_cartesian得到了正确的画图.

# These two do the same thing; all data points outside the graphing range are
# dropped, resulting in a misleading box plot
bp + ylim(5, 7.5)
## Warning: Removed 13 rows containing non-finite values (stat_boxplot).

ggplot2-设置坐标轴

# bp + scale_y_continuous(limits=c(5, 7.5))

# Using coord_cartesian "zooms" into the area
bp + coord_cartesian(ylim=c(5, 7.5))

ggplot2-设置坐标轴

# Specify tick marks directly
bp + coord_cartesian(ylim=c(5, 7.5)) + 
    scale_y_continuous(breaks=seq(0, 10, 0.25))  # Ticks from 0-10, every .25

ggplot2-设置坐标轴 ### 点到坐标轴的方向

# Reverse order of a continuous-valued axis
bp + scale_y_reverse()

ggplot2-设置坐标轴

设置和隐藏坐标轴的刻度

# Setting the tick marks on an axis
# 显示刻度从1到10,间隔为0.25
# The scale will show only the ones that are within range (3.50-6.25 in this case)
bp + scale_y_continuous(breaks=seq(1,10,1/4))

ggplot2-设置坐标轴

# 未设置刻度的地方会出现空白
bp + scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6,8))

ggplot2-设置坐标轴

# 隐藏刻度
bp + scale_y_continuous(breaks=NULL)

ggplot2-设置坐标轴

# 隐藏刻度可是显示标签
bp + theme(axis.ticks = element_blank())

ggplot2-设置坐标轴

相关文章: