圖片大小設(shè)定
1
2
3
4
|
x = c(1:10) y = c(11:20) par(pin = c(5,3)) #pin()函數(shù)控制圖形的尺寸 plot(x = x, y = y) |
1
2
3
4
|
x = c(1:10) y = c(11:20) par(pin = c(2,3)) #pin()函數(shù)控制圖形的尺寸 plot(x = x, y = y) |
補(bǔ)充:R語(yǔ)言ggplot2繪圖設(shè)置X軸刻度,字體大小及繪圖區(qū)大小
如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
> 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 可以設(shè)置x軸顯示的刻度,調(diào)整稀疏程度,比如 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可以調(diào)整矩形,字體,線(xiàn)段的屬性,比如 element_rect, element_line, element_blank, element_text // http: //docs .ggplot2.org /0 .9.2.1 /theme .html // panel.margin 用于 theme() 中,主要用于調(diào)整繪圖區(qū)域各圖之間的間距 // margin 用于element中,調(diào)整element 與周?chē)鷪D形元素的距離 // plot.margin 用于 theme()中,用于調(diào)整整個(gè)繪圖區(qū)的邊緣位置 > 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" )) |
有三種方法可以設(shè)置x軸或y軸 刻度范圍
1
2
3
4
5
6
7
8
9
|
> 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) // 一般使用倍數(shù)來(lái)限定大小,注意定義最小值的方式 > // 在theme 中可以用 axis.title.x或y 調(diào)整坐標(biāo)軸的標(biāo)識(shí) // geom_text(aes(label = "point_k" )) // 這個(gè)可以給點(diǎn)添加文字label // scale_size 可以把圖中的數(shù)據(jù)點(diǎn)轉(zhuǎn)化為不同大小的點(diǎn) // // |
原始繪圖代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
> 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" )) |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/tandelin/article/details/94769728