Skip to content

Commit

Permalink
Update 图3-5-1 坡度图.R
Browse files Browse the repository at this point in the history
  • Loading branch information
EasyChart authored Jun 3, 2019
1 parent 0f2f5e5 commit 279a1a2
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions 第3章_类别比较型图表/图3-5-1 坡度图.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

#EasyCharts团队出品,如有商用必究,
#如需使用与深入学习,请联系微信:EasyCharts
#EasyCharts团队出品,
#如有问题修正与深入学习,可联系微信:EasyCharts

library(ggplot2)
library(scales)
library(reshape)
#--------------------------------------(a)两年份对比---------------------------------------------------------------
#--------------------------------------(a)两年份对比---------------------------------------------------------------

df <- read.csv("Slopecharts_Data1.csv")
colnames(df) <- c("continent", "1952", "1957")
Expand All @@ -14,15 +14,15 @@ right_label <- paste(df$continent, round(df$`1957`),sep=", ")
df$class <- ifelse((df$`1957` - df$`1952`) < 0, "red", "green")

p <- ggplot(df) +
geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) + #连接线
geom_vline(xintercept=1, linetype="solid", size=.1) + # 1952年的垂直直线
geom_vline(xintercept=2, linetype="solid", size=.1) + # 1957年的垂直直线
geom_point(aes(x=1, y=`1952`), size=3,shape=21,fill="grey80",color="black") + # 1952年的数据点
geom_point(aes(x=2, y=`1957`), size=3,shape=21,fill="grey80",color="black") + # 1957年的数据点
geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) + #连接线
geom_vline(xintercept=1, linetype="solid", size=.1) + # 1952年的垂直直线
geom_vline(xintercept=2, linetype="solid", size=.1) + # 1957年的垂直直线
geom_point(aes(x=1, y=`1952`), size=3,shape=21,fill="grey80",color="black") + # 1952年的数据点
geom_point(aes(x=2, y=`1957`), size=3,shape=21,fill="grey80",color="black") + # 1957年的数据点
scale_color_manual(labels = c("Up", "Down"), values = c("green"="#A6D854","red"="#FC4E07")) +
xlim(.5, 2.5)

# 添加文本信息
# 添加文本信息
p <- p + geom_text(label=left_label, y=df$`1952`, x=rep(1, NROW(df)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=df$`1957`, x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="1952", x=1, y=1.02*(max(df$`1952`, df$`1957`)), hjust=1.2, size=5)
Expand All @@ -31,7 +31,7 @@ p <- p + geom_text(label="1957", x=2, y=1.02*(max(df$`1952`, df$`1957`)), hjust=
p<-p+theme_void()
p

#-------------------------------------(b)多年份对比---------------------------------------------------------------------------------
#-------------------------------------(b)多年份对比---------------------------------------------------------------------------------
library(ggalt)

df <- read.csv("Slopecharts_Data2.csv")
Expand Down Expand Up @@ -90,4 +90,4 @@ p <- p + geom_text(label="2013", x=7, y=1.02*(max(df2$value)), hjust=-0.1, size=

p<-p+theme_void()+
theme(legend.position = "none")
p
p

0 comments on commit 279a1a2

Please sign in to comment.