zoukankan      html  css  js  c++  java
  • R语言与医学统计图形-【20】ggplot2图例

    ggplot2绘图系统——图例:guide函数、标度函数、overrides.aes参数

    图例调整函数guide_legend也属于标度函数,但不能单独作为对象使用,即不能如p+guide_legend()使用。

    1. guides及guides_legend函数

    guide_legend函数参数:

    guide_legend(title = , #图例标题
                 title.position = ,#top/bottom/right/left
                 title.theme = , #图例风格
                 title.hjust = , #标题水平调整
                 title.vjust = ,
                 label = TRUE, #是否显示标签
                 label.position = , #标签位置,同上title
                 label.theme = ,
                 label.hjust = ,
                 label.vjust = ,
                 keywidth = , #图标宽度
                 keyheight = ,
                 direction = , #图标方向,horizontal/vetical
                 default.unit = 'line',
                 override.aes = list(), #忽略aes中设置
                 nrow = , #几行
                 ncol = , #几列
                 byrow = FALSE, #是否按行
                 reverse = FALSE, #图例是否翻转
                 order = 0,...)
    

    guide_legend结合guides函数调整图例。

    图例的四种形式:fill, color, shape, size,使用guides函数时,使用相应参数即可。

    df <- data.frame(x=1:20,y=1:20,color=letters[1:20])
    p <- ggplot(df,aes(x,y))+geom_point(aes(color=color))
    
    p+guide_legend(title='legend',nrow = 4,ncol = 5) #error
    p+guides(color=guide_legend('legend',nrow=4,ncol=5,label.position='left'))
    

    image.png

    不同的图例可以同时调整。

    dat <- data.frame(x=1:5,y=1:5,p=1:5,q=factor(1:5),r=factor(1:5))
    #生成3种图例
    pp <- ggplot(dat,aes(x,y,color=p,size=q,shape=r))+
      geom_point()
    
    #只使用guides函数
    b=pp+guides(color='colorbar', #颜色条
              size='none', #不显示
              shape='legend') #普通图例
    
    grid.arrange(pp,b,ncol=2)
    

    image.png

    #结合guide_*函数
    c=pp+guides(color=guide_colorbar('color'), #颜色条用相应函数colorbar
              shape=guide_legend('shape',ncol=5))
    
    #将3个图例整合成一个
    d=pp+guides(color=guide_legend('title'),
              size=guide_legend('title'),
              shape=guide_legend('title'))
    
    grid.arrange(c,d,ncol=2)
    

    image.png
    order参数:
    不同图例的排列顺序。

    ggplot(mpg,aes(displ,cty))+
      geom_point(aes(size=hwy,color=cyl,shape=drv))+
      guides(color=guide_colorbar(order = 2),
             shape=guide_legend(order=3),
             size=guide_legend(order=1)) #第一位
    

    image.png

    2. 标度函数调整图例

    在scale_*中使用guide参数。标度函数中,有严格的连续型和离散型变量之分。colorbar针对连续型变量,legend针对离散型变量。

    #只用guide参数
    a=pp+scale_color_continuous(guide='colorbar')+
      scale_shape(guide='legend')+
      scale_size_discrete(guide='legend')
    
    #结合guide_*函数(即自定义图例)
    b=pp+scale_color_continuous(guide=guide_colorbar('color'))+
      scale_shape(guide=guide_legend('shape',ncol=5))+
      scale_size_discrete(guide=guide_legend('size',ncol=5))
    
    grid.arrange(a,b,ncol=2)
    

    image.png

    3. 结合theme函数调整图例

    theme函数修饰图例有关参数:
    image.png
    示例。

    pt <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+
      geom_point()
    a=pt+scale_color_discrete(name='cyl')+
      theme(legend.title = element_text(color='blue'),
            legend.background = element_rect(color = 'red',
                                             linetype = 2))
    
    b=pt+scale_color_discrete(name='cyl')+
      theme(legend.position = 'bottom',
            legend.text = element_text(color='red',size=13,angle = 45),
            legend.key = element_rect(color='black',fill = 'orange'),
            legend.key.height = unit(1,'cm'),
            legend.key.width = unit(1,'cm'))
    grid.arrange(a,b,ncol=2)
    

    image.png

    自定义图例到图形区域中。

    pt <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+
      geom_point()
    pt+scale_color_discrete(name='cyl')+
      theme(legend.position = c(0.9,0.8), #相对位置比例
            legend.title = element_text(color='blue'),
            legend.background = element_rect(color='red',
                                             linetype = 2))
    

    image.png

    4. overrides.aes参数的技巧

    该参数接受list函数,可对图例样式进行修改而不受映射函数的影响。

    示例比较。

    p=ggplot(diamonds,aes(carat,price))+
      geom_point(aes(color=color),alpha=1/100)
    
    b=p+guides(color=guide_legend(override.aes = list(alpha=1)))
    
    grid.arrange(p,b,ncol=2)
    

    image.png

    虽然图上的点模糊,但图例上的点很清晰(将alpha还原为1),这就是overrides.aes参数作用。

    对图例进行其他设置,而不受映射函数干扰,如图例中图标大小。

    
    df <- data.frame(id=rep(c('hq','lq'),each=5),
                     values=rnorm(n=10,mean=1,sd=0.5)+c(1:10),
                     period=rep(c(1:5),2))
    ggplot(df,aes(x=period,y=values,group=id,shape=id,fill=id))+
      geom_line(color='gray40')+
      geom_point(size=2)+
      guides(shape=guide_legend(override.aes = list(size=5)))+
      scale_fill_manual(values=c('lightskyblue1','lightpink'),
                        labels=c('HQ','LQ'))+
      scale_shape_manual(values = c(22,24),labels=c('HQ',"LQ"))
    

    image.png
    为突出图例中的点,图例中的点设置比图中点大。

    fill和shape的labels设置相同,合并了两种图例。

  • 相关阅读:
    SharePoint 2013 商务智能报表发布
    sharepoint designer web 服务器似乎没有安装microsoft sharepoint foundation
    SharePoint 2013 Designer系列之数据视图
    SharePoint 2013 Designer系列之数据视图筛选
    SharePoint 2013 Designer系列之自定义列表表单
    SharePoint 2013 入门教程之创建及修改母版页
    SharePoint 2013 入门教程之创建页面布局及页面
    SharePoint 2010 级联下拉列表 (Cascading DropDownList)
    使用SharePoint Designer定制开发专家库系统实例!
    PL/SQL Developer 建立远程连接数据库的配置 和安装包+汉化包+注册机
  • 原文地址:https://www.cnblogs.com/jessepeng/p/12307769.html
Copyright © 2011-2022 走看看