ggplot scale alpha continuous fill color - r

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
I can be able to get rid off blue legend with activating "guides(fill=FALSE)+" line in the script and it give this:
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.

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

Related

Can't add legend into my graph on ggplot, Why is this the case?

I want to add a legend it shows black for the 7 day moving average and blue for the bars(daily cases). so it looks something similar to the NHS graph , but the legend does not work when i add it into my code?
ggplot(LimerickNew1, aes(x=TimeStamp, y=DailyCCase,Shape=MA7)) +
geom_bar(stat="identity",fill="steelblue") +
geom_line(aes(y=MA7),size=1.5) +
labs( x="", y="Total cases", title="Total Covid Cases for County Limerick 01-03-20 to 01-
06-`20" )+`
theme_bw()+
theme(legend.background = element_rect(fill = "darkgray"),
legend.key = element_rect(fill = "lightblue", color = NA),
legend.key.size = unit(1.5, "cm"),
legend.key.width = unit(0.5,"cm"),
axis.text.x = element_text(face="bold", color="#008000",size=12, angle=0),
axis.text.y = element_text(face="bold", color="#008000",size=12, angle=0),
axis.title.x = element_text(face="bold", size=12, color="black"),
plot.title = element_text( face = "bold", colour = "black", size = 20,, hjust = 0.5))
Without a reproducible example, it's hard to give a more specific answer for your question, but here's a method that expands upon the comment from #teunbrand and should guide you toward showing a legend.
TL;DR - The reason you're not seeing a legend is because you do not have any layers or geoms drawn that have one of the aesthetics mapped to aes(). All aesthetics like color, size, and fill are specified for each geom outside of an aes() function, which means there's no reason for ggplot2 to draw a legend. To get a legend to be drawn, you need to specify the aesthetic inside aes().
Example
Here's an illustrative example that is similar to OP's question.
library(ggplot2)
set.seed(8675309)
df <- data.frame(
x = 1:100,
y = rnorm(100, 10))
df$z <- log(cumsum(df$y))
ggplot(df, aes(x=x)) +
geom_col(aes(y=y), fill='blue', alpha=0.3) +
geom_line(aes(y=z), size=2) +
ylim(0,25) + theme_bw()
Showing a Legend
To get a legend to show, you have to specify one or more aesthetics inside aes(). The fun fact here is that you don't have to specify a column in the dataset when doing this. If you specify a single value, it will apply to the whole dataset, and basically just show a legend key for the entire set of observations. This is what we want.
ggplot(df, aes(x=x)) +
geom_col(aes(y=y, fill="the columns"), alpha=0.3) +
geom_line(aes(y=z, size="the line"), color="black") +
ylim(0,25) + theme_bw()
Formatting
The names come from the values specified in aes(), and the color change is due to default mapping of color values. To get things to look "right", you want to use some theme() elements to:
keep only one name
squish legends together
position on the chart, rather than on the side
The end result is here:
ggplot(df, aes(x=x)) +
geom_col(aes(y=y, fill="the columns"), alpha=0.3) +
geom_line(aes(y=z, size="the line"), color="black") +
ylim(0,25) +
scale_fill_manual(values="blue") + # recolor the columns
guides(
fill=guide_legend(title="My Legend", order=1),
size=guide_legend(title=NULL, order=2)
) +
theme_bw() +
# legend placement and spacing
theme(
legend.title = element_text(margin=margin(b=10)),
legend.spacing.y = unit(-3,'pt'),
legend.position = c(0.8, 0.8)
)

ggplot R grey scale but still colours

I want to create a line+dots plot with confidence intervals all in grey, but I obtain a coloured plot.
I used the option scale_fill_grey but this worked only on the confidence intervals and not on lines and points.
a<-ggplot(df, aes(x = Yr, y = SIR, color=Type, shape=Type,linetype=Type))+geom_point(size=2.5) + geom_smooth(method=lm, aes(fill=Type))
a + scale_fill_grey(start=0.8, end=0.5)+labs(x="Year", y="SIR")+theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
How can I obtain a BW-grey plot?
Thank you!
To change the colour of your points and lines, you need to use the colour aesthetic and not fill.
Creating a similar plot with the iris dataset, and adding scale_fill_grey() will only colour the confidence areas of your plot:
a<-ggplot(iris, aes(x = Petal.Length, y = Sepal.Length, color=Species, shape=Species,linetype=Species))+geom_point(size=2.5) + geom_smooth(method=lm, aes(fill=Species))
a + scale_fill_grey()
To get the points as well, you need to add scale_color_grey():
a + scale_fill_grey() + scale_color_grey()
For more details on ggplot colour aesthetics, see the docs:
https://ggplot2.tidyverse.org/reference/aes_colour_fill_alpha.html

Adding a single legend with very long text to a ggplot2 facet_grid

Using R ggplot2 library, I an generating a facet_grid plot and I want to put a legend off to the right hand side of all the plots -NOT EACH INDIVIDUAL PLOT, but one legend that works for all the plots
g = ggplot(pd2, aes(x = p/1000, y=c/1000 )) +
geom_point(alpha=.1, colour="red") +
geom_smooth(method=lm) +
xlab("Ave P") +
ylab("Ave C)") +
labs(title = "Comparison ") +
theme(plot.title = element_text(face="bold",
size=14, colour="black", hjust = 0.5)) +
theme(axis.title = element_text(face="bold", size=12, colour="blue")) +
facet_grid(code ~ TheState) +
theme(strip.text.y = element_text(colour = "purple", angle = 0,
size = 9, hjust = 0.5, vjust = 0.5))
What I want to do is have a legend off to the right that is turned 90 degrees - because I want the legend to refer to very_long_code instead of just "code." I've tried all kinds of variations of legend, theme, and guide. Intuitively, I would like to say,
legend(text, angle=90, very_long_code, color="purple")
The next step would be to not use a single color, but to use a different color for each one that would correspond to colors in the strip.

How to enable x-axis and y-axis line in GGPLOT theme_classic()

With this code:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) +
geom_jitter(position=position_jitter(0.2))+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
p + theme_classic()
I expect to get image like this:
But how come I get this instead:
Notice the missing x-axis an y-axis line.
How can I enable it?
This is theme_classic() specific issue.
Here is a solution from this GitHub issue
p + theme_classic() +
theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid'))
Edit
If you are running into this issue, updating ggplot2 should fix the issue, and the solution above shouldn't be necessary.

