Problems adding legend to ggplot2 + ggfortify - r

I'm having troubles using
scale_colour_manual
function of ggplot. I tried
guide = "legend"
to force legend appears, but it doesn't work. Rep code:
library(ggfortify)
library(ggplot2)
p <- ggdistribution(pgamma, seq(0, 100, 0.1), shape = 0.92, scale = 22,
colour = 'red')
p2 <- ggdistribution(pgamma, seq(0, 100, 0.1), shape = 0.9, scale = 5,
colour = 'blue', p=p)
p2 +
theme_bw(base_size = 14) +
theme(legend.position ="top") +
xlab("Precipitación") +
ylab("F(x)") +
scale_colour_manual("Legend title", guide = "legend",
values = c("red", "blue"), labels = c("Observado","Reforecast")) +
ggtitle("Ajuste Gamma")

A solution with stat_function:
library(ggplot2)
library(scales)
cols <- c("LINE1"="red","LINE2"="blue")
df <- data.frame(x=seq(0, 100, 0.1))
ggplot(data=df, aes(x=x)) +
stat_function(aes(colour = "LINE1"), fun=pgamma, args=list(shape = 0.92, scale = 22)) +
stat_function(aes(colour = "LINE2"), fun=pgamma, args=list(shape = 0.9, scale = 5)) +
theme_bw(base_size = 14) +
theme(legend.position ="top") +
xlab("Precipitación") +
ylab("F(x)") +
scale_colour_manual("Legend title", values=c(LINE1="red",LINE2="blue"),
labels = c("Observado","Reforecast")) +
scale_y_continuous(labels=percent) +
ggtitle("Ajuste Gamma")

This appears to be a bug with ggfortify.* You can achieve identical results simply using geom_line() from ggplot2 though:
library(ggplot2)
# Sequence of values to draw from dist(s) for plotting
x = seq(0, 100, 0.1)
# Defining dists
d1 = pgamma(x, shape=0.92, scale=22)
d2 = pgamma(x, shape=0.90, scale=5)
# Plotting
p1 = ggplot() +
geom_line(aes(x,d1,colour='red')) +
geom_line(aes(x,d2,colour='blue')) +
theme_bw(base_size = 14) +
theme(legend.position="top") +
ggtitle("Ajuste Gamma") +
xlab("Precipitación") +
ylab("F(x)") +
scale_colour_manual("Legend title",
guide = "legend",
values = c("red", "blue"),
labels=c("Observado", "Reforecast"))
* Related question: Plotting multiple density distributions on one plot

Related

R: adding a legend to a density plot

I am working with the R programming language. I am following this tutorial here on density plots: https://www.r-graph-gallery.com/2d-density-plot-with-ggplot2.html
I am trying to figure out how to add a "legend" to the density plots, so that the user can see roughly how many observations are located within a given region of the density plot.
I was able to figure out how to do this for a basic plot (by following the tutorial) :
#load library
library(ggplot2)
#create data
a <- data.frame( x=rnorm(20000, 10, 1.9), y=rnorm(20000, 10, 1.2) )
b <- data.frame( x=rnorm(20000, 14.5, 1.9), y=rnorm(20000, 14.5, 1.9) )
c <- data.frame( x=rnorm(20000, 9.5, 1.9), y=rnorm(20000, 15.5, 1.9) )
data <- rbind(a,b,c)
#make density plot
ggplot(data, aes(x=x, y=y) ) +
geom_bin2d(bins = 70) +
scale_fill_continuous(type = "viridis") +
theme_bw()
As seen in the above plot, a legend has been automatically created ("count").
But when I try to do this for the other plots in the tutorial, no legends are added:
# plot 1
ggplot(data, aes(x=x, y=y) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
scale_fill_distiller(palette=4, direction=-1) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme(
legend.position='none'
)
# plot 2
ggplot(data, aes(x=x, y=y) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
scale_fill_distiller(palette=4, direction=1) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme(
legend.position='none'
)
# plot 3
ggplot(data, aes(x=x, y=y) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
scale_fill_distiller(palette= "Spectral", direction=1) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme(
legend.position='none'
)
Can someone please show me if it is possible to add legends to these plots?
Thanks

How can I make a density scatterplot with log scale in R?

I'd like to make a density scatterplot with log10 scale in R. I tried to plot it using ggplot and stat_density2d in R. I used this code:
ggplot(data=vod_agb_df, aes(vod, agb)) +
stat_density2d(aes(fill = ..density..), geom = "tile", contour = FALSE, n = 100) +
scale_fill_distiller(palette = 'YlOrRd', direction = 1) +
scale_x_continuous(breaks=seq(0, 1, 0.25), limits = c(0, 1)) +
scale_y_continuous(breaks=seq(0, 300, 50), limits = c(0, 300)) +
labs(x='L-VOD', y='AGB(Mg/ha)') +
theme_bw()
But the result looks strange. the density scatterplot with my code
This is the plot I want to plot
The original scatterplot
You can log10-transform the density; here's a minimal & reproducible example
library(MASS)
library(tidyverse)
set.seed(2020)
mvrnorm(100, mu = c(0, 0), Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)) %>%
as_tibble() %>%
ggplot(aes(V1, V2)) +
stat_density2d(
aes(fill = log10(..density..)), geom = "tile", contour = FALSE, n = 100) +
scale_fill_distiller(palette = 'YlOrRd', direction = 1) +
theme_bw()
Update
It's not clear to me what you mean by ""I'd like to make the density scatterplot in the point distributed area, not the whole area of the plot."" If you're asking how to increase the height of the gradient colour bar, you can do the following
set.seed(2020)
mvrnorm(100, mu = c(0, 0), Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)) %>%
as_tibble() %>%
ggplot(aes(V1, V2)) +
stat_density2d(
aes(fill = log10(..density..)), geom = "tile", contour = FALSE, n = 100) +
scale_fill_distiller(palette = 'YlOrRd', direction = 1) +
theme_bw() +
guides(fill = guide_colorbar(barheight = unit(3.5, "in"), title.position = "right"))
Whatever plot you are showing as your expected output for that you can use following code
library(tidyverse)
# Bin size control + color palette
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length) ) +
geom_bin2d(bins = 20) +
scale_fill_distiller(palette = 'YlOrRd', direction = 1) +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Delete the symbol a from legend

