0

I have a plot that shows percentage of improvement over time. This plot has some -inf values because there has been a decrease from the initial value of 0. I'd like to keep these values in the plot if possible because they show there is a decrease.

I've tried labeling:

... + 
geom_text(data=subset(symptom_data, is.infinite(improvement) == TRUE),
          aes(months, improvement, label="divide by 0"))

I've tried a few different conditions other than is.infinite(improvement) == TRUE, but I always get the same error:

Error: Aesthetics must be either length 1 or the same as the data (1): x, y, colour

Oddly enough It will label the -inf values if I include other values in the condition, such as improvement < 5.

Is there any way to label only inf and -inf?

OTStats
  • 1,820
  • 1
  • 13
  • 22
  • 4
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. Otherwise, it's difficult to help seeing neither data nor output – camille Aug 19 '19 at 18:18
  • Take a look at naniar package too. Pretty cool way to visualize data https://github.com/njtierney/naniar – Tung Aug 19 '19 at 19:51

1 Answers1

2

One of the neat things about ggplot is that code inside aesthetic mappings get evaluated in the context of the data you provide. As such, you can do stuff like the following:

df <- iris
# Sprinkle some infinites around
df$Sepal.Width[sample(150, 10)] <- -Inf
df$Sepal.Length[sample(150, 10)] <- Inf

ggplot(df, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(
    colour = ifelse(!is.finite(Sepal.Width) | !is.finite(Sepal.Length), 
                    "Infinite point", as.character(Species))
  ))

enter image description here

The only downside is that you probably would have to find a prettier title for the colour legend.

teunbrand
  • 33,645
  • 4
  • 37
  • 63