美文网首页
R可视化之美之科研绘图-16.坡度图

R可视化之美之科研绘图-16.坡度图

作者: 科研私家菜 | 来源:发表于2022-07-29 15:00 被阅读0次

    本内容为【科研私家菜】R可视化之美之科研绘图系列课程

    快来收藏关注【科研私家菜】


    01 坡度图

    坡度图(slope chart)因形似斜坡而得名,它可以展示某一个指标随着时间推移的变化情况,比较此指标在时间(或位置)前后的不同。
    坡度图(slope chart)可以看成是一种特殊的折线图,一般用于比较在两种不同的情况下,某些类别变量的数据变化。通过坡度图可以更好的看清楚数据的变化。简单来说,坡度图就是绘制两点之间的线。

    02 两年份对比

    library(ggplot2)
    library(scales)
    library(reshape)
    df <- read.csv("第3章_类别比较型图表/Slopecharts_Data1.csv")
    colnames(df) <- c("continent", "1952", "1957")
    left_label <- paste(df$continent, round(df$`1952`),sep=", ")
    right_label <- paste(df$continent, round(df$`1957`),sep=", ")
    df$class <- ifelse((df$`1957` - df$`1952`) < 0, "red", "green")
    
    p <- ggplot(df) + 
      geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) +  #连接线
      geom_vline(xintercept=1, linetype="solid", size=.1) + # 1952年的垂直直线
      geom_vline(xintercept=2, linetype="solid", size=.1) + # 1957年的垂直直线
      geom_point(aes(x=1, y=`1952`), size=3,shape=21,fill="grey80",color="black") + # 1952年的数据点
      geom_point(aes(x=2, y=`1957`), size=3,shape=21,fill="grey80",color="black") + # 1957年的数据点
      scale_color_manual(labels = c("Up", "Down"), values = c("green"="#A6D854","red"="#FC4E07")) +  
      xlim(.5, 2.5) 
    
    # 添加文本信息
    p <- p + geom_text(label=left_label, y=df$`1952`, x=rep(1, NROW(df)), hjust=1.1, size=3.5)
    p <- p + geom_text(label=right_label, y=df$`1957`, x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
    p <- p + geom_text(label="1952", x=1, y=1.02*(max(df$`1952`, df$`1957`)), hjust=1.2, size=5)   
    p <- p + geom_text(label="1957", x=2, y=1.02*(max(df$`1952`, df$`1957`)), hjust=-0.1, size=5) 
    
    p<-p+theme_void()
    p
    

    效果如下:


    03 多年份对比

    library(ggalt)
    
    df <- read.csv("第3章_类别比较型图表/Slopecharts_Data2.csv")
    colnames(df) <- c("continent", 2007:2013)
    
    
    df2<-melt(df, id="continent")
    
    df2$value<-as.numeric(df2$value)
    df2$variable<-as.numeric(df2$variable)
    
    left_label<-paste(df2$continent,  round(df2$value),sep=", ")
    right_label<-paste(df2$continent, round(df2$value),sep=", ")
    
    left_point<-df2$value
    right_point<-df2$value
    class<-df2$variable
      
    for (i in 1:nrow(df2))
    {
      if (df2$variable[i]!=1)
      {
        left_label[i]<-""
        left_point[i]<-NaN
      }
      if (df2$variable[i]!=7)
      {
        right_label[i]<-""
        right_point[i]<-NaN
      }
      
      if (df[df$continent==df2$continent[i],2]>df[df$continent==df2$continent[i],8])
      {
        class[i]<-"green"
      }
      else
      {
        class[i]<-"red"
      }
      
    }
    
    p <- ggplot(df2) + 
      geom_xspline(aes(x=variable, y=value,group=continent, colour=class),size=.75) + 
      geom_vline(xintercept=1, linetype="solid", size=.1) + 
      geom_vline(xintercept=7, linetype="solid", size=.1) +
      geom_point(aes(x=variable, y=left_point), size=3,shape=21,fill="grey80",color="black") + 
      geom_point(aes(x=variable, y=right_point), size=3,shape=21,fill="grey80",color="black") + 
      scale_color_manual(labels = c("Up", "Down"), values = c("green"="#FC4E07",  "red"="#A6D854")) +  
      xlim(-4, 12) 
    
    p <- p + geom_text(label=left_label, y=df2$value, x=rep(1, NROW(df2)), hjust=1.1, size=3.5)
    p <- p + geom_text(label=right_label, y=df2$value, x=rep(7, NROW(df2)), hjust=-0.1, size=3.5)
    p <- p + geom_text(label="2007", x=1, y=1.02*(max(df2$value)), hjust=1.2, size=5)  # title
    p <- p + geom_text(label="2013", x=7, y=1.02*(max(df2$value)), hjust=-0.1, size=5)  # title
    
    p<-p+theme_void()+
      theme(legend.position = "none")
    p
    

    效果如下:


    参考资料

    《R语言数据可视化之美》

    关注R小盐,关注科研私家菜(溦❤工众號: SciPrivate),有问题请联系R小盐。让我们一起来学习 R可视化之美之科研绘图

    相关文章

      网友评论

          本文标题:R可视化之美之科研绘图-16.坡度图

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