图例符号居中显示_R可视化08|ggplot2图层-标度图层(scale layer)-图例篇
生活随笔
收集整理的這篇文章主要介紹了
图例符号居中显示_R可视化08|ggplot2图层-标度图层(scale layer)-图例篇
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文詳細介紹ggplot2中圖例標度(legends scales),續(xù)前篇
- R可視化07|ggplot2圖層-標度圖層(scale layer)-顏色盤篇
本文目錄
4、圖例標度(legends scale) 圖例位置設置 修改ggplot2的圖例符號 ggplot2的圖例順序|方向等花里胡哨設置4、圖例標度(legends scale)
- 圖例位置設置
圖例位置通過theme中的legend.position設置,有參數(shù)“right”, “l(fā)eft”, “top”, “bottom”, or “none” (不顯示圖例)可選。
toy <- data.frame(const = 1, up = 1:4,txt = letters[1:4], big = (1:4)*1000,log = c(2, 5, 10, 2000) )base <- ggplot(toy, aes(up, up)) + geom_point(aes(colour = txt), size = 3) + xlab(NULL) + ylab(NULL)#legend.position控制圖例上下左右位置 p1 <- base + theme(legend.position = "left") p2 <- base + theme(legend.position = "right") # 默認圖例在左邊 p3 <- base + theme(legend.position = "bottom") p4 <- base + theme(legend.position = "none")#不顯示圖例#legend.position和legend.justification設置圖例占比圖形比例控制圖例位置 base <- ggplot(toy, aes(up, up)) + geom_point(aes(colour = txt), size = 3) p5 <- base p6 <- base + theme(legend.position = c(0, 1), legend.justification = c(0, 1)) p7 <- base + theme(legend.position = c(0.5, 0.5), legend.justification = c(0.5, 0.5)) p8 <- base + theme(legend.position = c(1, 0), legend.justification = c(1, 0)) p9 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,nrow = 4)ggsave("scale8.png", p9, width = 8, height = 10)- 修改ggplot2的圖例符號draw_key_*函數(shù)指定圖例符號,注意右側(cè)圖例符號變化。
其它draw_key_*圖例符號
- ggplot2的圖例順序|方向等花里胡哨設置
這部分詳細介紹圖例各種個性化設置,看圖找代碼即可。
options(repr.plot.width = 5, repr.plot.height = 15, repr.plot.res = 300) base <- ggplot(mpg, aes(drv, fill = factor(cyl))) + geom_bar() p1 <- base#ncol byrow設置圖例排列順序 p2 <- base + guides(fill = guide_legend(ncol = 2)) p3 <- base + guides(fill = guide_legend(ncol = 2, byrow = TRUE))#reverse顛倒圖例順序 p4 <- base + guides(fill = guide_legend(reverse = TRUE))#override.aes:覆蓋從每個圖層派生的一些圖形屬性設置 base1 <- ggplot(mpg, aes(displ, hwy, colour = drv)) +geom_point(size = 4, alpha = .2, stroke = 0)p5 <- base1 + guides(colour = guide_legend()) p6 <- base1 + guides(colour = guide_legend(override.aes = list(alpha = 1)))#axis圖例處坐標軸控制 base2 <- ggplot(mpg, aes(displ, manufacturer, size = hwy)) +geom_point(alpha = .2) +scale_size_binned()p7 <- base2 p8 <- base2 + guides(size = guide_bins(axis = FALSE))#direction設置圖例方向 p9 <- base2 + guides(size = guide_bins(direction = "vertical")) p10 <- base2 + guides(size = guide_bins(direction = "horizontal"))#show.limits,axis.colour, axis.linewidth和axis.arrow控制圖例處axis屬性base3 <- ggplot(mpg, aes(cyl, displ, colour = hwy)) +geom_point(size = 2)#guide_colourbar() / guide_colorbar()控制colourbar型圖例 p13 <- base3 p14 <- base3 + guides(colour = guide_colourbar(reverse = TRUE))#設置colourbar高度2cm p15 <- base3 + guides(colour = guide_colourbar(barheight = unit(2, "cm")))#guide_coloursteps() / guide_colorsteps()控制刻度顯示形式 base4 <- ggplot(mpg, aes(displ, hwy, colour = cyl)) + geom_point() + scale_color_binned()p16 <- base4 p17 <- base4 + guides(colour = guide_coloursteps(show.limits = TRUE))#colourbar首尾顯示 p18 <- base4 + guides(colour = guide_coloursteps(show.limits = FALSE))p19 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p13,p14,p15,p16,p17,p18,nrow = 8) ggsave("scale10.png", p19, width = 5, height = 15)本文結(jié)束,更多好文,歡迎關(guān)注:pythonic生物人
- Python可視化|Matplotlib39-Matplotlib 1.4W+字教程(珍藏版)
- Python可視化|Matplotlib&Seaborn36(完結(jié)篇)
- python3基礎(chǔ)12詳解模塊和包(庫)|構(gòu)建|使用
- Perl基礎(chǔ)系列合集
- NGS各種組學建庫原理(圖解)
總結(jié)
以上是生活随笔為你收集整理的图例符号居中显示_R可视化08|ggplot2图层-标度图层(scale layer)-图例篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再论C++之垃圾回收(GC)
- 下一篇: 安装 并且使用_安装密封圈的注意事项