Place a grey box around chart title in ggplot2

when we use the facet option in ggplot, we get a beautiful grey box around the headings (3,4,5).
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(cyl)) + geom_bar() + facet_wrap(~gear) + theme_bw()
How can we place a similiar grey box around the chart title when we do no use the facet option?
ggplot(mtcars, aes(cyl)) + geom_bar() + theme_bw() +
ggtitle("How do you put a grey box around me??")
Since you are asking how to do it without facets, this is strictly speaking not an answer, but just to point it out, one quick and dirty way would be to "cheat" and to use a facet after all. For instance,
mtcars$title <- "How do you put a grey box around me??"
ggplot(mtcars, aes(cyl)) + geom_bar() + theme_bw() +
facet_grid(. ~ title)
does the trick.
you can use annotate and change plot title margin, then from annotate you can use "rect" geom for annotation:
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar() +
theme_bw() +
ggtitle(" How do you put a grey box around me??") +
theme(panel.grid = element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"),
plot.title = element_text(size = 12, color = "black", face = "bold", margin = margin(t = 10, b = -30)))+
annotate("rect",xmin=0,xmax=3,ymin=14,ymax=15,alpha=.2,color="black", linetype = 'dotted', fill = "grey1")
Title with borders from annotate
The other best and easy solution to save image as SVG then open it from inkscape 'free software' and then easily add rectangle to the title:
Title grey box using inkscape

Resources