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)
+ )
复制