1

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

有没有一种方法可以将xy plot() 中的xxand转换为命令,以便我可以在段命令之后设置和添加以便更好地控制它?yypoints()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 轴的范围总是不同的。我怎么能把它合并到我的代码中?

4

2 回答 2

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()
}
于 2014-09-22T12:51:22.773 回答
1

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)

在此处输入图像描述

于 2014-09-22T16:46:50.673 回答