I need to delete that symbol 'a' that is coming in the legend, plus I would like to know if there is a possibility to place the label on the top of the bars.
This my example file:
Residue,Position,Weight,SVM Count,Odd,Ttest,lower,upper,Resistance
G163R,163,0.357,49,19.9453848,6.978518E-82,5.6628402,70.2925768,Accessory
V165I,165,0.268,49,2.98167788,1.60934E-80,1.25797484,7.06728692,Novel
N155H,155,0.253,50,38.6089584,1.089188E-83,9.5815554,155.7070612,Major
library(ggplot2)
m <- read.csv('example.csv', header=T, row.names=1)
boxOdds = m$Odd
df <- data.frame(
yAxis = length(boxOdds):1,
boxnucleotide = m$Position,
boxCILow = m$lower,
boxCIHigh = m$upper,
Mutation = m$Resistance)
ticksy<-c(seq(0,0.3,by=.1), seq(0, 1, by =.5), seq(0, 20, by =5), seq(0, 150, by =50))
ticksx<-c(seq(0,300,by=25))
p <- ggplot(df, aes(x = boxnucleotide, y = boxOdds, colour=Mutation,label=rownames(m)))
p1 <- p + geom_errorbar(aes(ymax = boxCIHigh, ymin = boxCILow), size = .5, height = .01) +
geom_point(size = 1) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
scale_y_continuous(breaks=ticksy, labels = ticksy) +
scale_x_continuous(breaks=ticksx, labels = ticksx) +
coord_trans(y = "log10") +
ylab("Odds ratio (log scale)") +
scale_color_manual(values=c("#00BFC4","#F8766D","#619CFF")) +
xlab("Integrase nucleotide position") +
geom_text(size=4,hjust=0, vjust=0)+
theme(legend.position = c(0.9, 0.9))
p1
I already tried all possible solutions from Remove 'a' from legend when using aesthetics and geom_text but none worked out

Generate two plots with same x axis breaks

