代码之家  ›  专栏  ›  技术社区  ›  sjillani

ggplot:在时间序列数据中添加标签的奇怪问题

  •  0
  • sjillani  · 技术社区  · 4 年前

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

    我在下面生成了一些笨拙的数据来证明这个问题。由于某种原因,平均值的标签在一些变量上出现了混乱。这方面的任何帮助都会非常有用。我只是好奇为什么代码对一个变量而不是另一个变量都能很好地工作。否则,这两组代码之间没有区别。

    
    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

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

    0 回复  |  直到 4 年前
        1
  •  1
  •   yang    4 年前

    另一种解决方法是使用 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)