--原文链接--
image.png image.png上图中,可以看出不同类别在这几年中的高低起伏变化,红色的依然为2021年和2014年比下降的,蓝色为上升的。
library(ggplot2)
library(reshape2)
library(ggalt)
data <- read.table("clipboard",header=T,sep='\t')
data$class <- ifelse((data$X2021 - data$X2014) < 0, "red", "green")
colnames(data) <- c("name","2014","2015","2016","2017","2018","2019","2020","2021","class")
data1 <- melt(data,c("name","class"))
left_label <- paste(data$name, round(data$`2014`),sep=",")
right_label <- paste(data$name, round(data$`2021`),sep=",")
ggplot()+
geom_xspline(data=data1,aes(x=variable,y=value,group=name,color=class))+
geom_vline(xintercept=2, linetype="solid", size=.1) +
geom_vline(xintercept=9, linetype="solid", size=.1) +
geom_point(aes(x=2, y=data$`2014`), size=3,shape=21,fill="grey80",color="black") +
geom_point(aes(x=9, y=data$`2021`), size=3,shape=21,fill="grey80",color="black") +
scale_color_manual(labels = c("Up", "Down"), values = c("green"="blue","red"="red"))+
geom_text(data=data,label=left_label, y=data$`2014`, x=rep(2, 15), hjust=1.1, size=3.5)+
geom_text(data=data,label=right_label, y=data$`2021`, x=rep(9, 15), hjust=-0.1, size=3.5)+
geom_text(data=data,label="2014", x=2, y=1.08*(max(data$`2014`, data$`2021`)), hjust=1.2, size=5)+
geom_text(data=data,label="2021", x=9, y=1.08*(max(data$`2014`, data$`2021`)), hjust=-0.1, size=5)+
ylim(0,4000)+scale_x_discrete(limits=c("2013","2014","2015","2016","2017","2018","2019","2020","2021","2022"))+
theme_void()
精彩推荐:
R语言绘制散点图geom_point
R语言添加拟合曲线geom_smooth
R语言箱线图boxplot
R语言线图geom_line
![image.png](https://upload-images.jianshu.io/upload_images/27049
网友评论