同樣是一無所有疫衩,有的人覺得有朝一日可以擁有全世界;
希望你是有的人荣德,可以沒臉沒皮地積極向上;
####diamonds數(shù)據(jù)集的格式闷煤,color、cut涮瞻、carat鲤拿、price分別是該數(shù)據(jù)的列
> head(diamonds)
# A tibble: 6 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
準備畫布
- 數(shù)據(jù)需要是數(shù)據(jù)框格式
- diamonds是ggplot2自帶數(shù)據(jù)集
library(ggplot2)
####just set up, no point
####格式:ggplot(df, aes(x, y, other aesthetics))
ggplot(diamonds, aes(x=carat, y=price,color=cut))
空白和xy軸.png
加點署咽,加趨勢線
ggplot(diamonds, aes(x=carat, y=price,color=cut)) +
geom_point(alpha=0.1,size=1.0,shape=21,fill='white',stroke=2)+
geom_smooth(method='glm')
點_線.png
- 依據(jù)cut分組近顷,geom_point自動分配顏色
- alpha設置點的透明度(避免點太大重疊在一起)
- stroke的第一反應是中風,在這里是指宁否,對于有邊界的形狀(可以用vignette("ggplot2-specs")查看相應形狀)窒升,可以為內(nèi)部和外部分別設置顏色,stroke用于設置邊界的寬度家淤;fill即內(nèi)部顏色异剥,這里color是依據(jù)分組的顏色;
- method設定geom_smooth的曲線擬合方式
- vignette("ggplot2-specs")調(diào)出參數(shù)的詳細說明
設置特定顏色
ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
geom_point()+scale_color_brewer(type='qual',palette=8)
- 可通過type選擇‘div’絮重,‘qual’冤寿,‘seq’來設置,palette設置數(shù)字青伤,選擇顏色
Diverging
BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
Qualitative
Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3
Sequential
Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu, YlOrBr, YlOrRd - 還有其他參數(shù)督怜,?scale_color_brewer查找即可
對點的形狀不滿意狠角,可用scale_shape_manual()設置
x号杠、y、figure的title設置
gg <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
geom_point()+scale_color_brewer(type='qual',palette=8)+labs(title="Scatterplot", x="Carat", y="Price")
print(gg)
-
這里將整個設置存為圖片,通過print打印出來進行展示
Rplot_各種標題.png
標題字體設置
gg1 <- gg + theme(plot.title=element_text(size=30, face="bold"),
axis.text.x=element_text(size=15),
axis.text.y=element_text(size=15),
axis.title.x=element_text(size=25),
axis.title.y=element_text(size=25))
print(gg1)
Rplot_標題字體.png
標題字體方向-改為垂直
適用于標題太長的時候姨蟋,可以修改字體的方向
gg2 <- gg + theme(axis.text.x = element_text(angle=90, hjust=1),
axis.text.y = element_text(angle=90, hjust=1))
print(gg2)
image.png
展示不同的分類下的分面
##cut和color是其另外的列
gg1 + facet_wrap( ~ cut)
gg_grid<-gg1 + facet_grid(color ~ cut)
ggsave(gg_grid,filename = 'grid.png',width=20,height=15)
capitalize <- function(string) {
substr(string, 1, 1) <- toupper(substr(string, 1, 1))
string
}
conservation_status <- c(
cd = "Conservation Dependent",
en = "Endangered",
lc = "Least concern",
nt = "Near Threatened",
vu = "Vulnerable",
domesticated = "Domesticated"
)
global_labeller <- labeller(
vore = capitalize,
conservation = conservation_status,
conservation2 = label_wrap_gen(10),
.default = label_both
)
gg_q<-gg1 + facet_wrap(color ~ cut, scales="free",labeller = global_labeller)
ggsave(gg_q,filename = 'test.png',width = 20,height = 15)
- facet_wrap 和facet_grid將一個圖展示成不同的panel
查資料的個人理解如下:
- 單個變量的展示屉凯,用facet_wrap比較方便,也好看一點眼溶;
- 兩個變量的展示悠砚,用facet_grid方便一點;
- 均展示兩個變量的情況堂飞,facet_grid會把兩個變量的所有組合均進行展示灌旧,而facet_wrap只會展示兩個變量的所有組合中真實有數(shù)據(jù)的組合;
- 可以通過設定labeller的方式改變label的展示情況
- 圖太大就存成圖片的格式
Rplot_facet_wrap.png
grid.png
facet_wrap_labeller_set.png
[參考內(nèi)容]
http://r-statistics.co/ggplot2-Tutorial-With-R.html
http://www.rpubs.com/Logos/252923
課程分享
生信技能樹全球公益巡講
(https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g)
B站公益74小時生信工程師教學視頻合輯
(https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw)
招學徒:
(https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw)