【问题标题】:R: fixed x axis scaling and converting plot() xy arguments to points()R:固定 x 轴缩放并将 plot() xy 参数转换为 points()
【发布时间】:2014-11-14 07:24:51
【问题描述】:

我有一个 data.frame xy,我在下面的代码中绘制它。

有没有办法可以将 xy plot() 中的 xx 和 yy 转换为 points() 命令,以便我可以设置 type='n' 并在段命令之后按顺序添加 points()更好地控制它?

xy <- data.frame(NAME=c("NAME1","NAME1","NAME1","NAME2","NAME2","NAME2"),ID=c(87,87,87,199,199,199), X_START_YEAR=c(1984,1986,1984,1899,1909,1924),Y_START_VALUE=c(75,25,-90,-8,-55,-10),X_END_YEAR=c(1986,1994,1999,1909,1924,1927), Y_END_VALUE=c(20,50,-15,-70,-80,-100))
xy
  NAME  ID X_START_YEAR Y_START_VALUE X_END_YEAR Y_END_VALUE
1 NAME1  87         1984            75       1986          20
2 NAME1  87         1986            25       1994          50
3 NAME1  87         1984           -90       1999         -15
4 NAME2 199         1899            -8       1909         -70
5 NAME2 199         1909           -55       1924         -80
6 NAME2 199         1924           -10       1927        -100   

ind <- split(xy,xy$ID)

for (x in ind){
  xx = unlist(x[,grep('X_',colnames(x))])
  yy = unlist(x[,grep('Y_',colnames(x))])    
    fname <- paste0(x[1, 'ID'], '.png')
    png(fname, width=1679, height=1165, res=150)
    par(mar=c(6,8,6,5))
    plot(xx, 
         yy,
         main=unique(x[,1]),
         xlab="Time [Years]",
         ylab="Value [m]")
    axis(1, at = seq(1000, 2050, 5), cex.axis=1, labels=FALSE, tcl=-0.3)
    axis(2, at = seq(-100000, 100000, 500), cex.axis=1, labels=FALSE, tcl=-0.3)
    x <- x[,-1]
    segments(x[,2],x[,3],x[,4],x[,5],lwd=2)
    dev.off()
  }

如果可能的话,如果 x 轴可以处于固定范围(例如从 1940 年到 2014 年)并且如果存在 1940 年之前的值,那么 x 轴应该是自动的。 y 轴的范围总是不同的。我怎样才能将它合并到我的代码中?

【问题讨论】:

    标签: r plot dataframe points segments


    【解决方案1】:
    xy <- data.frame(NAME = c("NAME1", "NAME1", "NAME1", "NAME2", "NAME2", "NAME2"),
                     ID = c(87, 87, 87, 199, 199, 199), 
                     X_START_YEAR = c(1984, 1986, 1984, 1899, 1909, 1924),
                     Y_START_VALUE = c(75, 25, -90, -8, -55, -10),
                     X_END_YEAR = c(1986, 1994, 1999, 1909, 1924, 1927), 
                     Y_END_VALUE = c(20, 50, -15, -70, -80, -100))
    xy
    
    ind <- split(xy, xy$ID)
    
    for (i in ind){
    
      xx = unlist(i[, grep('X_', colnames(i))])
    
      yy = unlist(i[, grep('Y_', colnames(i))])    
    
      fname <- paste0(i[1, 'ID'], '.png')
    
      png(fname, width = 1679, height = 1165, res = 150)
    
      # test for xx smaller than 1940
      if(any(xx < 1940)) {
    
        my_x_lim <- c(min(xx), max(xx))
    
      } else {
    
        my_x_lim <- c(1940, 2014)}
    
      # plot the data using pch at your choice and color them as you like
      # plot your x limits
      par(mar = c(6, 8, 6, 5))
    
      plot(xx, 
           yy,
           main = unique(i[, 1]),
           xlab = "Time [Years]",
           ylab = "Value [m]",
           pch = 21, col = "black",
           xlim = my_x_lim)
    
      axis(2, at = seq(-100000, 100000, 500), cex.axis = 1, labels = FALSE, tcl = -0.3)
    
      i <- i[, -1]
    
      segments(i[, 2], i[, 3], i[, 4], i[, 5],lwd = 2)
    
      points(xx, yy, pch = 21, col = "black")
    
      dev.off()
    }
    

    【讨论】:

    • 感谢您的回答!这是做我想做的事,完美!一个小细节我遇到了问题,也许你有一个解决方案:我想用pch=21 和bg='white' 绘制点,这会产生一个带有黑色边距的白色圆点。然而,现在首先调用点的绘图,然后在这些点上绘制线段。这看起来不如将带有黑色边距的白点绘制在线段上时那么好。这就是为什么我最初使用points() 寻找解决方案的原因,以便我可以定义要绘制的特征的顺序...
    • 谢谢!!没有看到它是如此容易合并。你帮了我很多!
    【解决方案2】:

    ggplot2 库有一个分段工具,它使得绘制此类数据非常紧凑,因此既易于维护又便于将来参考。在这里。

    它基本上是七行,但可以进一步压缩。注意修改 x 轴的额外灵感。

    dat = xy
    
    # Adapted from the other solution
    if(any(dat$X_START_YEAR < 1940)) {  
      my_x_lim <- c(min(dat$X_START_YEAR), max(dat$X_END_YEAR))
      } else {
        my_x_lim <- c(1940, 2014)
      }
    
    # plot based on Hadley Wickham's ggplot2
    library(ggplot2)
    p = ggplot(dat)
    p = p + geom_segment(aes(x = X_START_YEAR, xend = X_END_YEAR, y = Y_START_VALUE, yend = Y_END_VALUE))
    # p = p + facet_wrap(~ID)
    # proposal to uncomment if y-axis should not be equivalent over multiple facets (makes the plot harder to read, though):
    p = p + facet_wrap(~ID, scales = "free")
    # Alternatively, only let the x-axis scale vary freely (this gets you ride of the initial procedure to determine the x-axis limits)
    # p = p + facet_wrap(~ID, scales = "free_x")
    p = p + xlab("Time [Years]") + ylab("Value [m]")
    p = p + xlim(my_x_lim)
    print(p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      相关资源
      最近更新 更多