【问题标题】:ggplot line graph with NA values具有NA值的ggplot折线图
【发布时间】:2015-02-27 21:57:32
【问题描述】:

我在 ggplot 尝试在同一图表上绘制 2 个不完整的时间序列时遇到问题,其中 y 数据在 x 轴(年份)上没有相同的值 - 因此某些年份存在 NA:

test<-structure(list(YEAR = c(1937, 1938, 1942, 1943, 1947, 1948, 1952, 
1953, 1957, 1958, 1962, 1963, 1967, 1968, 1972, 1973, 1977, 1978, 
1982, 1983, 1986.5, 1987, 1993.5), A1 = c(NA, 24, NA, 32, 32, 
NA, 34, NA, NA, 18, 12, NA, 10, NA, 11, NA, 15, NA, 24, NA, NA, 
25, 26), A2 = c(40, NA, 38, NA, 25, NA, 26, NA, 20, NA, 17, 
17, 17, NA, 16, 18, 21, 18, 17, 25, NA, NA, 26)), .Names = c("YEAR", "A1", 
"A2"), row.names = c(NA, -23L), class = "data.frame")

我尝试的以下代码输出杂乱无章:

ggplot(test, aes(x=YEAR)) + 
  geom_line(aes(y = A1), size=0.43, colour="red") +  
  geom_line(aes(y = A2), size=0.43, colour="green") +
  xlab("Year") + ylab("Percent") +
  scale_x_continuous(limits=c(1935, 1995), breaks = seq(1935, 1995, 5),
                     expand = c(0, 0)) + 
  scale_y_continuous(limits=c(0,50), breaks=seq(0, 50, 10), expand = c(0, 0))

我该如何解决这个问题?

【问题讨论】:

    标签: r ggplot2 na


    【解决方案1】:

    我首选的解决方案是将其重塑为长格式。那么你只需要1个geom_line电话。特别是如果你有很多系列,那就更整洁了。结果与 LyzanderR 的第二张图表相同。

    library(ggplot2)
    library(reshape2)
    
    test2 <- melt(test, id.var='YEAR')
    test2 <- na.omit(test2)
    
    ggplot(test2, aes(x=YEAR, y=value, color=variable)) + 
      geom_line() +
      scale_color_manual(values=c('red', 'green')) +
    
      xlab("Year") + ylab("Percent") +
      scale_x_continuous(limits=c(1935, 1995), breaks = seq(1935, 1995, 5),
                         expand = c(0, 0)) + 
      scale_y_continuous(limits=c(0,50), breaks=seq(0, 50, 10), expand = c(0, 0))
    

    您可能会考虑在该行之外添加一个geom_point() 调用,以便清楚哪些点是实际值,哪些点是缺失的。长格式的另一个优点是每个额外的 geom 只需要 1 次调用,而不是每个系列 1 次。

    【讨论】:

    • 谢谢,我试过melt,但错过了na.omit。如何更改每条线的线型?
    • 与上面的颜色更改方式相同。 linetype=variable 在 aes 调用中,然后(可选)scale_linetype_manual 如果你想指定什么线型
    【解决方案2】:

    您可以使用na.omit 删除它们:

    library(ggplot2)
    #use na.omit below
    ggplot(na.omit(test), aes(x=YEAR)) + 
      geom_line(aes(y = A1), size=0.43, colour="red") +  
      geom_line(aes(y = A2), size=0.43, colour="green") +
      xlab("Year") + ylab("Percent") +
      scale_x_continuous(limits=c(1935, 1995), breaks = seq(1935, 1995, 5),
                         expand = c(0, 0)) + 
      scale_y_continuous(limits=c(0,50), breaks=seq(0, 50, 10), expand = c(0, 0))
    

    编辑

    使用 2 个单独的 data.frames 和 na.omit

    #test1 and test2 need to have the same column names
    test1 <- test[1:2]
    test2 <- tes[c(1,3)]
    colnames(test2) <- c('YEAR','A1')
    
    library(ggplot2)
    ggplot(NULL, aes(y = A1, x = YEAR)) + 
      geom_line(data = na.omit(test1), size=0.43, colour="red") +  
      geom_line(data = na.omit(test2), size=0.43, colour="green") +
      xlab("Year") + ylab("Percent") +
      scale_x_continuous(limits=c(1935, 1995), breaks = seq(1935, 1995, 5),
                         expand = c(0, 0)) + 
      scale_y_continuous(limits=c(0,50), breaks=seq(0, 50, 10), expand = c(0, 0))
    

    【讨论】:

    • 好的,但是为什么没有绘制1947年之前的数据呢?
    • na.omit 删除带有 NA 的行。否则你不能将它们放在同一个数据框中。
    • 好的,但是可以用 ggplot 绘制多个数据框吗?
    【解决方案3】:

    您可以通过子集数据框来删除它们:

      ggplot(test, aes(x=YEAR)) + 
      geom_line(data=subset(test, !is.na(A1)),aes(y = A1), size=0.43, colour="red") +  
      geom_line(data=subset(test, !is.na(A2)),aes(y = A2), size=0.43, colour="green") +
      xlab("Year") + ylab("Percent") +
      scale_x_continuous(limits=c(1935, 1995), breaks = seq(1935, 1995, 5),
                         expand = c(0, 0)) + 
      scale_y_continuous(limits=c(0,50), breaks=seq(0, 50, 10), expand = c(0, 0))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多