ggplot2 legend, geom_abline and stat_gg - r

I have been trying to plot a Normal Q-Q plot with a red line across, in R with ggplot2. I have been unable to add a legend (with LaTeX math) to explain the red line
Here is the code for the basic figure:
ggplot(stdres_df, aes(sample=stdres)) +
stat_qq(color="black") +
geom_abline(slope = 1,
intercept = 0, color ="red")
Thanks in advance.

To get a legend, you need to map something to a color aesthetic inside a call to aes(). In this case, there's no grouping variable to map to colour, but you can just map colour to the name you want to use for the red line.
The line will be red by default, because ggplot uses hcl(15, 100, 65) (a light red) as the first color in its default color palette. However, you can set the color to whatever you want using scale_colour_manual, as shown in the example below. For example:
set.seed(2)
df <- data.frame(y = rnorm(200))
ggplot(df, aes(sample = y)) +
stat_qq() +
geom_abline(aes(slope=1, intercept=0, colour="Test"), size=1) +
coord_equal(xlim=range(df$y)) +
labs(colour="") +
scale_colour_manual(values="red")

Something like this?
ggplot() +
stat_qq(aes(sample=1:100), distribution = qt,dparams = list(df=5)) +
geom_abline(aes(linetype = "line"), slope = 1, intercept = 0, color ="red") +
geom_text(aes(3, 0, label = "TEXT HERE"))

Related

ggplot2 unable to color legend icons

I'm trying to use ggplot2 to make some sort of timeline using values from a dataframe (df). I've managed to plot the data exactly how I want it (the different colored line segments connecting the x-marks in this exact order, i.e., from left to right: 'early', 'unknown', 'late', 'sub'). The startpoint and endpoint columns in the dataframe are used to define the positions of the points and line segments.
The problem is that the legend doesn't show the color of the 'x' icons, they are just grey. I've tried adding scale_color_manual() and scale_fill_manual() commands but they don't seem to change anything. The legend does display the correct color when I change the shape to shape = 21, however, I really want the shape to be 4 (x icons). I don't care about the shape of the legend though but scale_shape_manual() again didn't change anything about the legend.
I have also tried placing different color arguments inside and outside the aes() argument of ggplot(), geom_segment() and/or geom_point().
How can I make the icons from the legend show the correct color?
Below I added a piece of code to reproduce the problem.
library(ggplot2)
library(RColorBrewer)
## Define dataframe
df <- data.frame(Var = c("sub","late","unknown","early"),
Time = c(10,267,0,1256),
Endpoint = c(1533,1523,1256,1256),
Startpoint = c(1523,1256,1256,0))
colorscheme <- RColorBrewer::brewer.pal(9, "Set1")[c(1,4,2,3)]
## Make plot
ggplot(df, aes(x="", y=Endpoint, fill=Var), color =colorscheme) +
geom_segment( aes(x="", xend="", y=Startpoint, yend=Endpoint), color = colorscheme) +
geom_point(aes(x="", y=Endpoint),size=5, shape=4 , color = colorscheme) +
coord_flip()
Thanks in advance for any suggestions!
You should use color instead of fill. To remove the line from the legend, use guides(color = guide_legend(override.aes = list(linetype = 0))) or use show.legend = F in geom_segment.
Also, arguments passed in ggplot need not to be repeated afterward.
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme) +
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint)) +
geom_point(size=5, shape=4) +
coord_flip() +
guides(color = guide_legend(override.aes = list(linetype = 0)))
#or
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme) +
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint)) +
geom_point(size=5, shape=4) +
coord_flip()
Try this:
ggplot(df, aes(x = "", y = Endpoint, color = Var), colorscheme) +
geom_segment(aes(x = "", xend = "", y = Startpoint, yend = Endpoint), show.legend = FALSE) +
geom_point(aes(x = "", y = Endpoint), size = 5, shape = 4) +
coord_flip()
In this way legend will show only X

Add simple histogram to legend in ggplot2

