绘制分组散点图(克里夫兰点图)

时间:2022-07-25
本文章向大家介绍绘制分组散点图(克里夫兰点图),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1读取数据

mydata<-read.csv("/home/shijm/Rlearning/Beautiful-Visualization-with-R-master/第3章_类别比较型图表/DotPlots_Data.csv",sep=",",na.strings="NA",stringsAsFactors=FALSE) 
> mydata<-melt(mydata,id.vars='City')
 mydata
            City variable   value
1       Acapulco   Female 2565.51
2     Bellingham   Female  453.36
3  Beverly Hills   Female 5050.46
4      Bremerton   Female 5269.89
5        Camacho   Female 3643.30
6    Guadalajara   Female  290.99
7        Hidalgo   Female 7361.04
8    Los Angeles   Female 6014.64
9         Merida   Female 4770.14

2绘制分组散点图

> ggplot(mydata, aes(value,City)) +                       
+   geom_line(aes(group = City)) +                
+   geom_point(aes(shape=variable,color=variable),size=3)+  #这里指定了shape和color,所以后面可以用scale_color_manual和shape_manual函数,否则用默认的
+   scale_color_manual(values = c('blue', 'yellow')) +
+   scale_shape_manual(values = c(15, 16)) +
+                           
+   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)
+   )

如果不指定shape和color

> ggplot(mydata, aes(value,City)) +                       #????????shape=variable??
+   geom_line(aes(group = City)) +                                                  #????????ʶgeom_hline
+   geom_point(aes(shape=variable,color=variable),size=3)+
+   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)
+   )