R绘制x轴十年循环

时间:2015-10-22 16:25:43

标签: r plot axis-labels

我正在运行一个循环来从1个数据帧生成多个图。每个图都有一个可变的x轴范围(这就是我想要的),但我只想标记几十年。有没有办法在我的代码中指定它?

我查看了其他x轴标签帖子,并找到了定义标签间隔的方法,但没有找到如何为一系列值指定确切数字的方法。一个解决方案是:

> axis(1, seq=(1950, 2000, by=10)

如果我的日期在十进制年份开始和结束,这很有效,但他们通常不会。我试图将我找到的各种选项组合在一起,但是我无法获得完整的代码来运行;我不断收到错误消息(如“非数字二进制参数”)。

可重复的数据摘录:

> df6 <- structure(list(Year = c("1958", "1959", "1960", "1961", "1962", 
"1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", 
"1971", "1972", "1972", "1973", "1973", "1974", "1974", "1975", 
"1975", "1976", "1976", "1977", "1977", "1978", "1978", "1979", 
"1979", "1980", "1980", "1981", "1981", "1982", "1982", "1983", 
"1983", "1984", "1984", "1985", "1985", "1986", "1986", "1987", 
"1987", "1988", "1988", "1989", "1989", "1990", "1990", "1991", 
"1991", "1992", "1992", "1993", "1993", "1994", "1994", "1995", 
"1995", "1996", "1996", "1997", "1997", "1998", "1998", "1999", 
"1999", "2000", "2000", "2001", "2001", "2002", "2002", "2003", 
"2003", "2004", "2004", "2005", "2005", "2006", "2006", "2007", 
"2007", "2008", "2008", "2009", "2009", "2010", "2010", "2011"
), plot = c("FA5", "FA5", "FA5", "FA5", "FA5", "FA5", "FA5", 
"FA5", "FA5", "FA5", "FA5", "FA5", "FA5", "FA5", "BR8", "FA5", 
"BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", 
"FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", 
"BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", 
"FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", 
"BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", 
"FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", 
"BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", 
"FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", "BR8", "FA5", 
"BR8", "FA5", "BR8", "FA5", "FA5"), AvgRW = c(0.999, 0.671, 0.638, 
0.767, 0.839, 1.072, 1.162, 0.7215, 0.416, 0.3265, 0.464, 1.492, 
1.011, 1.0905, 0.933, 1.0755, 1.018, 1.42183333333333, 0.6415, 
1.17783333333333, 0.7495, 0.931166666666667, 0.654166666666667, 
1.761375, 0.635333333333333, 2.063875, 0.44875, 1.91758333333333, 
0.615583333333333, 0.945583333333333, 0.825857142857143, 0.60775, 
0.7095, 1.07725, 1.1536875, 1.85278571428571, 1.65311111111111, 
2.05792857142857, 1.50425, 2.6515, 1.87955, 2.25066666666667, 
2.14577272727273, 2.19533333333333, 1.61231818181818, 1.931, 
2.48027272727273, 2.31927777777778, 1.82383336363636, 2.5933, 
1.373, 3.03875, 1.36390909090909, 2.66533333333333, 1.38969441666667, 
2.70753846153846, 1.39786108333333, 2.53957692307692, 2.14262823076923, 
2.98446153846154, 2.37315478571429, 3.24507692307692, 2.37939285714286, 
3.23284615384615, 1.85764285714286, 3.59303846153846, 1.98335714285714, 
4.03557692307692, 1.34670237857143, 3.45111538461538, 0.710416664285714, 
3.47661538461538, 0.657595235714286, 2.96003846153846, 1.3541905, 
2.96257692307692, 1.81747621428571, 2.65265384615385, 1.88546428571429, 
2.53873076923077, 1.92221428571429, 2.60619230769231, 2.10236907142857, 
2.26838461538462, 1.62090476428571, 1.96623076923077, 1.61060714285714, 
2.27869230769231, 2.03883335714286, 2.00357692307692, 2.10155130769231, 
1.74626923076923, 2.61165384615385), SampDepth = c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 2L, 
3L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 6L, 6L, 6L, 7L, 6L, 7L, 6L, 8L, 
7L, 9L, 7L, 10L, 9L, 10L, 9L, 11L, 9L, 11L, 9L, 11L, 9L, 11L, 
10L, 11L, 10L, 11L, 12L, 12L, 13L, 12L, 13L, 13L, 13L, 14L, 13L, 
14L, 13L, 14L, 13L, 14L, 13L, 14L, 13L, 14L, 13L, 14L, 13L, 14L, 
13L, 14L, 13L, 14L, 13L, 14L, 13L, 14L, 13L, 14L, 13L, 14L, 13L, 
14L, 13L, 13L, 13L, 13L)), .Names = c("Year", "plot", "AvgRW", 
"SampDepth"), row.names = c(1143L, 1162L, 1181L, 1200L, 1219L, 
1238L, 1259L, 1281L, 1303L, 1325L, 1348L, 1371L, 1394L, 1417L, 
1440L, 1441L, 1464L, 1465L, 1488L, 1489L, 1512L, 1513L, 1536L, 
1537L, 1560L, 1561L, 1584L, 1585L, 1608L, 1609L, 1632L, 1633L, 
1656L, 1657L, 1680L, 1681L, 1704L, 1705L, 1728L, 1729L, 1752L, 
1753L, 1776L, 1777L, 1800L, 1801L, 1824L, 1825L, 1848L, 1849L, 
1872L, 1873L, 1896L, 1897L, 1920L, 1921L, 1944L, 1945L, 1968L, 
1969L, 1992L, 1993L, 2016L, 2017L, 2040L, 2041L, 2064L, 2065L, 
2088L, 2089L, 2112L, 2113L, 2136L, 2137L, 2160L, 2161L, 2184L, 
2185L, 2208L, 2209L, 2232L, 2233L, 2256L, 2257L, 2280L, 2281L, 
2304L, 2305L, 2328L, 2329L, 2352L, 2353L, 2375L), class = "data.frame")

当前的绘图代码(此DOES正常工作,它没有正确的x轴标签):

> for (i in df6$plot) {                               #Start loop
     #Cut out the data you are working with  
     plotting <- df6[which(df6$plot == i),]

  #Begin plotting
  png(filename = paste0("C:\\Users\\User\\Desktop\\", i, type="png")) #Saves graphs  
  par(mar=c(5.1,4.1,4.1,5.1))             #Increase right margin
  plot(plotting[,c(1,3)],xaxt="n", type="l", ylab="Avg RW (mm)", col="blue", ylim=c(0,4.5))
  abline(a=2.0, b=0, col="blue", lty=2)

  #This will tell R how to plot the polygon (sample depth):
  #It need the x values (years) ascending and descending
  #It also needs y values (AvgRW) for the above 0 values as well as the 0 values
  xx <- c(unique(plotting$Year),rev(unique(plotting$Year)))
  yy <- c(plotting$SampDepth,rep(0,length(unique(plotting$Year))))

    title(main=i,line=2.5)                  #add title
  axis(1,at=as.integer(plotting$Year),labels=plotting$Year) #bottom axis
  par(new = TRUE)                                 #overlay for secondary y
  plot(plotting[,c(1,4)],xaxt="n",yaxt="n", ylab="",type="l", col=NA, ylim=c(0,35))
  polygon(xx, yy, border="lightgray", col=rgb(190/255, 190/255, 190/255, 0.1))
  abline(a=4, b=0, col="black", lty=4)
  axis(4)                                         #add secondary y axis
  mtext("Sample Depth", side = 4, line=2)         #add secondary y label
  dev.off()
}

我尝试通过选择可被10整除的年份来调整轴标签,但此调整会导致出现“非二进制参数”错误消息:

>   axis(1,at=as.integer(plotting$Year[ which(plotting$Year %% 10 = c(1:20))]), labels=plotting$Year) #bottom axis

任何建议都会有所帮助!

1 个答案:

答案 0 :(得分:1)

采用稍微不同的使用ggplot2的方法可能会更容易。例如:

library(ggplot2)
library(gridExtra)

df6$Year <- as.numeric(df6$Year)

p1 <- ggplot(df6, aes(x = Year, y = AvgRW, group = 1)) +
  geom_line() +
  geom_hline(aes(yintercept = 2), color = "blue", linetype = 2) +
  facet_wrap(~ plot)

p2 <- ggplot(df6, aes(x = Year, y = SampDepth, group = 1)) +
  geom_line() +
  geom_hline(aes(yintercept = 4), color = "red", linetype = 2) +
  facet_wrap(~ plot) 

grid.arrange(p1, p2)

Plot

注意:如果轴的轻微错位困扰您或者是一个问题,您可以使用以下代码修改grid.arrange代码,如this SO answer中所述:

gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)