1

I am no expert in R, but I have used ggplot2 many times and never had any problems. Still, this time I am not able to plot lines in my graph and I have no idea why (it should be something really simple though).

For instance for:

   def.percent    period
1    5.0657339 1984-1985
2    3.9164528 1985-1986
3    -1.756613 1986-1987
4    2.8184863 1987-1988
5    -2.606311 1988-1989

I have to code:

ggplot(plot.tab, aes(x=period, y=def.percent)) + geom_line() + geom_point() + ggtitle("Deforestation rates within Properties")

BUt when I run it, it just plots the points without a line. It also gives me this message:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

Its not really an error but I cannot figure it out how to plot the lines... Any ideas?

TWest
  • 765
  • 2
  • 6
  • 27
  • Following on that, I added a new column with "posite" and "negative" labels for the "def.percent", like: plot.tab$valence <- NA /// plot.tab$valence[plot.tab$def.percent >= 0] <- "pos" /// plot.tab$valence[plot.tab$def.percent < 0] <- "neg" /// And then I tried to use: geom_area(aes(fill=valence)) + /// to get areas under the line colored but I keep getting this error: "Aesthetics can not vary with a ribbon" Still, when I try this with other datasets it works... What is problem here? – TWest Feb 25 '15 at 21:06
  • Does this answer your question? [ggplot2 line chart gives "geom\_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"](https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation) – tjebo Jan 23 '21 at 14:36

1 Answers1

1

Your x axis (period) is a factor rather than numeric, so it doesn't connect them. You can fix this by setting group = 1 in the aesthetics, which tells ggplot2 to group them all together into a single line:

ggplot(plot.tab, aes(x = period, y = def.percent, group = 1)) +
    geom_line() +
    geom_point() +
    ggtitle("Deforestation rates within Properties")
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Following on that, I added a new column with "posite" and "negative" labels for the "def.percent", like: plot.tab$valence <- NA /// plot.tab$valence[plot.tab$def.percent >= 0] <- "pos" /// plot.tab$valence[plot.tab$def.percent < 0] <- "neg" /// And then I tried to use: geom_area(aes(fill=valence)) + /// to get areas under the line colored but I keep getting this error: "Aesthetics can not vary with a ribbon" Still, when I try this with other datasets it works... What is problem here? – TWest Feb 25 '15 at 21:01