美文网首页R plot
每日一图 | 坡度图(二)

每日一图 | 坡度图(二)

作者: R语言搬运工 | 来源:发表于2021-11-18 12:52 被阅读0次

    \color{blue}{此文内容首发于微信公众号:R语言搬运工,关注公众号浏览更多精彩内容}

    \color{red}{交流分享扣扣群:925920448}

    --原文链接--

    image.png image.png

    上图中,可以看出不同类别在这几年中的高低起伏变化,红色的依然为2021年和2014年比下降的,蓝色为上升的。

    library(ggplot2)
    library(reshape2)
    library(ggalt)
    
    data <- read.table("clipboard",header=T,sep='\t')
    data$class <- ifelse((data$X2021 - data$X2014) < 0, "red", "green")
    colnames(data) <- c("name","2014","2015","2016","2017","2018","2019","2020","2021","class")
    data1 <- melt(data,c("name","class"))
    
    left_label <- paste(data$name, round(data$`2014`),sep=",")
    right_label <- paste(data$name, round(data$`2021`),sep=",")
    
    ggplot()+
      geom_xspline(data=data1,aes(x=variable,y=value,group=name,color=class))+
      geom_vline(xintercept=2, linetype="solid", size=.1) +
      geom_vline(xintercept=9, linetype="solid", size=.1) +
      geom_point(aes(x=2, y=data$`2014`), size=3,shape=21,fill="grey80",color="black") +
      geom_point(aes(x=9, y=data$`2021`), size=3,shape=21,fill="grey80",color="black") +
      scale_color_manual(labels = c("Up", "Down"), values = c("green"="blue","red"="red"))+
      geom_text(data=data,label=left_label, y=data$`2014`, x=rep(2, 15), hjust=1.1, size=3.5)+
      geom_text(data=data,label=right_label, y=data$`2021`, x=rep(9, 15), hjust=-0.1, size=3.5)+
      geom_text(data=data,label="2014", x=2, y=1.08*(max(data$`2014`, data$`2021`)), hjust=1.2, size=5)+
      geom_text(data=data,label="2021", x=9, y=1.08*(max(data$`2014`, data$`2021`)), hjust=-0.1, size=5)+
      ylim(0,4000)+scale_x_discrete(limits=c("2013","2014","2015","2016","2017","2018","2019","2020","2021","2022"))+
      theme_void()
    

    \color{blue}{此文内容首发于微信公众号:R语言搬运工,关注公众号浏览更多精彩内容}

    \color{red}{交流分享扣扣群:925920448}

    精彩推荐:

    R语言绘制散点图geom_point
    R语言添加拟合曲线geom_smooth
    R语言箱线图boxplot
    R语言线图geom_line

    \color{#D2691E}{喜欢}\color{#6495ED}{我们}\color{#DC143C}{的}\color{#7FFF00}{文章}\color{#FF1493}{就}\color{ #1E90FF}{点个}\color{#FFD700}{赞赏}\color{#FF69B4}{吧}\color{#8A2BE2}{!}
    ![image.png](https://upload-images.jianshu.io/upload_images/27049

    相关文章

      网友评论

        本文标题:每日一图 | 坡度图(二)

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