ggplot2 theme相關設置—矩形設置(rect)


在主題設置中,rect設置主要用於設置圖例和面板

element_rect(fill = NULL, colour = NULL, size = NULL, linetype = NULL, color = NULL)

參數也相對簡單,基本上是常用的參數,從設置來看,靈活性還是很高的。

下面看些例子:

library(ggplot2)
p<-ggplot(mtcars,aes(mpg,hp,colour=factor(cyl)))+geom_point()
p

p+theme(legend.margin=unit(0.3,"inches"),
        legend.key=element_rect(fill="white",color="blue",size=2),
        legend.key.width=unit(0.5,"inches"),
        legend.key.height=unit(0.5,"inches"),
        legend.direction="horizontal",
        legend.position="top")

  

雖然上圖畫得不美觀,但我們可以很清楚的了解每個參數的作用

除了element_rect調整的圖例外觀以外,

這里通過legend.margin調整圖例與圖形的間的距離

通過legend.key.width和legend.key.height來調整圖例的寬度和高度

通過legend.direction來調整圖例是水平還是垂直的,參數值為"horizontal" 或 "vertical"

通過legend.position來調整圖例所在的位置,參數值為"left", "right", "bottom", "top"

 

p+theme(panel.background=element_rect(fill="white",color="grey50"),
        panel.grid=element_line(color="grey50",size=2),
        panel.grid.major=element_line(size=1,linetype =3,color="grey70"),
        panel.grid.minor=element_line(size=1,linetype =3,color="grey70"))

  

可以看到整個面板的顏色變成白色了,這里是通過panel.background進行調整,面板顏色是用參數fill的顏色進行調整,邊框變成灰色通過color調整

p+theme(plot.background=element_rect(color="blue",fill="grey60"))

 

最后我們還可以對整個外部的面板進行調整。

 

從以上的調整可以看出,element_rect 主要用於圖例和面板的調整。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM