实际图表缺失没有错误消息

时间:2012-06-05 22:18:51

标签: r ggplot2

我刚开始尝试使用R中的ggplot2包来可视化我的数据,最后得到了一个点,几乎所有东西看起来都很好。意思是,轴标记正确,有正确的刻度标记等。

现在的问题是,虽然在运行下面的代码时没有收到任何错误消息,但是图中的实际图形没有显示出来。这让我很难弄明白,出了什么问题。关于为什么(不)发生的任何建议都将非常感激。

time <- seq(as.Date("2010-04-01"), as.Date("2012-02-01"), by = "2 months")

日期矢量,用作刻度线。每隔2个月开始一次标记,从数据中的最早日期开始,直到最后一个日期之后。

qplot(date, sums/1000, data=sums[(sums$lake=="Gollinsee") & (sums$maize=="no maize"),] ,color=location, group=location, main="Gollinsee without maize") +
    geom_line(size=0.7) + geom_point(size=3) +
    labs(x="Date", y=expression("Total C  [" * g %.% m^-3 * "]")) + 
    scale_x_date(breaks=(time), limits=c(min(time), max(time)), labels=format.Date(time, "%b %Y"))

一切都按预期运行,直到我添加最后一行。没有它,x轴太杂乱,日期是不合需要的格式。好吧,生成以下图像(不幸的是我不允许发布图像,链接应该可以工作):

http://i.stack.imgur.com/I6rRY.png

在没有修改的x轴的情况下,情节看起来不太明显。

http://i.stack.imgur.com/WidpZ.png

亲切的问候!

修改 输出的输出(总和)

structure(list(date = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 11L, 11L, 19L, 10L, 10L, 
12L, 12L, 14L, 14L, 16L, 16L, 19L, 15L, 20L, 20L, 13L, 13L, 17L, 
17L, 18L, 20L, 20L, 22L, 22L, 18L, 4L, 21L, 25L, 28L, 25L, 25L, 
23L, 23L, 23L, 23L, 28L, 24L, 24L, 24L, 24L, 22L, 22L, 22L, 22L, 
21L, 25L, 35L, 35L, 37L, 37L, 35L, 35L, 37L, 37L, 39L, 39L, 39L, 
39L, 36L, 28L, 33L, 33L, 33L, 33L, 31L, 31L, 31L, 31L, 29L, 29L, 
29L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L, 
32L, 32L, 32L, 32L, 34L, 34L, 34L, 34L, 38L, 38L, 38L, 38L, 36L, 
36L), .Label = c("2010-04-06", "2010-04-08", "2010-05-04", "2010-05-06", 
"2010-05-19", "2010-06-01", "2010-06-03", "2010-06-29", "2010-06-30", 
"2010-07-27", "2010-07-29", "2010-08-24", "2010-08-26", "2010-09-21", 
"2010-09-23", "2010-10-19", "2010-10-21", "2010-11-16", "2010-11-18", 
"2011-01-05", "2011-02-24", "2011-03-22", "2011-04-05", "2011-04-07", 
"2011-05-03", "2011-05-05", "2011-05-31", "2011-06-01", "2011-06-28", 
"2011-07-01", "2011-07-26", "2011-07-28", "2011-08-23", "2011-08-25", 
"2011-09-20", "2011-09-22", "2011-10-17", "2011-10-20", "2012-01-10"
), class = "factor"), lake = structure(c(1L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Gollinsee", 
"Schulzensee"), class = "factor"), location = structure(c(2L, 
1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L
), .Label = c("littoral", "pelagic"), class = "factor"), maize = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 
2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 
2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 
2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L
), .Label = c("maize", "no maize"), class = "factor"), sums = c(388.7, 
254.5, 320.4, 373.3, 224.1, 182.4, 245.5, 258.7, 47.7, 60.5, 
58.3, 103.7, 311.9, 207.7, 283.9, 308.3, 237.9, 165.4, 556.4, 
1090.4, 1792.6, 1350.5, 932.4, 526.5, 1157.8, 2343.7, 4570.9, 
393.2, 315.5, 351, 1008, 504.8, 289.9, 113.4, 117.7, 707.9, 193.4, 
122.2, 828.2, 1106.4, 837.8, 148, 777.6, 1824.6, 388, 1658.6, 
636.3, 1434.7, 770.8, 1776.4, 705.9, 1634.5, 1237.1, 1882.5, 
1073.8, 2283, 1030.7, 273, 194.6, 788.6, 1034.9, 420.7, 404.6, 
295.9, 332, 229.3, 381, 304.2, 780.2, 227.3, 50.6, 509.3, 209.7, 
675.8, 691.9, 651.7, 635.1, 560.8, 602.2, 1243.1, 733.8, 669.1, 
574.5, 379.6, 1405.3, 513.6, 612.4, 1396.4, 256.7, 486.8, 521.3, 
55.5, 48.2, 181.7, 281.3, 322.7, 478.1, 208.4, 173.8, 599.2, 
262.9, 760.7, 365.5, 746.6, 790.3, 581.7, 388.9, 826.4, 132.6, 
167.5, 817.6, 425.1, 582.2)), .Names = c("date", "lake", "location", 
"maize", "sums"), class = "data.frame", row.names = c(NA, -113L))

亲切的问候!

1 个答案:

答案 0 :(得分:4)

问题是总和中的“日期”目前是一个因素。将其转换为日期类型。您可能不必定义“时间”,因为我认为您只是这样做,所以轴看起来不那么杂乱。将日期作为实际日期类型应该稍微清理标签。

sums$date <- as.Date(sums$date)

qplot(date, 
      sums/1000,
      data=sums[(sums$lake=="Gollinsee") & (sums$maize=="no maize"),],
      color=location, 
      group=location, 
      main="Gollinsee without maize") +
geom_line(size=0.7) + 
geom_point(size=3) +
labs(x="Date", y=expression("Total C  [" * g %.% m^-3 * "]"))