多维放射状流向图的最佳布局方案
最近,有很多小伙伴儿跟我咨询一个比较复杂的地图图表画法。
需求是这样的,一个国家各省或者全球各国之间存在的贸易关系、或者其他经济往来。想要用线条来表达这些指标的流向,同时使用线条粗细来表达指标流向的量级,我给他们的建议是,虽然你很明确要表达的意思,但是实际上这种形式所呈现的最终结果,可能并非你想要的。
如果在一个地图中这些线条都是从一个点发散出来的,这种表达形式虽说不妥,但是不算糟糕,但是倘若你的数据中是多个发散中心,即每个城市都会向其他各个城市发散出一组放射线条,同时线条还有粗细之分,那么最终的效果简直惨不忍睹。
当然我还是会用案例来把这种常规的想法用代码演示一遍,同时给出自己觉得最优的两种解决思路:
#加载包:
library(ggplot2)
library(dplyr)
library(rgdal)
library(shiny)
library(shinythemes)
##转换为数据框并合并城市数据:
china_map <- fortify(china_map)
province_city <- read.csv("D:/R/rstudy/Province/chinaprovincecity.csv",stringsAsFactors = FALSE,check.names = FALSE)
###构造线条起始点数据:
city<-c("北京","上海","重庆","天津","武汉","南京","广州","沈阳","西安","郑州")
city_data<-merge(city,city)%>%rename(Start=x,End=y)%>%arrange(Start)
city_data<-city_data%>%merge(province_city[,c("city","jd","wd")],by.x="Start",by.y="city",all.x=TRUE)%>%rename(Start_long=jd,Start_lat=wd)
city_data<-city_data%>%merge(province_city[,c("city","jd","wd")],by.x="End",by.y="city",all.x=TRUE)%>%rename(End_long=jd,End_lat=wd)
city_data<-transform(city_data,zhibiao1=runif(nrow(city_data),0,100),zhibiao2=runif(nrow(city_data),0,100),zhibiao3=runif(nrow(city_data),0,100))
###理所当然的普通做法1:
ggplot()+
geom_polygon(data=china_data,aes(x=long,y=lat,group=group),fill="white",colour="grey60")+
geom_segment(data=city_data,aes(x=Start_long,y=Start_lat,xend=End_long,yend=End_lat,size=zhibiao1),colour="black")+
coord_map("polyconic") +
scale_size_area(max_size=2)+
theme_void()
###最合适的做法1:图形分面:
ggplot()+
geom_polygon(data=china_data,aes(x=long,y=lat,group=group),fill="white",colour="grey60")+
geom_segment(data=city_data,aes(x=Start_long,y=Start_lat,xend=End_long,yend=End_lat),colour="black")+
geom_point(data =city_data,aes(x=End_long,y=End_lat,size=zhibiao1),shape=21,fill="#8E0F2E",colour="black",alpha=0.4)+
scale_size_area(max_size=6)+
coord_map("polyconic") +
facet_wrap(~Start,nrow = 2)+
theme_void()
###最合适的做法2:
Shiny动态交互图:
city_list<-list("北京"="北京","上海"="上海","重庆"="重庆","天津"="天津","武汉"="武汉","南京"="南京","广州"="广州","沈阳"="沈阳","西安"="西安","郑州"="郑州")
ui <-shinyUI(fluidPage(
theme=shinytheme("cerulean"),
titlePanel("Population Structure Data"),
sidebarLayout(
sidebarPanel(
radioButtons("var1","City",city_list,inline=FALSE),
selectInput("var2","Value",c("zhibiao1"="zhibiao1","zhibiao2"="zhibiao2","zhibiao3"="zhibiao3"),selected="zhibiao1")
),
mainPanel(h2("Trade Stream"),plotOutput("distPlot"))
)
))
server<-shinyServer(function(input,output){
output$distPlot <- renderPlot({
mydata=filter(city_data%>%filter(Start==input$var1))
argu<-switch(input$var2,zhibiao1=mydata$zhibiao1,zhibiao2=mydata$zhibiao2,zhibiao3=mydata$zhibiao3)
ggplot(mydata)+
geom_polygon(data=china_data,aes(x=long,y=lat,group=group),fill="white",colour="grey60")+
geom_segment(aes(x=Start_long,y=Start_lat,xend=End_long,yend=End_lat),colour="black")+
geom_point(aes(x=End_long,y=End_lat,size=argu),shape=21,fill="#8E0F2E",colour="black",alpha=0.4)+
scale_size_area(max_size=6)+
coord_map("polyconic") +
theme_void()
})
})
shinyApp(ui=ui,server=server)
R语言(ggplot2入门)可视化在商务场景中的应用,已经有200+小伙伴加入了!
感兴趣的可点击阅读原文报名参加哦,满满的干货!