5

This seems to be a duplicate of R - ggplot geom_step error, which has not been answered yet.

When the data to plot consists of one point only, geom_step throws an error while geom_line gives a warning only:

library(ggplot2)
data <- data.frame(x = 1, y = 2)

# works
ggplot(data = data, aes(x = x, y = y)) + geom_line()

# does not work
ggplot(data = data, aes(x = x, y = y)) + geom_step()

geom_step gives the error message: invalid line type. Is this a bug or the desired behaviour? With this behaviour geom_step looses a part of ggplot's flexibility since the single point case needs to be dealt with manually. One brute force solution is to manually check for number of points to be plotted and only add the step-layer if there are at least two point. But surely there must be a more elegant workaround?!

packageVersion("ggplot2")
[1] ‘1.0.1’
Community
  • 1
  • 1
Jens
  • 133
  • 6
  • but what is your desired output? for me the `geom_line` plot is empty – mts Jul 01 '15 at 17:21
  • 1
    seems to be a known issue https://github.com/hadley/ggplot2/issues/719 – user20650 Jul 01 '15 at 19:13
  • @mts The desired output would be an empty plot. Imagine you have a shiny app visualizing some data for arbitrary users. If the inputs are selected in a way, that the resulting data comprises one data point only, the app throws an error (due to geom_step), which needs to be dealt with. geom_line smoothly shows nothing, which is in my eyes, the expected result. – Jens Jul 01 '15 at 20:12
  • Indeed, as user20650 indicated, the issue seems to be known since 2012 and the desired functionality has not been implemented, yet. But what would be the most elegant way to deal with the error? tryCatch seems not to be an option, since the error is thrown when the plot is printed, i.e. after the layers have been build. Checking the data if single points are present before the plot is created?! – Jens Jul 01 '15 at 20:26
  • Both `geom_line` & `geom_step` now returns the same empty plot + warning, instead of error message. Propose to close since the issue is no longer reproducible (my current package version is 3.2.1) – Z.Lin Oct 26 '19 at 05:40

0 Answers0