I have two plots I want the x axes being broken by the same way.
This is the code for plot 1:
m <- read.csv('Finalfor1lowergreaterthan1.csv', header=T, row.names=1)
m <- m[m$SVM.Count >= 40,]
boxOdds = m$Odd
df <- data.frame(
yAxis = length(boxOdds):1,
boxnucleotide = m$Position,
boxCILow = m$lower,
boxCIHigh = m$upper,
Mutation = m$Resistance)
ticksy <- c(seq(0,0.3,by=.1), seq(0, 1, by =.5), seq(0, 20, by =5), seq(0, 150, by =50))
ticksx <- c(seq(0,300,by=25))
p <- ggplot(df,
aes(x = boxnucleotide, y = boxOdds, colour=Mutation, label=rownames(m)))
p1 <- p +
geom_errorbar(aes(ymax = boxCIHigh, ymin = boxCILow), size = .5, height = .01) +
geom_point(size = 1) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
scale_y_continuous(breaks=ticksy, labels = ticksy) +
scale_x_continuous(breaks=ticksx, labels = ticksx) +
coord_trans(y = "log10") +
ylab("Odds ratio (log scale)") +
scale_color_manual(values=c("#00BFC4","#F8766D","#619CFF")) +
xlab("Integrase nucleotide position") +
geom_text(size=2,hjust=0, vjust=0)
Then I have another plot:
m <- read.csv('Finalfor20lowergreaterthan1.csv', header=T, row.names=1)
#m <- m[m$SVM.Count >= 40, ]
boxOdds = m$Odd
df <- data.frame(
yAxis = length(boxOdds):1,
boxnucleotide = m$Position,
boxCILow = m$lower,
boxCIHigh = m$upper,
Mutation = m$Resistance)
ticksy <- c(seq(0,0.3,by=.1), seq(0, 1, by =.5), seq(0, 20, by =5), seq(0, 150, by =50))
ticksx <- c(seq(0,300,by=25))
p <- ggplot(df,
aes(x = boxnucleotide, y = boxOdds, colour=Mutation, label=rownames(m)))
p1 <- p +
geom_errorbar(aes(ymax = boxCIHigh, ymin = boxCILow), size = .5, height = .01) +
geom_point(size = 1) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
scale_y_continuous(breaks=ticksy, labels = ticksy) +
scale_x_continuous(breaks=ticksx, labels = ticksx) +
coord_trans(y = "log10") +
ylab("Odds ratio (log scale)") +
scale_color_manual(values=c("#00BFC4","#F8766D","#619CFF")) +
xlab("Integrase nucleotide position") +
geom_text(size=2,hjust=0, vjust=0)
Why is plot 1 starting from 75 on x axis and plot 2 starting at 100...how can plot2 start at 75 as well and being scaled like plot 1.
The two codes get the same piece of: ticksx <- c(seq(0, 300, by=25))
A good technique to align the axis range on different plots is to use expand_limits.
You can simply use p1 + expand_limits(x=c(0, 300)). This will ensure the x-axis contains at least 0 and 300 on all your plots. You can also control the y-axis range by using the y argument.
From ?expand_limits:
Sometimes you may want to ensure limits include a single value, for all panels or all plots. This function is a thin wrapper around geom_blank() that makes it easy to add such values.

Label on the side of box plot in R - ggplot

I want to add few labels on the side of the boxplot like image1. I have generated box plot in ggplot (image2). Please help me on the solution.
Please check my code for generating the boxplot,
library(ggplot2)
d <- data.frame(runif(100, min=0, max=10000))
names(d) <- "randnum"
box1 <- ggplot(d, aes_string(x=factor(0), y=d$randnum)) +
geom_boxplot(alpha = 0) + geom_jitter(size = 3, alpha = 0.5, color = "tomato")
box1 <- box1 + theme(legend.position = "none", axis.title =element_blank(),
axis.text.x =element_blank(), axis.ticks.x=element_blank())
box1
Thanks,
SJB.
We can use the ggrepel package, and create the texts and position we need.
We first need to calculate the y position of the labels, and I hope I got those right, change if needed.
Note that in this form many parts of the plot are hard coded and won't work in some cases, in particular the xlims are hand picked for my particular screen, and may need to be tweaked.
library(ggplot2)
library(ggrepel)
d <- data.frame(runif(100, min=0, max=10000))
names(d) <- "randnum"
first_quantile <- quantile(d$randnum, .25)
third_quantile <- quantile(d$randnum, .75)
inner_fence <- third_quantile + (third_quantile - first_quantile) * 1.5
outer_fence <- inner_fence + (third_quantile - first_quantile) * 1.5
fences <- data.frame(labels = c('first quartile', 'third quartile', 'inner fence', 'outer fence'),
y = c(first_quantile, third_quantile,
inner_fence, outer_fence))
ggplot(d, aes_string(x=factor(0), y=d$randnum)) +
geom_boxplot() +
geom_jitter(size = 3, alpha = 0.5, color = "tomato") +
geom_text_repel(data = fences, aes(x = 1.45, y = y, label = labels), nudge_x = 500, segment.color = 'green', xlim = c(NA, 2)) +
coord_cartesian(xlim = c(0.85,1.5)) +
theme_classic() +
theme(legend.position = "none", axis.title =element_blank(),
axis.text.x =element_blank(), axis.ticks.x=element_blank())
Created on 2018-05-16 by the reprex package (v0.2.0).
Or you can try
# your data
set.seed(1234)
d <- data.frame(runif(100, min=0, max=10000))
names(d) <- "randnum"
# the plot
box1 <- ggplot(d, aes_string(x=factor(0), y=d$randnum)) +
geom_boxplot(alpha = 0) +
geom_jitter(size = 3, alpha = 0.5, color = "tomato")
# the data for the annotation
d2 <- data.frame(y=boxplot(d,plot = F)$stats,
x=1.4,
xend=1.5)
d2 <- rbind.data.frame(d2, c(d2[4,1]+ (d2[4,1] - d2[2,1]) * 1.5, 1.4, 1.5))
d2 <- rbind.data.frame(d2, c(d2[6,1]+ (d2[4,1] - d2[2,1]) * 1.5, 1.4, 1.5))
d2$label <- c("Min", "1Q", "Median", "3Q", "Max", "Inner", "Outer")
# and the plot
box1 + scale_y_continuous(name="", sec.axis =dup_axis(name = "",
breaks = d2$y,
labels = d2$label)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=y), data = d2, inherit.aes = F)

Resources