Search code examples
rggplot2legendalphafill

ggplot scale alpha continuous fill color


I've tried to create an alpha plot but I couldn't find right way to do it. I tried different combination to figure out and I've almost there but I need a little help.
My question is how can I get rid off blue color in the plot.

My script is `

p <- ggplot(df, aes(x=x, y=y))
p + geom_hex(aes(alpha=..count..),bins=20)+
  scale_x_log10("ratio following/followers",
                labels = trans_format("log10", math_format(10^.x))) + 
  scale_y_log10("ratio messages received/sent", 
                labels = trans_format("log10", math_format(10^.x))) +
  theme_bw() + 
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor=element_blank(),
        plot.background = element_blank())+ 
  #guides(fill=FALSE)+
  scale_alpha_continuous ("Counts",breaks=c(0,2000,4000,6000,8000,10000))+
  geom_vline(xintercept =1, color="red", size=0.25, linetype=5)+
  geom_hline(yintercept =1, color="red", size=0.25, linetype=5) +
  annotate('text', x=500, y=0.01, size=3, label="4\ncommon\nusers") +
  annotate('text', x=0.0001, y=0.01, size=3, label="3\nbroadcasters") +
  annotate('text', x=0.0001, y=7000, size=3, label="1\ninfluentials") +
  annotate('text', x=500, y=7000, size=3, label="2\nhidden\ninfluentials")

This script creates this plot

This scrip creates this plot

I can be able to get rid off blue legend with activating "guides(fill=FALSE)+" line in the script and it give this:

guides(fill=FALSE)+

You can reach sample data from here

Thanks @Didzis Elferts for his answer. I couldn't be sure about the legend and plot breaks colors. As you can see these pictures 10K and 8K has the same color (Am I right!) so 10K should be darker, shouldn't be. 10K 8K


Solution

  • Function geom_hex() by default maps counts to the fill and so you get fill gradient (blue by default). If you just want map counts to alpha values then you must assign fill = outside the aes() of geom_hex() to some color (used grey45 as example). As fill = is set outside the aes() there won't be legend for fill values.

    p + geom_hex(aes(alpha=..count..),bins=20, fill = "grey45")+ ...
    

    enter image description here