美文网首页
R可视化之美之科研绘图-15.克利夫兰点图、棒棒糖图、哑铃图

R可视化之美之科研绘图-15.克利夫兰点图、棒棒糖图、哑铃图

作者: 科研私家菜 | 来源:发表于2022-07-23 17:42 被阅读0次

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

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


    01 棒棒糖图

    library(ggplot2)
    library(reshape2)
    
    mydata<-read.csv("第3章_类别比较型图表/DotPlots_Data.csv",sep=",",na.strings="NA",stringsAsFactors=FALSE)
    mydata$sum<-rowSums(mydata[,2:3])
    
    order<-sort(mydata$sum,index.return=TRUE,decreasing = FALSE)
    mydata$City<- factor(mydata$City, levels = mydata$City[order$ix])
    
    ggplot(mydata, aes(sum, City)) +
      geom_segment(aes(x=0, 
                       xend=sum,
                       y=City, 
                       yend=City))+
      geom_point(shape=21,size=3,colour="black",fill="#FC4E07")+
      theme(
        axis.title=element_text(size=13,face="plain",color="black"),
        axis.text = element_text(size=10,face="plain",color="black"),
        legend.title=element_text(size=14,face="plain",color="black")
      )
    

    效果如下:


    02 克利夫兰点图

    
    mydata<-read.csv("第3章_类别比较型图表/DotPlots_Data.csv",sep=",",na.strings="NA",stringsAsFactors=FALSE)
    mydata$sum<-rowSums(mydata[,2:3])
    
    order<-sort(mydata$sum,index.return=TRUE,decreasing = FALSE)
    mydata$City<- factor(mydata$City, levels = mydata$City[order$ix])
    
    ggplot(mydata, aes(sum, City)) +
      geom_point(shape=21,size=3,colour="black",fill="#FC4E07")+
      theme(
        axis.title=element_text(size=13,face="plain",color="black"),
        axis.text = element_text(size=10,face="plain",color="black"),
        legend.title=element_text(size=14,face="plain",color="black")
      )
    
    

    效果如下:


    03 哑铃图

    mydata<-read.csv("第3章_类别比较型图表/DotPlots_Data.csv",sep=",",na.strings="NA",stringsAsFactors=FALSE)
    mydata$City <- factor(mydata$City, levels = mydata$City[order(mydata$Female)])
    mydata<-melt(mydata,id.vars='City')
    
    ggplot(mydata, aes(value,City,fill=variable)) +
      geom_line(aes(group = City)) +
      geom_point(shape=21,size=3,colour="black")+
      scale_fill_manual(values=c("#00AFBB", "#FC4E07","#36BED9"))+
      theme(
        axis.title=element_text(size=13,face="plain",color="black"),
        axis.text = element_text(size=10,face="plain",color="black"),
        legend.title=element_text(size=12,face="plain",color="black"),
        legend.background = element_blank(),
        legend.position = c(0.85,0.12)
      )
    

    效果如下:


    参考资料

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

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

    相关文章

      网友评论

          本文标题:R可视化之美之科研绘图-15.克利夫兰点图、棒棒糖图、哑铃图

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