redwyn
redwyn

Reputation: 23

actual graph missing without error message

I just started trying to visualize my data by using the ggplot2 package in R and finally got to a point, where almost everything looks fine. Meaning, the axis are labeled correctly, having the right tick-marks etc.

The problem now is that although I receive no error messages when running the code below, the actual graph inside the plot is not showing up. That makes it kind of hard for me to figure out, what went wrong. Any suggestions on why this is (not) happening would be appreciated very much.

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

The vector of dates, used as tick-marks. A mark every 2nd month beginning with the earliest date in the data until a bit after the last 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 * "]")) + 
    scale_x_date(breaks=(time), limits=c(min(time), max(time)), labels=format.Date(time, "%b %Y"))

Everything runs as expected until I add the last line. Without it the x-axis is too cluttered and the dates are in an undesirable format. Well with it, the following image is generated (unfortunately I am not allowed to post images yet, the links should work though):

https://i.sstatic.net/I6rRY.png

The not very presentable way the plot looks like without the modified x-axis.

https://i.sstatic.net/WidpZ.png

Kind regards!

EDIT: The output of dput(sums)

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))

Kind regards!

Upvotes: 2

Views: 131

Answers (1)

Dason
Dason

Reputation: 61913

The problem is that "date" in sums is currently a factor. Convert it to a Date type. You might not have to muck around with defining "times" since I think you're only doing that so the axis doesn't look so cluttered. Having date as an actual date type should clean up the labels a little bit.

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 * "]"))

Upvotes: 4

Related Questions