R語言ggplot2繪圖設置X軸刻度,字體大小及繪圖區大小

> colnames(data1)[seq(2,ncol(data1), 15)]
 [1] "AAAA" "AAGG" "ATGC" "ACGT" "AGGA" "TACG" "TTCC" "TCCT" "TGCA" "CATG"
[11] "CTTC" "CCTT" "CGTA" "GAAG" "GTAC" "GCAT" "GGAA" "GGGG"

> ggplot(data2[data2$name==11,], aes(x = Tetra, y = Freq, group = 1)) + geom_line(size=0.2) + theme_bw() + scale_x_discrete(breaks = colnames(data1)[seq(2,ncol(data1), 15)])

// scale_x_discrete 能夠設置x軸顯示的刻度,調整稀疏程度,好比 breaks=seq(0,12200,1000)

> ggplot(data2[data2$name==11,], aes(x = Tetra, y = Freq, group = 1)) + geom_line(size=0.2) + theme_bw() + scale_x_discrete(breaks = colnames(data1)[seq(2,ncol(data1), 15)]) + theme(axis.text.x = element_text(face="bold", color="blue", size=8))

// ggplot2 中有不少 element能夠調整矩形,字體,線段的屬性,好比 element_rect, element_line, element_blank, element_text
// http://docs.ggplot2.org/0.9.2.1/theme.html


// panel.margin 用於 theme() 中,主要用於調整繪圖區域各圖之間的間距
// margin 用於element中,調整element 與周圍圖形元素的距離

// plot.margin 用於 theme()中,用於調整整個繪圖區的邊緣位置

> ggplot(data2[data2$name==11,], aes(x = Tetra, y = Freq, group = 1)) + geom_line(size=0.2) + theme_bw() + scale_x_discrete(breaks = colnames(data1)[seq(2,ncol(data1), 15)]) + theme(axis.text.x = element_text(face="bold", color="blue", size=8), plot.margin = unit(c(2,3,3,4),"cm"))

有三種方法能夠設置x軸或y軸 刻度範圍

> p + scale_x_continuous(limits = c(-5,15))   // 方法一
> p + xlim(-5,15)   // 方法二

> p + xlim(min(dt$A, 0)*1.2, max(dt$A)*1.2)   // 通常使用倍數來限定大小,注意定義最小值的方式
> 

// 在theme 中能夠用 axis.title.x或y 調整座標軸的標識
//  
geom_text(aes(label = "point_k"))   // 這個能夠給點添加文字label

// scale_size 能夠把圖中的數據點轉化爲不一樣大小的點

// //


原始繪圖代碼:html

> library(ggplot2)
> library(reshape2)
> setwd("/Users/m/working/R_bin_two/new_bin")
> data1 <- read.table("result_data_g2.txt", header=T, check.names = F, fill =T)
> data2 <- melt(data1, id="name")
> colnames(data2)[2:3] <- c("Tetra","Freq")
> data2[,1] <- as.factor(data2[,1])
> p <- ggplot(data2, aes(x = Tetra, y = Freq, group = name, colour = name)) 
> p + geom_line(size=0.2) 
> p + theme_bw() 
> p + scale_x_discrete(breaks = colnames(data1)[seq(2,ncol(data1), 15)]) 
> p + guides(color = guide_legend(ncol=6))
> p + theme(axis.text.x = element_text(face="bold", color="blue", size=8), plot.margin = unit(c(2,3,3,4),"cm"))
相關文章
相關標籤/搜索