Last Updated
Viewed 2,085 Times

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:

      +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.

Can I change the symbol used in the legend for a boxplot? I would like to use a square instead of the boxplot symbol that is the default. I've tried the following after some searching, however the last line does not seem to have an effect:

df = data.frame(x = rnorm(10), y = sample(letters[1:2], 10, TRUE))
ggplot(df, aes(y, x)) +
geom_boxplot(aes(color = y)) +
guides(fill = guide_legend(override.aes = list(shape = 22)))

The reason is that there is another graph left of this one that uses the same colors but points instead of a boxplot, so I would like a legend that works for both.

I have combined ggplot boxplots with a connecting line which is the mean average.

How could I create a legend so people know the blue circle points represent the mean average for each boxplot?


ggplot(Price, aes(x=Price$stage_code, y=Price$Realvalue)) + 
  scale_y_continuous(labels = comma) +
  geom_boxplot(notch=FALSE, outlier.shape=NA, fill="red", alpha=0.2) +
  coord_cartesian(ylim=c(0,1000000000)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
  ggtitle("Average True Value of Listed Mining Companies\nThroughout Mine Development Stages") +
  xlab("Project Development Stages") + 
  ylab("Number of Diluted Stocks x Closing Stock Price") +
  stat_summary(fun.y=mean, geom="line", linetype="dotted", 
               size=1.4, color = "Blue",alpha=0.6, aes(group=1)) +
  stat_summary(fun.y=mean, geom="point", alpha=1, color="darkblue", 
               size=3.2, shape = 21, fill = "lightblue", stroke = 2)

Boxplot + Mean line

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?

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

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

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

Similar Question 8 (1 solutions) : ggplot2: strange behaviour of geom_boxplot

Similar Question 9 (1 solutions) : R boxplot vs ggplot2 geom_boxplot