image
alpha

代码写的好,bug改到老

R语言绘制克利夫兰点图(棒棒糖图、哑铃图等)

alpha    2019-07-03 10:43

克利夫兰点图系列 [1] 包含:棒棒糖图(lollipop chart),克利夫兰点图(Cleveland’s dot plot),哑铃图(dumbbell plot)。ggplot2-exercising-with-ggalt-dumbbells [2] 给出了一个例子,但是运行之后效果并不是很好的感觉。
 

用Excel也可以绘制哑铃图,但是操作比较麻烦[3]。
R语言示例代码:

library(ggplot2)
library(reshape2)

#-------------------------------- (a)棒棒糖图 ----------------------------------------------

mydata<-read.csv("./figure/data/dot.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")
  )

#-------------------------------- (b) 克利夫兰点图  ----------------------------------------------

mydata<-read.csv("./figure/data/dot.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")
  )

#----------------------------------(c) 哑铃图------------------------
mydata<-read.csv("./figure/data/dot.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)
  )
数据格式:
下载地址:dot.csv
City,Female,Male
Acapulco,2565.51,2595.8
Bellingham,453.36,539.27
Beverly Hills,5050.46,5269.71
Bremerton,5269.89,5705
Camacho,3643.3,2154.15
Guadalajara,290.99,232.33
Hidalgo,7361.04,3951.73
Los Angeles,6014.64,6281.53
Merida,4770.14,3970.31
Mexico City,1255.17,1233.14
Orizaba,3531.85,2713.37
Portland,5612.43,6314.17
Salem,9416.67,8795.17
San Andres,3458.54,4747.51
San Diego,5800.65,5569.12
San Francisco,392.54,351.76
Seattle,6561.9,5590.49
Spokane,5835.68,5398.36
Tacoma,8467.8,8363.35
Vancouver,3973.95,4691.05
Victoria,1151.91,1250.12
Walla Walla,488.42,676.18
Yakima,2158.89,2911.08

哑铃图

对哑铃图进行一下翻转,x-y轴对调,效果如下: 

R语言安装教程可参考:https://blog.csdn.net/arlene_mzt/article/details/81318213

参考资料:
  1. 张杰,R语言数据可视化之美:专业图表绘制指南, (北京)电子工业出版社,2019,100-101.
  2. ggplot2-exercising-with-ggalt-dumbbells. https://rud.is/b/2016/04/17/ggplot2-exercising-with-ggalt-dumbbells/
  3. EXCEL哑铃图棒棒糖图的思考.https://baijiahao.baidu.com/s?id=1629071947394897321&wfr=spider&for=pc
Last Modified: 2019-07-16 17:26
Views: 7.4K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment