theme设定ggplot2图形格式

7 篇文章 2 订阅

theme函数

theme函数定义内容

  • plot区域
  • panel区域
  • axis坐标轴
  • legend图例
  • strip分面栏

在这里插入图片描述

theme中使用的元素

  • rect
  • line
  • text

FAQ

坐标轴

坐标轴线格式

axis.line=element_line(color,size,linetype)

坐标刻度线格式

axis.ticks=element_line()

坐标刻度线方向

负数向内,正数向外

axis.ticks.length=unit(2,"mm")

刻度线标签文本

axis.text=element_text(color,size,face,family,margin,vjust,hjust,angle)

坐标轴标题文本

axis.title=element_text(color,size,face,family,margin,vjust,hjust,angle)

图例legend

在这里插入图片描述

图例位置

#pannel外
legend.postion="right" #left, top, bottom

#pannel内,如右上角
legend.position = c(0.9,0.9)
legend.justification = c(0.9,0.9)

图列内元素方向分布

legend.direction = "vertical", #legend内方向
    legend.spacing.x = unit(1,"cm"),
    legend.spacing.y = unit(1,"cm"),

多组图例方向分布

legend.box="horizontal", #legend间排列
    legend.box.just="bottom", 
    legend.box.spacing = unit(1,"cm"),

图例中文本格式

#标题文本
legend.title = element_text(color="red",size=20),
    legend.title.align = 0,

#标签文本
legend.text=element_text(color="blue",size=12,face="bold"),
    legend.text.align=0,

图例中标签格式

legend.key=element_rect(color="red",fill="green"),
legend.key.size = unit(5,"mm"),

面板区panel

网格线

    panel.grid=element_line(color="gray"),
    panel.grid.minor = element_blank(),

边框

panel.border = element_rect(color="black",fill=NA,size=2)
panel.background = element_rect(color="blue",fill="yellow")

面板间距

    panel.spacing.x = unit(4,"cm"),
    panel.spacing.y = unit(2,"cm"),

plot区

格式定义

plot.background = element_rect(fill="gray")
 plot.margin=margin(1,1,1,1,"cm")

标题文本格式及位置

hjust=0.5居中

    plot.title = element_text(size=15,face="bold",hjust=0.5),
    plot.title.position = "panel"

strip面板标签

    strip.background = element_rect(fill="blue"),
    strip.placement = "outside",
    strip.text = element_text(color="yellow"),
    strip.switch.pad.grid = unit(1,"cm"), #outside起效

实例

在这里插入图片描述

library(tidyverse)


p1=mtcars%>%ggplot(aes(wt,qsec))+
  geom_point(aes(color=as.factor(gear),shape=as.factor(am)))+
  labs(x="Length",y="Width",title="qsec~wt",color="Gear",shape="A/M")+
  facet_grid(cyl~vs)

p1

p1+theme(
  #高宽比
   # aspect.ratio = 0.8,
  # 坐标轴标题 axis.title.?(x,y,x.top,bottom,y.left,right)
    axis.title.x=element_text(face="bold",size=15,margin=margin(0.2,0,0,0,"cm")),
    axis.title.y=element_text(face="bold",size=15,margin=margin(0,0.2,0,0,"cm")),
  # 坐标轴刻度标签 axis.text
    axis.text.x= element_text(face="bold",size=12,margin=margin(0.3,0,0,0,"cm")),
    axis.text.y= element_text(face="bold",size=12,margin=margin(0,0.3,0,0,"cm")),
  
  #  坐标轴刻度线 axis.ticks
    axis.ticks=element_line(size=1),
    axis.ticks.length = unit(-2,"mm"),
  # 坐标轴线
   # axis.line=element_line(color="black",size=1),
  # 图例legend
    legend.background = element_rect(color="blue",fill="yellow",size=2),
    legend.margin = margin(0.5,0.5,0.5,0.5,"cm"),
    legend.spacing.x = unit(1,"cm"),
    legend.spacing.y = unit(1,"cm"),
    legend.key=element_rect(color="red",fill="green"),
    legend.key.size = unit(15,"mm"),
    legend.text=element_text(color="blue",size=12,face="bold"),
    legend.text.align=0,
    legend.title = element_text(color="red",size=20),
    legend.title.align = 0,
    legend.position = "left",#bottom,left,top,right
    legend.justification = c(0.9,0.9),
    legend.direction = "vertical", #legend内方向
    legend.box="horizontal", #legend间方向
    legend.box.background = element_rect(fill="red"),
    legend.box.margin = margin(1,1,1,1,"cm"),
    legend.box.just="bottom", 
    legend.box.spacing = unit(1,"cm"),
  
  #pannel
    panel.border = element_rect(color="black",fill=NA,size=2),#最外层
    panel.background = element_rect(color="blue",fill="yellow"),
    panel.spacing.x = unit(4,"cm"),
    panel.spacing.y = unit(2,"cm"),
    panel.grid=element_line(color="gray"),
    panel.grid.minor = element_blank(),
  
  # plot区域
    plot.background = element_rect(fill="gray"),
    plot.title = element_text(size=15,face="bold",hjust=0.5),
    plot.title.position = "panel",
    plot.margin=margin(1,1,1,1,"cm"),
    
  # strip
    strip.background = element_rect(fill="blue"),
    strip.placement = "outside",
    strip.text = element_text(color="yellow"),
    strip.switch.pad.grid = unit(1,"cm"), #outside起效
    
    
  
)


 
 
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值