ggplot2图例系统

作者: drlee_fc74 | 来源:发表于2018-09-23 23:06 被阅读64次

    连续型图例的修改

    对于图例的修改是先试用guides函数。然后制定修改那个图例。再制定图例是离散型还是连续性 比如如果要修改一个连续性的填充图例则为 guides(fill = guide_colorbar(…………))

    library(ggplot2)
    mydata5 <- data.frame(
      x = runif(100,0,100),
      y = runif(100,0,100),
      z = runif(100,0,100),
      f = runif(100,0,100),
      g = rep(LETTERS[1:5],each = 20))
    p <- ggplot(mydata5, aes(x,y)) + geom_point(aes(fill = z, size = f), shape = 21);p
    
    image.png

    图例标题的修改

    p1 <- p + guides(fill = guide_colorbar(title = "colorbar", ##图例的名字
                             title.position = "right", ##可以选top, left, right, bottom  这里指的是注释标题相对于注释的位置
                             title.theme = element_text(size = 15, face = "italic", color = "red", angle = 45), ##对图例字体的描述
                             title.hjust = - .5, ##题目水平对齐的位置 正为向左移动,负为向右移动
                             title.vjust = -.5 ##图例题目垂直对齐的位置 其中正为向上移动,负为向下移动。
                             )); p1
    
    image.png

    图例刻度注释的调整

    p2 <- p + guides(fill = guide_colorbar(label =  T, ##刻度是否显示,逻辑值
                                           label.position = "left", ##可以使用的是left 和 right
                                           label.theme = element_text(size = 15, face = "italic", color = "red", angle = 30),
                                           label.hjust = .5,
                                           label.vjust = .5
                                           ));p2
    
    image.png

    图例宽窄的调整

    p3 <- p + guides(fill = guide_colorbar(barwidth = unit(6, "cm"), ##图例的宽度
                                           barheight = unit(6, "cm"), ##图例的高度
                                           raster = T ## 如果为TRUE,则颜色条将呈现为栅格对象。 如果为FALSE,则颜色条呈现为一组矩形
                                           ));p3
    
    image.png

    图例边框的调整.

    默认的边框是没有颜色的,线条是1,宽度是0.5

    p4 <- p + guides(fill = guide_colorbar(frame.colour = "red", frame.linetype = 3, frame.linewidth = 3
                                           ));p4
    
    image.png

    颜色栏的刻度线调整。默认的是显示刻度线,颜色为白色,线条宽0.5

    p5 <- p + guides(fill = guide_colorbar(ticks = T, 
                                          ticks.colour = "red", 
                                          ticks.linewidth = 2));p5
    
    image.png

    制定上下刻度线最大值是否显示 但是。修改没有发现变化。。。。

    p6 <- p + guides(fill = guide_colorbar(nbin = 2000, ##指定绘制颜色条的箱数,可以让颜色变得更加平滑。默认为20
                                           draw.llim = T, draw.ulim = F));p6
    
    image.png

    图例整体的调整

    p7 <- p + guides(fill = guide_colorbar(direction = "horizontal", ##整个图例的方向。 可选"horizontal" or "vertical."默认是垂直的。
                                           reverse = T,  ##是否图例的顺序上下颠倒。
                                           order =  1##这个图例和其他图例的相对位置
                                           ));p7
    
    image.png

    离散型图例修改

    离散型图例修改使用的是guide_legend()函数。 这个函数可以除了离散型里面的参数。还有几个特定的参数 #### key的调整 调整离散型每个小图例之间的距离和高度

    p + guides(size = guide_legend(keywidth = 5, keyheight = 2))
    
    image.png

    小刻度放置顺序

    nrow 和ncol 来制定每一列和每一行放多少个。 byrow 按照行排序

    p + guides(size = guide_legend(nrow = 2, byrow = T))
    
    image.png

    图例的隐藏

    图例的隐藏直接可以再guides(fill = F)即可隐藏fill的图例

    p + guides(fill = F)
    
    image.png

    图例位置的变化

    通过theme函数来改变图例的位置

    p + theme(legend.position = "top")
    
    image.png

    相关文章

      网友评论

        本文标题:ggplot2图例系统

        本文链接:https://www.haomeiwen.com/subject/qwaqoftx.html