另一种解决方法是使用
annotate()
library(ggplot2)
library(dplyr)
state<-c(rep("A",21), rep("B",21), rep("C",21), rep("D",21))
#Creating years for each state
year<-rep(2000:2020, 4)
#Generating each state's population
population_a<-5000:5020
population_b<-population_a+2
population_c<-population_a+3
population_d<-population_a+5
population<-c(population_a, population_b, population_c, population_d)
#Consolidating the data
mydata<-data.frame(state, year, population)
sub_dat <- subset(mydata, year==max(year))
ggplot(data=mydata, aes(year, population)) +
geom_line(aes(group=state), colour="gray")+
geom_text(data=mydata %>% group_by(state) %>%
arrange(desc(year)) %>%
slice(1) %>%
filter(state=="A"),
aes(x = year+0.3, label=state), colour="purple", hjust=0)+
geom_text(data=mydata %>% group_by(state) %>%
arrange(desc(year)) %>%
slice(1) %>%
filter(state=="B"),
aes(x = year+0.3, label=state), colour="red",hjust=0)+
geom_text(data=mydata %>% group_by(state) %>%
arrange(desc(year)) %>%
slice(1) %>%
filter(state=="D"),
aes(x = year+0.3, label=state), colour="blue",hjust=0)+
guides(colour=FALSE) +
expand_limits(x = max(mydata$year) + 0.3)+
geom_line(data=subset(mydata, state == "A"), colour="purple")+
geom_line(data=subset(mydata, state == "B"), colour="red")+
geom_line(data=subset(mydata, state == "D"), colour="blue")+
stat_summary(fun = mean, geom = "line") +
annotate("text",
x = max(sub_dat$year) + 0.05, y = mean(sub_dat$population),
label = "AVG", hjust = 0) +
xlab("Year")+
ylab("Population")
创建于2020年4月16日
reprex package
(v0.3.0)
或设置参数
orientation = x
在里面
stat_summary()
明确地
该几何模型对每个轴的处理方式不同,因此可以有两个方向。通常,从给定的映射和使用的位置尺度类型的组合中很容易推断出方向。因此,ggplot2默认情况下会尝试猜测层应该具有哪个方向。在极少数情况下,方向是模糊的,猜测可能会失败。在这种情况下,可以直接使用方向参数指定方向,该参数可以是“x”或“y”。该值给出了geom应该沿着的轴,“x”是geom的默认方向。
ggplot(data=mydata, aes(year, population)) +
geom_line(aes(group=state), colour="gray")+
geom_text(data=mydata %>% group_by(state) %>%
arrange(desc(year)) %>%
slice(1) %>%
filter(state=="A"),
aes(x = year+0.3, label=state), colour="purple", hjust=0)+
geom_text(data=mydata %>% group_by(state) %>%
arrange(desc(year)) %>%
slice(1) %>%
filter(state=="B"),
aes(x = year+0.3, label=state), colour="red",hjust=0)+
geom_text(data=mydata %>% group_by(state) %>%
arrange(desc(year)) %>%
slice(1) %>%
filter(state=="D"),
aes(x = year+0.3, label=state), colour="blue",hjust=0)+
guides(colour=FALSE) +
expand_limits(x = max(mydata$year) + 0.3)+
geom_line(data=subset(mydata, state == "A"), colour="purple")+
geom_line(data=subset(mydata, state == "B"), colour="red")+
geom_line(data=subset(mydata, state == "D"), colour="blue")+
stat_summary(fun = mean, geom = "line") +
stat_summary(data=subset(mydata, year==max(year)), fun = mean, geom = "text", show.legend = FALSE, hjust=0, aes(x=year+0.05,label="AVG"), orientation = "x") +
xlab("Year")+
ylab("Population (Logged)")
创建于2020年4月16日
reprex包
(v0.3.0)