美文网首页
ggplot2||图层添加特定斜率的直线并显示对应图例

ggplot2||图层添加特定斜率的直线并显示对应图例

作者: CopLee | 来源:发表于2020-05-05 22:38 被阅读0次

    需求描述

    上次内容讲述了在做好的图层中添加特定的阴影区,来显示着重突出的部分。本次内容学习一下在图层中添加特定斜率的直线并在图例中显示,我们知道添加特定斜率的直线一般分为水平线geom_hline,垂直线geom_vline和特定斜率的直线geom_abline,添加对应的图例诸如scale_shape_manualscale_linetype_manual,scale_color_manual,...

    1. 数据导入

    本次内容以数据集mtcars为例,来演示图形绘制。

    rm(list = ls(all=TRUE))#清除所有变量内存
    library(ggplot2)#load packages
    data(mtcars)
    head(mtcars)
    > head(mtcars)
                       mpg cyl disp  hp drat    wt  qsec vs am gear carb
    Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
    Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
    Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
    Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
    

    2. 图形绘制(散点图)

    #设置斜率直线属性
    df_abline <- data.frame(intercept=c(.2,1.05,2),slope=c(3, 1.5,.7),linetype=factor(c(1,3,5)))
    #作图
    p <- ggplot(data=mtcars, aes(x=wt, y=drat)) + 
      geom_point(aes(shape = factor(cyl),color = factor(cyl))) +#赋值给颜色或性状属性(分组因子变量)
      geom_abline(data=df_abline, aes(intercept=intercept,slope=slope, linetype=linetype)) +
      labs(color="",linetype="") +
      scale_shape_manual(name = "",
                         values=c(1,2,4),
                         labels=c("4","6","8")) +
      scale_linetype_manual(name = "",
                            values=c(1,3,2),
                            labels=c("a","b","c"))
    p
    
    geom_abline

    2.1 添加水平或者垂直线

    df_hline <- data.frame(yintercept=c(3,3.5,4),linetype=factor(c(1,3,5)))
    #df_vline <- data.frame(xintercept=c(2,3.5,5),linetype=factor(c(1,3,5)))
    p1 <- ggplot(data=mtcars, aes(x=wt, y=drat)) + 
      geom_point(aes(shape = factor(cyl),color = factor(cyl))) +
      geom_hline(data = df_hline, aes(yintercept = intercept, linetype = linetype)) +
      labs(color="",linetype="") +
    
      scale_shape_manual(name = "",
                         values=c(1,2,4),
                         labels=c("4","6","8")) +
      scale_linetype_manual(name = "",
                            values=c(1,3,2),
                            labels=c("a","b","c"))
    p1
    
    Rplot_hline
    Rplot_vline

    3. 散点图+趋势线

    df_abline <- data.frame(intercept=c(.2,1.05,2),slope=c(3, 1.5,.7),linetype=factor(c(1,3,5)))
    
    p2 <- ggplot(data=mtcars, aes(x=wt, y=drat)) + 
      geom_point(aes(shape = factor(cyl),color = factor(cyl))) +
      stat_smooth(aes(linetype="regression"),method = "lm",
                  formula = y ~ x, se = TRUE, colour = 1, size = 0.5) +
      geom_abline(data = df_abline, aes(intercept = intercept,slope = slope, linetype = linetype)) +
      labs(color="",linetype="") +
    
      scale_shape_manual(name = "",
                         values=c(1,2,4),
                         labels=c("4","6","8")) +
      scale_linetype_manual(name = "",
                            values=c(1,3,2,4),
                            labels=c("a","b","c","regression"))
    p2
    
    Rplot_gression
    可以看出当加入趋势线后,legend发生了一些变化

    4. 条形图

    (a) 将vline添加到现有绘图并使其出现在ggplot2图例中?
    (b) 将一条线添加到ggplot2图并调整图例
    (c) How to make geom_abline show_guide line types show up in legend correctly

    set.seed(20200505)
    df <- data.frame(val=rnorm(300, 75, 10))
    cuts1 <- data.frame(Thresholds="Thresholds A", vals=c(43, 70, 90)) 
    cuts2 <- data.frame(Thresholds="Thresholds B", vals=c(46, 79, 86)) 
    cuts <- rbind(cuts1,cuts2) 
    
    p3 <- ggplot(data=df, aes(x=val)) + 
      geom_histogram() + 
      geom_vline(data=cuts, 
                 aes(xintercept=vals, 
                     linetype=Thresholds, 
                     colour = Thresholds), 
                 show.legend = TRUE)
    p3
    
    Rplot_barplot
    Rplot02_hline
    Rplot02_xyz

    后记

    全是图,和移栽的代码是为总结记录

    PS

    2020/05/05 22:40
    于疫情劳动节假期

    相关文章

      网友评论

          本文标题:ggplot2||图层添加特定斜率的直线并显示对应图例

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