RHA
RHA

Reputation: 3862

Line with size=0 still drawn by geom_line

So I have a data.frame of the activity of certain species through time. Here's part of it:

df <- structure(list(soort_wet = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 
        2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
        2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
        2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Callophrys rubi", 
            "Coenagrion hastulatum"), class = "factor"), periode = structure(c(1L, 
                            1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 
                            9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 
                            16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 
                            22L, 23L, 23L, 24L, 24L, 25L, 25L), .Label = c("1-mrt", "10-mrt", 
                  "20-mrt", "1-apr", "10-apr", "20-apr", "1-mei", "10-mei", "20-mei", 
                  "1-jun", "10-jun", "20-jun", "1-jul", "10-jul", "20-jul", "1-aug", 
                  "10-aug", "20-aug", "1-sep", "10-sep", "20-sep", "1-okt", "10-okt", 
                  "20-okt", "30-okt"), class = "factor"), activiteit = c(NA, NA, 
                    NA, NA, NA, NA, NA, NA, 0, NA, 1, 0, 1, 1, 3, 2, 3, 3, 3, 3, 
                    3, 3, 1, 2, 1, 2, 0, 1, 0, 1, NA, 1, NA, 0, NA, NA, NA, NA, NA, 
                   NA, 0, NA, 0, NA, NA, NA, NA, NA, NA, NA)), .Names = c("soort_wet", 
                   "periode", "activiteit"), class = "data.frame", row.names = c(9L, 
                           15L, 70L, 76L, 131L, 137L, 192L, 198L, 253L, 259L, 314L, 320L, 
                           375L, 381L, 436L, 442L, 497L, 503L, 558L, 564L, 619L, 625L, 680L, 
                           686L, 741L, 747L, 802L, 808L, 863L, 869L, 924L, 930L, 985L, 991L, 
                           1046L, 1052L, 1107L, 1113L, 1168L, 1174L, 1229L, 1235L, 1290L, 
                           1296L, 1351L, 1357L, 1412L, 1418L, 1473L, 1479L))

As you can see, I have classified the activity in 4 classes (0 to 3). Now I want to draw a graph showing the activity as a line. I am using ggplot2 for this:

library(ggplot2)

# First attempt: size = 0 is still plotted as a line
ggplot(df, aes(x=periode,y=soort_wet, group=soort_wet)) +
  geom_line(aes(size=activiteit))

enter image description here

Well, that's almost what I want, except: I want the lines with acitiviteit==0 and hence size==0 NOT drawn. So my first question is why are lines with size==0 drawn and how can I solve this? Note that I don't want to replace the 0s with NAs, because that would cause my lines to end prematurely.

I thought I could solve this myself, by making these lines white (or colour=NA):

ggplot(df, aes(x=periode,y=soort_wet, group=soort_wet)) +
  geom_line(aes(size=activiteit,colour=ifelse(activiteit==0,NA,"black")))

But that got me into deeper trouble. Does anyone understand why this doesn't work (bonus question!).

enter image description here

Upvotes: 0

Views: 609

Answers (2)

Axeman
Axeman

Reputation: 35262

Admittedly, this is a bit annoying to do. Let me go over some of the stuff in your attempts and why it didn't work first.

First, simply stating size=activiteit does not mean a value of 0 will result in a size of 0. ggplot makes a scale first, by default between size 1 and 6. If you want to map to actual values you have to use scale_size_identity() or adjust the range in the scale_size_continuous. Unfortunately, adjusting this will result in a better graph, but for a reason I don't understand either, the line is still visible.

Next, your second attempt doesn't work either, since you are now mapping the value 'black' (this sort of approach would work in base R).

Now, to get around all this, we can make a new variable that is TRUE when activiteit == 0, and map that variable to alpha, so we make that part of the line see-through.

Example code:

df$alp <- ifelse(df$activiteit == 0, T, F)

ggplot(df, aes(x=periode,y=soort_wet, group=soort_wet, alpha=alp)) +
  geom_line(aes(size=activiteit)) + theme_bw() +
  scale_alpha_manual(values = c("TRUE" = 0, "FALSE" = 1), guide = 'none') +
  scale_size_continuous(range = c(0, 15)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

Results in:

enter image description here

Edit:

I just realized there is an even easier way, you can just adjust the limits of the size scale to not include 0:

ggplot(df, aes(x=periode,y=soort_wet, group=soort_wet)) +
  geom_line(aes(size=activiteit)) + theme_bw() +
  scale_size_continuous(range = c(3, 15), limit = c(1, 3)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

enter image description here

Upvotes: 2

MLavoie
MLavoie

Reputation: 9836

would that work for you?

you subset first for you want

df_s <- df %>% filter(activiteit > 0)

and then plot

ggplot(df_s, aes(x=periode,y=soort_wet, group=soort_wet)) +
  geom_line(aes(size=as.factor(activiteit)))

you size 0 is not plot.

enter image description here

Upvotes: 0

Related Questions