R绘图(03)——ggplot绘制标记特殊样本点的散点图
绘图数据库仍然同01篇的数据
本次的难点是多个条件下求和,即既要根据年份,也要根据地区对统计变量value进行求和。
因为我搜索的方法有限,没有找到对应的函数语句,于是干脆自己写了个循环,对每个条件下的数据以此计算,再将各个条件下的数据汇总为一个总的文件
效果图如下:
library(dplyr) var3<-c("ICEWSGOVT_bin","Year_F","GrowingSeason_Month","ADM1_EN","SPIBelow1sd") pICE<-ICE[var3] pICE$ADM1_EN<-as.factor(pICE$ADM1_EN) pICE$SPIBelow1sd<-as.numeric(pICE$SPIBelow1sd) newdata<-pICE[which( pICE$ICEWSGOVT_bin== 1& pICE$GrowingSeason_Month==1 ),] newdata2<-newdata[,c(2,4,5)] reC<-list() res<-list() for (i in 2011:2018){ temp<-newdata2[which( newdata2$Year_F==i),] iris_group<-group_by(temp,ADM1_EN) reC[[i-2010]]<-summarise(iris_group,sumnumber=sum(SPIBelow1sd)) tempY<-as.data.frame(matrix(rep(i:i),nrow = nrow(reC[[i-2010]]) , ncol = 1 ,byrow =F)) res[[i-2010]]<-cbind(tempY,reC[[i-2010]]) } resC<-rbind(res[[1]],res[[2]],res[[3]],res[[4]],res[[5]],res[[6]],res[[7]],res[[8]]) df0=resC%>%filter(ADM1_EN == "As-Sweida") df1=resC%>%filter(ADM1_EN == "Homs") p <- ggplot(resC,aes(x=V1,y=sumnumber)) + labs(x="YEAR" ,y="Number of SPI-1sd")+ geom_point( ) p <-p+theme_classic()+theme(axis.line = element_line(colour = "black"))+theme()+ guides(fill="none",color="none")+scale_x_continuous(breaks = seq(2011, 2018, by = 1)) p <-p+geom_point(data=resC[resC$ADM1_EN == c("As-Sweida"),],color="red")+geom_text(data=df0,mapping=aes(label = ADM1_EN), size = 3,col = 'red') p <-p+geom_point(data=resC[resC$ADM1_EN == c("Homs"),],color="blue")+geom_text(data=df1,mapping=aes(label = ADM1_EN), size = 3,col = 'blue') ####### Avar3<-c("acledGOVT_bin","Year_F","GrowingSeason_Month","ADM1_EN","SPIBelow1sd") ApICE<-ACL[Avar3] ApICE$ADM1_EN<-as.factor(ApICE$ADM1_EN) ApICE$SPIBelow1sd<-as.numeric(ApICE$SPIBelow1sd) Anewdata<-ApICE[which( ApICE$acledGOVT_bin== 1& ApICE$GrowingSeason_Month==1 ),] Anewdata2<-Anewdata[,c(2,4,5)] AreC<-list() Ares<-list() for (i in 2017:2019){ temp<-Anewdata2[which( Anewdata2$Year_F==i),] Airis_group<-group_by(temp,ADM1_EN) AreC[[i-2016]]<-summarise(Airis_group,sumnumber=sum(SPIBelow1sd)) tempY<-as.data.frame(matrix(rep(i:i),nrow = nrow(AreC[[i-2016]]) , ncol = 1 ,byrow =F)) Ares[[i-2016]]<-cbind(tempY,AreC[[i-2016]]) } AresC<-rbind(Ares[[1]],Ares[[2]],Ares[[3]]) Ap <- ggplot(AresC,aes(x=V1,y=sumnumber)) + labs(x="YEAR" ,y="Number of SPI-1sd")+ geom_point( ) Ap <-Ap+theme_classic()+theme(axis.line = element_line(colour = "black"))+theme()+ guides(fill="none",color="none")+scale_x_continuous(breaks = seq(2017, 2019, by = 1)) df2=AresC%>%filter(ADM1_EN == "As-Sweida") df3=AresC%>%filter(ADM1_EN == "Homs") Ap <-Ap+geom_point(data=AresC[AresC$ADM1_EN == c("As-Sweida"),],color="red")+geom_text(data=df2,mapping=aes(label = ADM1_EN), size = 3,col = 'red') Ap <-Ap+geom_point(data=AresC[AresC$ADM1_EN == c("Homs"),],color="blue")+geom_text(data=df3,mapping=aes(label = ADM1_EN), size = 3,col = 'blue') ggarrange(p, Ap, labels = c("A", "B"),ncol = 1, nrow = 2)