【3.2】ggplot2--坐标轴

1.x,y轴互换

ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
 ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() + coord_flip()

2.坐标轴范围确定

ylim(0, 10)
 scale_y_continuous(limits=c(0, 10))

 discrete数据
 p <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
 p + scale_x_discrete(limits=c("trt1","ctrl","trt2"))

3.轴上的数字反转过来

ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() +
 scale_y_reverse(limits=c(8, 0))

4.调节x轴和y轴的 Scaling Ratio

x轴和Y轴线段长度代表的数字比例一样

 library(gcookbook) # For the data set
 sp <- ggplot(marathon, aes(x=Half,y=Full)) + geom_point()
 sp + coord_fixed()

 sp + coord_fixed(ratio=1/2) +
 scale_y_continuous(breaks=seq(0, 420, 30)) +
 scale_x_continuous(breaks=seq(0, 420, 15))

5.改变刻度线(tick)

刻度线叫tick marker,数字或字符代表tick label,图上tick对应的线条叫grid lines

 ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
 ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() +
 scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6, 8))

 删掉刻度线和刻度线对应的文字
 p + theme(axis.ticks = element_blank(), axis.text.y = element_blank())

To remove the tick marks, the labels, and the grid lines, set breaks to NULL (Figure 8-14, right):

p + scale_y_continuous(breaks=NULL)

6.改变tick对应的内容

替换

 library(gcookbook) # For the data set
 hwp <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) +
 geom_point()
 hwp

 hwp + scale_y_continuous(breaks=c(50, 56, 60, 66, 72),
 labels=c("Tiny", "Really\nshort", "Short",
 "Medium", "Tallish"))

批量转换

 footinch_formatter <- function(x) {
 foot <- floor(x/12)
 inch<-x%%12
 return(paste(foot, "'", inch, "\"", sep=""))
 }

 hwp + scale_y_continuous(labels=footinch_formatter)

 改变tick文字的属性
 bp + theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))


 bp + theme(axis.text.x = element_text(family="Times", face="italic",colour="darkred", size=rel(0.9)))

7.改变坐标轴内容

hwp + xlab("Age in years") + ylab("Height in inches")
 hwp + labs(x = "Age in years", y = "Height in inches")

 方法二
 hwp + scale_x_continuous(name="Age in years")
 hwp + scale_x_continuous(name="Age\n(years)")


scale_x_continuous(limits=c(1950,2000),breaks=seq(1950,2000,5)) 
scale_x_continuous(breaks=c(-2.5,0.0,2.5), labels=c("two", "five", "eight"))
breaks:x标签位置
labels:x标签名称


scale_y_continuous(name=NULL, limits=NULL, breaks=NULL, labels=NULL, trans=NULL, expand=c(0.05, 0), minor_breaks=NULL, formatter="scientific", ...)

8.删掉坐标轴内容

p <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
 p + theme(axis.title.x=element_blank())
 p + xlab("")

9.改变坐标轴出现得形式

library(gcookbook) # For the data set
 hwp <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()

 hwp + theme(axis.title.x=element_text(face="italic", colour="darkred", size=14))

 hwp + ylab("Height\n(inches)") +
 theme(axis.title.y=element_text(angle=0, face="italic", size=14))

10.对坐标轴的两条线进行处理

library(gcookbook) # For the data set
 p <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
 p + theme(axis.line = element_line(colour="black"))

 With thick lines, only half overlaps
 p + theme_bw() +
 theme(panel.border = element_blank(),
 axis.line = element_line(colour="black", size=4))
 # Full overlap
 p + theme_bw() +
 theme(panel.border = element_blank(),
 axis.line = element_line(colour="black", size=4, lineend="square")) 

10.坐标log化

library(MASS) # For the data set
 # The base plot
 p <- ggplot(Animals, aes(x=body, y=brain, label=rownames(Animals))) +
 geom_text(size=3)
 p
 # With logarithmic x and y scales
 p + scale_x_log10() + scale_y_log10()

library(scales)
 p + scale_x_log10(breaks=10^(-1:5),
 labels=trans_format("log10", math_format(10^.x))) +
 scale_y_log10(breaks=10^(0:3),
 labels=trans_format("log10", math_format(10^.x)))

By default, the minor grid lines appear visually halfway between the major grid lines, but this is not the same place as the “5” tick marks on a logarithmic scale. To get them to be the same, you can manually set the scale’s minor_breaks. To do this, we need to set them to log10(5*10^(minpow:maxpow)), which reduces to log10(5) + minpow:max pow

ggplot(Animals, aes(x=body, y=brain, label=rownames(Animals))) +
 geom_text(size=3) +
 annotation_logticks() +
 scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
 labels = trans_format("log10", math_format(10^.x)),
 minor_breaks = log10(5) + -2:5) +
 scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
 labels = trans_format("log10", math_format(10^.x)),
 minor_breaks = log10(5) + -1:3) +
 coord_fixed() +
 theme_bw()

##参考资料:

R Graphics Cookbook by Winston Chang (O’Reilly)

个人公众号,比较懒,很少更新,可以在上面提问题:

更多精彩,请移步公众号阅读:

Sam avatar
About Sam
专注生物信息 专注转化医学