Given a ggplot plot generated using the following code
size = 10000
d = data.frame(
type = rep(c("A","B","C"), each=size),
val = c(rnorm(size, 0, 1), rnorm(size, 1, 2),rnorm(size, 2, 3))
)
require(ggplot2)
(
ggplot(subset(d, is.element(type, c("A", "C"))), aes(x=val))
+ geom_histogram(aes(y=..density..), bins=100, position="identity", alpha=0.5)
+ geom_line(aes(color=type), stat="density", size=1)
)
Is it possible to add a grey square with a custom label representing the simple histogram to the legend? Can it be done without creating a dummy item?
All you need is to put fill= into aes() for the geom_histogram() line. You don't have a column in your dataset to assign to this, but if you assign fill="string name" in aes(), then ggplot will create a fill legend with that as the label.
Of course, the color will default to the ggplot "red" color, so if you want to go with gray again, you have to set that with scale_fill_manual(), since fill= outside aes() will overwrite anything you put within aes().
ggplot(d, aes(x=val)) +
geom_histogram(aes(y=..density.., fill='histogram'),
bins=100, position="identity", alpha=0.5) +
geom_line(aes(color=type), stat="density", size=1) +
scale_fill_manual(values='gray20')

Legend for overlaid plots in ggplot

I'm trying to make a plot that overlays a bunch of simulated density plots that are one color with low alpha and one empirical density plot with high alpha in a new color. This produces a plot that looks about how I want it.
library(ggplot2)
model <- c(1:100)
values <- rnbinom(10000, 1, .4)
df = data.frame(model, values)
empirical_data <- rnbinom(1000, 1, .3)
ggplot() +
geom_density(aes(x=empirical_data), color='orange') +
geom_line(stat='density',
data = df,
aes(x=values,
group = model),
color='blue',
alpha = .05) +
xlab("Value")
However, it doesn't have a legend and I can't figure out how to add a legend to differentiate plots from df and plots from empirical_data.
The other road I started to go down was to put them all in one dataframe but I couldn't figure out how to change the color and alpha for just one of the density plots.
Moving the color = ... into the aes allows you to call the scale_color_manual and move them into the aes and make the values you pass to color a binding. You can then change it to whatever you want as the actual colors are determined in the scale_color_manual.
ggplot() +
geom_density(aes(x=empirical_data, color='a')) +
geom_line(stat='density',
data = df,
aes(x=values,
group = model,
color='b'),
alpha = .05) +
scale_color_manual(name = 'data source',
values =c('b'='blue','a'='orange'),
labels = c('df','empirical_data')) +
xlab("Value")

Display legends on a combined ggplot2 plot - Stacked bar and line

I have a ggplot, which is a combination of a stacked graph and line graph
ggplot() +
geom_bar(data=smr2, aes(x=Pract, y=value, fill=variable), stat='identity') +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
geom_line(data=summarised[,1:3], aes(x=Pract,y=YTDTarget, group=1),size = 1) +
geom_point(data=summarised[,1:3], mapping = aes(x = Pract, y = YTDTarget),size=2.5)+
geom_text_repel(data=summarised[,1:3], aes(x=Pract,y=YTDTarget,label=YTDTarget), size = 5)
I want to add the legend for line graph. But the part group=1 seems to prevent this.
The graph I created is as
Also, please help to change the name of the legend from variable to "Actuals"
This graph is for compare the Target(line graph) against actually achieved(stacked Bar).
Please try this:
To geom_line add dummy variable (to add it to legend - in this case I'm using linetype).
geom_line(data = summarised[,1:3],
aes(Pract, YTDTarget, group = 1, linetype = ""),
size = 1)
To change legend name add labs() to your plot.
labs(fill = "Actuals",
linetype = "My Line Name")

making a single gridline thicker in ggplot2

I have a plot of a bunch of values between -1 and 1. Let's say it looks like this:
data <- data.frame(x=1:20,y=runif(20)*2-1)
p <- ggplot(data=data,aes(x=x,y=y)) + geom_line() + scale_y_continuous(breaks=seq(-1,1,0.5),limits=c(-1,1))
p
What I want is for the horizontal line with y=0, to be thicker than all the other gridlines. Is there a simple way to do this?
You can add a line at y = 0 via geom_hline. Put this layer before geom_line so it is behind the rest of the plot.
ggplot(data=data,aes(x=x,y=y)) +
geom_hline(yintercept = 0, color = "white", size = 2) +
geom_line() +
scale_y_continuous(breaks=seq(-1,1,0.5),limits=c(-1,1))

Resources