gpt4 book ai didi

r - ggplot : bizarre issue adding labels in time-series data

转载 作者:行者123 更新时间:2023-12-04 04:13:25 26 4
gpt4 key购买 nike

因此,我在处理州和地方的面板数据时发现了一个绘制时间序列的奇怪问题。我试图用浅灰色单独绘制每个状态的数据,使用特定颜色突出显示关键状态,并在绘图末尾为我突出显示的状态添加彩色标签。我还想为各州的平均值添加一条线。出于某种原因,所讨论变量的缩放会导致标签脱落。

我在下面生成了一些笨重的数据来说明问题。出于某种原因,平均值的标签与一些变量发生了困惑。在这方面的任何帮助都会非常有用。我只是好奇为什么代码对一个变量而不是另一个变量工作得很好。否则两组代码没有区别。


library(tidyverse)

#Creating state labels
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+10
population_c<-population_a+20
population_d<-population_a+30
population<-c(population_a, population_b, population_c, population_d)


#Consolidating the data
mydata<-data.frame(state, year, population)

mydata$lnpop<-log(mydata$population)

#PLOTTING TIME-SERIES FOR EACH STATE

#THIS WORKS:

ggplot(data=mydata, aes(year, lnpop)) +
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")) +
xlab("Year")+
ylab("Population (Logged)")

#BUT THIS DOES NOT:

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")) +
xlab("Year")+
ylab("Population")

This works

--

enter image description here

编辑:将图中的线条隔开一点。

最佳答案

使用 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")

reprex package 创建于 2020-04-16 (v0.3.0)

或在 stat_summary() 中显式设置参数 orientation = x

This geom treats each axis differently and, thus, can thus have two orientations. Often the orientation is easy to deduce from a combination of the given mappings and the types of positional scales in use. Thus, ggplot2 will by default try to guess which orientation the layer should have. Under rare circumstances, the orientation is ambiguous and guessing may fail. In that case the orientation can be specified directly using the orientation parameter, which can be either "x" or "y". The value gives the axis that the geom should run along, "x" being the default orientation you would expect for the 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)")

reprex package 创建于 2020-04-16 (v0.3.0)

关于r - ggplot : bizarre issue adding labels in time-series data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com