美文网首页R语言学习
ggplot2 003 绘图参数 — 标题及图例

ggplot2 003 绘图参数 — 标题及图例

作者: caoqiansheng | 来源:发表于2020-08-17 17:06 被阅读0次

    ggplot2 Graphical Parameters

    1.标题:主标题,轴和图例标题

    ggplot2软件包修改绘图标题(主标题,轴标签和图例标题)主要是通过以下函数:

    • ggtitle(label)#主标题
    • xlab(label)#x轴标签
    • ylab(label)#y轴标签
    • labs(...)#用于主标题,轴标签和图例标题

    1.1 数据准备

    # 将dose转换weight因子
    ToothGrowth$dose <- as.factor(ToothGrowth$dose)
    # 检查数据
    head(ToothGrowth)
    library(ggplot2)
    p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
    p
    
    image.png

    1.2 更改主标题及轴标题

    1.2.1 使用 ggtitle(), xlab() and ylab()
    p + ggtitle("Plot of length \n by dose") +
      xlab("Dose (mg)") + ylab("Teeth length")
    
    image.png
    1.2.2 使用labs()
    p +labs(title="Plot of length \n by dose",
            x ="Dose (mg)", y = "Teeth length")
    
    image.png

    1.3 使用labs()更改图例

    p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+
      geom_boxplot()
    p
    # 更改图例
    p1 <- p + labs(fill = "Dose (mg)")
    
    image.png

    1.4 更改主标题和轴标签的外观

    可以使用theme()和element_text()函数自定义主标题以及x和y轴标签

    # main title
    p + theme(plot.title = element_text(family, face, colour, size))
    # x axis title 
    p + theme(axis.title.x = element_text(family, face, colour, size))
    # y axis title
    p + theme(axis.title.y = element_text(family, face, colour, size))
    
    ## family : font family
    ## face : font face. Possible values are “plain”, “italic”, “bold” and “bold.italic”
    ## colour : text color
    ## size : text size in pts
    ## hjust : horizontal justification (in [0, 1])
    ## vjust : vertical justification (in [0, 1])
    ## lineheight : line height. In multi-line text, the lineheight argument is used to change the
    ## spacing between lines.
    ## color : an alias for colour
    
    • example
    # Default plot
    p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
      ggtitle("Plot of length \n by dose") +
      xlab("Dose (mg)") + ylab("Teeth length")
    p
    # Change the color, the size and the face of
    # the main title, x and y axis labels
    p1 <- p + theme(
    plot.title = element_text(color="red", size=14, face="bold.italic"),
    axis.title.x = element_text(color="blue", size=14, face="bold"),
    axis.title.y = element_text(color="#993333", size=14, face="bold")
    )
    ggarrange(p,p1)
    
    image.png

    1.5 移除x和y轴标签

    可以使用element_blank()函数隐藏主要标题和轴标签

    p + theme(
      plot.title = element_blank(),
      axis.title.x = element_blank(),
      axis.title.y = element_blank())
    
    image.png

    2 图例的位置及外观

    2.1 数据准备

    # 将dose转换weight因子
    ToothGrowth$dose <- as.factor(ToothGrowth$dose)
    # 检查数据
    head(ToothGrowth)
    library(ggplot2)
    p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + 
      geom_boxplot()
    p
    
    image.png

    2.2 更改图例位置

    可以使用theme()函数来更改图例的位置

    # legend.position are : “left”,“top”, “right”, “bottom”,vector c(x,y)
    p1 <- p + theme(legend.position="top")
    p2 <- p + theme(legend.position="bottom")
    p3 <- p + theme(legend.position = c(0.8, 0.2))
    ggarrange(p1,p2,p3,nrow=1)
    
    image.png

    2.3 更改图例标题和文本字体样式

    # 图例标题
    p4 <- p + theme(legend.title = element_text(colour="blue", size=10, 
                                          face="bold"))
    # 图例标签
    p5 <- p + theme(legend.text = element_text(colour="blue", size=10, 
                                         face="bold"))
    ggarrange(p4,p5)
    
    image.png

    2.4 更改图例框的背景颜色

    # 背景颜色修改
    p6 <- p + theme(legend.background = element_rect(fill="lightblue", 
                                               size=0.5, linetype="solid"))
    p7 <- p + theme(legend.background = element_rect(fill="lightblue",
                                               size=0.5, linetype="solid", 
                                               colour ="darkblue"))
    ggarrange(p6,p7)
    
    image.png

    2.5 更改图例项的顺序

    p + scale_x_discrete(limits=c("2", "0.5", "1"))
    
    image.png

    2.6 移除图例

    # Remove only the legend title
    p8 <- p + theme(legend.title = element_blank())
    # Remove the plot legend
    p9 <- p + theme(legend.position='none')
    ggarrange(p8,p9)
    
    image.png

    2.7 删除条形图例中的斜线

    # 默认图
    p10 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) + geom_bar()
    # 更改条形图边框颜色,
    # 但在图例中添加了斜杠
    p11 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose)) +
      geom_bar(colour="black")
    p11
    # 隐藏斜线:
    # 1.绘制没有边框颜色的条,
    # 2.再次用边框颜色绘制条形,但带有空白图例。
    p12 <- ggplot(data=ToothGrowth, aes(x=dose, fill=dose))+ 
      geom_bar() + 
      geom_bar(colour="black", show.legend = FALSE)
    ggarrange(p10,p11,p12,nrow = 1)
    

    然而很尴尬,第二幅图的图例并没有出现斜线

    image.png

    3. guides() 设置或删除图例以获得特定的美感

    3.1 设置或删除具有特定美感的图例(填充,颜色,大小,形状等)

    # Prepare the data : convert cyl and gear to factor variables
    mtcars$cyl<-as.factor(mtcars$cyl)
    mtcars$gear <- as.factor(mtcars$gear)
    head(mtcars)
    p <- ggplot(data = mtcars, 
                aes(x=mpg, y=wt, color=cyl, size=qsec, shape=gear))+
      geom_point()
    # Print the plot without guide specification
    p
    
    image.png

    3.2 更改多个指南的图例位置

    # Change the legend position
    p +theme(legend.position="bottom")
    
    image.png
    # Horizontal legend box
    p +theme(legend.position="bottom", legend.box = "horizontal")
    
    image.png

    3.3 更改多个图例的顺序

    p+guides(color = guide_legend(order=1),
             size = guide_legend(order=2),
             shape = guide_legend(order=3))
    
    image.png

    如果使用连续颜色,则可以使用功能guide_colourbar()更改颜色指南的顺序:

    qplot(data = mpg, x = displ, y = cty, size = hwy,
          colour = cyl, shape = drv) +
      guides(colour = guide_colourbar(order = 1),
             alpha = guide_legend(order = 2),
             size = guide_legend(order = 3))
    
    image.png

    3.4 删除具有特殊美感的图例

    p+guides(color = FALSE, size = FALSE)
    
    image.png
    # Remove legend for the point shape
    p1 <- p+scale_shape(guide=FALSE)
    # Remove legend for size
    p2 <- p +scale_size(guide=FALSE)
    # Remove legend for color
    p3 <- p + scale_color_manual(values=c('#999999','#E69F00','#56B4E9'),
                           guide=FALSE)
    ggarrange(p1,p2,p3,nrow = 1)
    
    image.png

    相关文章

      网友评论

        本文标题:ggplot2 003 绘图参数 — 标题及图例

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