• R语言仪表盘


    • 单个仪表盘

    导入包及代码:

    library(ggplot2)
    library("showtext")
    library(Cairo)
    library("Rmisc")
    library(grid)
    setwd("F:")
    font.add("myfont","msyh.ttc")
    bardata<-seq(from=0,to=270,length=1000)
    rectdata<-seq(from=0,to=270,by=27)%>%c(360)
    
    target<-1
    assist<-target*270
    
    CairoPNG(file="I_LOVE_YOU.png",width=800,height=540)
    showtext.begin()
    ggplot(data=NULL)+
      geom_rect(aes(xmin=rectdata[-12],xmax=rectdata[-1],ymin=5,ymax=10),fill="#F2F2F2",col="white")+
      geom_bar(aes(x=bardata,y=5,col=bardata),stat="identity",fill=NA,size=2)+
      geom_text(aes(x=rectdata[-12],y=-5,label=seq(0,100,by=10)),vjust=.5,hjust=.5,size=5,family="myfont",col="#0F1110")+
      geom_segment(aes(x=assist,y=-50,xend=assist,yend=-10),arrow =arrow(length=unit(0.4,"cm")),size=1.2,col="red")+
      geom_point(aes(x=assist,y=-50),shape=21,fill="white",col="black",size=7)+
      annotate("text",x=315,y=-30,label="100%",size=12,hjust=.5,vjust=.5,family="myfont",col=ifelse(target<.5,"#F32626","#38E968"),fontface="plain")+ 
      annotate("text",x=315,y=-15,label="我爱你",size=15,hjust=.5,vjust=.5,family="myfont",col=2)+ 
      ylim(-50,12)+
      coord_polar(theta="x",start=179.85)+
      scale_colour_gradient(low="#F32626",high="#38E968",guide=FALSE)+
      theme_minimal()+
      theme(
        text=element_blank(),
        line=element_blank(),
        rect=element_blank()
      )
    showtext.end()
    dev.off()
    

      

    • 多个仪表盘
    set.seed(123)
    target<-runif(5,0,1)
    assist<-270*target
    
    CairoPNG(file="bigdashboard.png",width=1500,height=675)
    showtext.begin()
    grid.newpage()
    pushViewport(viewport(layout=grid.layout(1,5)))
    vplayout<-function(x,y){viewport(layout.pos.row =x,layout.pos.col=y)}
    for(i in 1:5){
    p<-ggplot(data=NULL)+
    geom_rect(aes(xmin=rectdata[-12],xmax=rectdata[-1],ymin=5,ymax=12),fill="#F2F2F2",col="white")+
    geom_bar(aes(x=bardata,y=5,col=bardata),stat="identity",fill=NA,size=2)+
    geom_text(aes(x=rectdata[-12],y=-5,label=seq(0,100,by=10)),vjust=.5,hjust=.5,size=3.5,family="myfont",col="#0F1110")+
    geom_segment(aes(x=assist[i],y=-50,xend=assist[i],yend=-10),arrow =arrow(length=unit(0.4,"cm")),size=1.2,col="red")+
    geom_point(aes(x=assist[i],y=-50),shape=21,fill="white",col="black",size=7)+
    annotate("text",x=315,y=-30,label=percent(target[i]),size=7.5,hjust=.5,vjust=.5,family="myfont",col=ifelse(target[i]<.5,"#F32626","#38E968"),fontface="plain")+ 
    annotate("text",x=315,y=-15,label=paste0("指标",i),size=8.5,hjust=.5,vjust=.5,family="myfont")+ 
    ylim(-50,12)+
    coord_polar(theta="x",start=179.85)+
    scale_colour_gradient(low="#F32626",high="#38E968",guide=FALSE)+
    theme_minimal()+
    theme(
    text=element_blank(),
    line=element_blank(),
    rect=element_blank()
    )
    print(p,vp=vplayout(1,i))
    }
    showtext.end()
    dev.off()
    

      

  • 相关阅读:
    Lodop实现web套打
    oracle监听文件 内容
    【数据存储】【Redis】第四章:高并发下实现分布式锁
    Demo:第二章:Java实现随机图像生成(人像,汽车,房屋等等)
    【数据存储】【Redis】第五章:Redis缓存String类型的使用场景
    【数据存储】【Redis】第三章: Redis五大数据类型实现原理
    【Java面试】:第三章:P6级面试
    【数据存储】【Redis】第二章:底层数据结构
    实战:第二十二章:i18n国际化(已实现中文,英文,波兰文)
    Demo:第三章:权限框架spring security oauth2
  • 原文地址:https://www.cnblogs.com/yijiaming/p/10081981.html
Copyright © 2020-2023  润新知