Last Updated
Viewed 2,837 Times
              

I am trying to make boxplots with a transparent fill and a corresponding legend. The plotting with alpha values works fine except that the legend keys do not have transparent fills. The following example illustrates the difficulty:

dat <- data.frame(x=c('a', 'b', 'c'), y = runif(300), z = c('d', 'e', 'e'))
ggplot(dat) + geom_boxplot(aes(x, y, fill = z, colour = z), alpha = 0.2)

Boxplot with transparencies in fill, but not int the legend

Is there a way to make the fill values in the legend keys transparent too?

SessionInfo()
R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
ggplot2_0.8.9

Thanks in advance for any tips on this issue.

An example where the alpha mapping works is the following

ggplot(dat) + geom_point(aes(x, y, fill = z, colour = z, shape = z), alpha = 0.2)

Plot where alpha mapping works fine

I have created a faceted boxplot using the ggplot2 package. The R code is as follows:

version.labs <- c(`1`="Version 1.0", `2`="Version 2.0", `3`="Version 3.0", `4`="Version 4.0", `5`="Version 5.0")
ggplot(df, aes(x=factor(Subsystem), y=Risk.value, fill=factor(Version)) ) + 
  geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
  geom_boxplot(alpha = 0.5, show.legend = FALSE) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
  theme(strip.text.x = element_text(size=9, color="black", face="bold"))

The resulting plot looks pretty good (as shown below) exept for the legend.

enter image description here

In the legend I want to change the title as well as the text label for each item. The title should be "Version" and the labels "Version 1.0", ..., "Version 5.0".

I've tried various ways but they all add a new separate legend. The new legend looks good, but the old one is still there which doesn't look good and I can't find a way to remove it.

The last thing I tried was to add the scale_color_manual() function, like this:

scale_color_manual(name = "Version", labels=c("v1.0","v2.0","v3.0","v4.0","v5.0"), values=c("grey","blue","green","red","black"))

This results in a boxplot that looks like this.

enter image description here

As can be seen there are two legends. So, close but no cigar. Any hints on how to solve this are appreciated.

I am trying to use R ggplot2 package to make a boxplot.

However I can only get legend like this. Is there anyway I can change those legend key to just a solid square, instead of using those small box with central line?

The code I used is:

print(ggplot(mydata,aes(x=factor(sp),fill=factor(CommunityType),y=Abundance*100))+geom_boxplot(show_guide=FALSE)
      +theme(axis.text = element_text(colour = "black",size=10))
      +scale_y_continuous(" RA (%) ")+scale_x_discrete(limits=taxalist[1:5]," ")
      +scale_fill_manual(name = "MY type", values = mycol[1:nmc])
      +theme_bw() + guides(fill=guide_legend(title=NULL))+theme(legend.position=c(1,1),legend.justification=c(1,1)) 
      +theme(legend.key = element_blank(),legend.key.size = unit(1.5, "lines"))
      +theme( panel.grid.major = element_blank(),  panel.grid.minor = element_blank(),  panel.background = element_blank())  )

Sorry I cannot image here to describe my question.

I have made a boxplot using ggplot2 which appears beautifully coloured in R, with the corresponding legend. When I export this to plotly, the boxplots are all red, and the legend has disappeared. I cannot post a picture of the plot on here, as it wont let me, but my code is as follows:

r <- ggplot(data=all.data, aes(factor(Year), Data))+xlab("Year")
r <- r + geom_boxplot(outlier.shape = NA, aes(fill = factor(Month))) 
r <- r + scale_y_continuous(limits = c(-1, 6.5))
r <- r+ scale_fill_discrete(name = "Month")
r
gg <- ggplotly(r,kwargs=list(world_readable=FALSE))

I have included within the geom_boxplot layer the fill command which deals with colour, but I cannot understand why this is not transferring.

Similar Question 7 (2 solutions) : Legend customization for boxplots

Similar Question 8 (1 solutions) : Boxplot in R using ggplot2

Similar Question 9 (2 solutions) : Rotate boxplot legend (R, ggplot2)

cc