ggplot2-设置坐标轴

本文更新地址: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

反转 x轴 与 y轴

bp + coord_flip()

离散型数据的坐标轴

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

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

# 逆转顺序
# 得到 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)

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

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"))

# 隐藏
bp + scale_x_discrete(breaks=NULL)

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

连续型数据的坐标轴

设置坐标轴的范围和颠倒

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

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

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

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

# 同样可以这样
bp + scale_y_continuous(limits=c(0, 8))

# 如果同时设置 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.

如果 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).

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

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

# 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

 ### 点到坐标轴的方向

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

设置和隐藏坐标轴的刻度

# 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))

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

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

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

坐标轴的数据转换(log, sqrt, etc.)

坐标轴可以进行线性变换,比如 log, power, roots 等等

这里有两种方式对数据进行转换, 一种是比例转换, 另一种是坐标转换. 对于比例变换, 在坐标轴刻度和范围被决定之前发生变换, 也就是先绘制图形,在标明刻度; 对于坐标变换, 在坐标轴刻度和范围被决定之后发生变换.也就是先标明刻度再绘制图形.

具体的理解可以看下面的例子.

# 指数分布的数据
set.seed(201)
n <- 100
dat <- data.frame(
    xval = (1:n+rnorm(n,sd=5))/20,
    yval = 2*2^((1:n+rnorm(n,sd=5))/20)
)

# 散点图
sp <- ggplot(dat, aes(xval, yval)) + geom_point()
sp

你可能感兴趣的:(R语言,ggplot2,坐标轴)