24

I create a graphic with several groups and plotting a geom_boxplot() over a seet of lines. However, it would be nice to colour the boxes transparently so that the lines can be seen.

Here's some sample data:

x11()

name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
year <- c("2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)

df <- data.frame(name, class, year, value)
df

I draw the graphic with:

p1 <- ggplot(df, aes(year, value))
p1 <- p1 + geom_line(aes(group=name, size=name),colour="#ff2300",alpha=0.5) +     facet_wrap(~ class, scales = "free_y") 
p1 <- p1 + geom_boxplot(aes(group=name))
print(p1)

And on my system the line width in the legend is not correctly displayed. Am I doing something wrong? Thanks in advance!

Seb
  • 5,417
  • 7
  • 31
  • 50

2 Answers2

39

You can add alpha argument to your boxplot. For example:

geom_boxplot(aes(group=name), alpha = 0.8)

will give you

enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    To get it completely transparent set alpha to 0. ~ geom_boxplot(aes(group=name), alpha = 0) – Amroco Aug 26 '21 at 06:17
5

Change the order of the geoms to draw the boxplot first and then the lines. However I don't think your graph makes sense. Why are you using name to change the size of the line? Wouldn't it make more sense to change the linetype? And I advise against free_y in the facets since it makes it hard to compare.

name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
year <- c("2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)

df <- data.frame(name, class, year, value)
df
library(ggplot2)
p1 <- ggplot(df, aes(year, value))
p1 <- p1 + geom_boxplot(aes(group=name)) + geom_line(aes(group=name, size=name),colour="#ff2300",alpha=0.5) + 
  facet_wrap(~ class, scales = "free_y")
Luciano Selzer
  • 9,806
  • 3
  • 42
  • 40
  • +1 for warning about `free_y`. Luckily, ggplot's background alerts that the scales are not comparable between facets. – Roman Luštrik Dec 05 '11 at 15:21
  • hi, thanks for the hint. yes, you're right. the graph does not make much sense! especially the line size argument was just something i tried for fun - shouldn't have it posted that way. concerning the `free_y` argument: the original data contains classes on different real estate types which are hardly comparable (e.g. rents vs. prices) so i think it is okay. – Seb Dec 05 '11 at 20:14