Plotting legend in ggplot2 - r

I am trying to plot several things in a chart. Points colored by ID, the regression line, a modified regression line and an area where I do not want values to fall in.
I would like to have a legend with the names of the two lines. e.g. blue=Fitted model, red=Worst case scenario and the area, red= Suspect values.
This is the code that I used to create the graph:
ggplot(data, aes(x=log_dilution, y=ct)) +
geom_point(aes(color=ID),show.legend = FALSE) +
geom_smooth(aes(linetype ='Fitted model +95%CI'), method = 'lm',show.legend = TRUE) +
geom_segment(aes(x = 0, xend = 1.7, y = model1$coefficients[1], yend
=model1$coefficients[1] + (model1$coefficients[2]+(1.96*1.030535))*1.7),
color='red', lwd=1, lty=2,show.legend = FALSE) +
theme(axis.text.x= element_text(size=14), axis.title= element_text(size=16), axis.text.y = element_text(size=14)) +
ylim(15, 40) +
annotate('rect',xmin=0, xmax=1.7, ymin=35, ymax=40, alpha=0.2, fill="red") +
scale_size_manual( values = c(1.5, 1.5), labels = c("Fitted model +95%CI", "Worst case")) +
guides(color=FALSE)+ylab("Ct")+theme(legend.position = "bottom")
and this is the result so far.
Can anybody give me some directions on how I can plot a legend for the lines and the area (i am not interested in plotting the points)?

Related

gganimate along time series, not left to right

I have made a ggplot line plot that uses two uses two sets of time series data and looks as it should as a static plot. Script and plot below:
p_curve <- ggplot(df, aes(x = Var1, y = Var2)) +
geom_path(size = 1, colour = "red") +
geom_path(x = Var3, y = Var4, size = 1, colour = "blue") +
geom_vline(xintercept = 0) +
geom_hline(yintercept = Var2[1]) +
xlim(c(min(df$Var1, df$Var3)), c(max(df$Var1, df$Var3))) +
ylim(c(min(df$Var2, df$Var4)), c(max(df$Var2, df$Var4))) +
theme_classic() +
labs(x = "Variable", y = "Other Variable", title = "Variable x Variable Curve") +
theme(plot.title = element_text(hjust = 0.5),
panel.border = element_rect(colour = "black", fill = NA, size = 0.5))
The plot looks exactly as it should. I would like to animate it so that it starts where the data starts (the middle of the curve, intersection of hline and vline) and then follows the time series. When I add transition_reveal, the plot animates from left to right in a wipe like fashion.
p_curve + transition_reveal(along = Var1)
Can anyone help with getting this to reveal along the data series, not the x-axis? Thanks in advance.

How to colour background on a scatterplot using ggplot but still show data points in R?

This is my first question here so hope this makes sense and thank you for your time in advance!
I am trying to generate a scatterplot with the data points being the log2 expression values of genes from 2 treatments from an RNA-Seq data set. With this code I have generated the plot below:
ggplot(control, aes(x=log2_iFGFR1_uninduced, y=log2_iFGFR4_uninduced)) +
geom_point(shape = 21, color = "black", fill = "gray70") +
ggtitle("Uninduced iFGFR1 vs Uninduced iFGFR4 ") +
xlab("Uninduced iFGFR1") +
ylab("Uninduced iFGFR4") +
scale_y_continuous(breaks = seq(-15,15,by = 1)) +
scale_x_continuous(breaks = seq(-15,15,by = 1)) +
geom_abline(intercept = 1, slope = 1, color="blue", size = 1) +
geom_abline(intercept = 0, slope = 1, colour = "black", size = 1) +
geom_abline(intercept = -1, slope = 1, colour = "red", size = 1) +
theme_classic() +
theme(plot.title = element_text(hjust=0.5))
Current scatterplot:
However, I would like to change the background of the plot below the red line to a lighter red and above the blue line to a lighter blue, but still being able to see the data points in these regions. I have tried so far by using polygons in the code below.
pol1 <- data.frame(x = c(-14, 15, 15), y = c(-15, -15, 14))
pol2 <- data.frame(x = c(-15, -15, 14), y = c(-14, 15, 15))
ggplot(control, aes(x=log2_iFGFR1_uninduced, y=log2_iFGFR4_uninduced)) +
geom_point(shape = 21, color = "black", fill = "gray70") +
ggtitle("Uninduced iFGFR1 vs Uninduced iFGFR4 ") +
xlab("Uninduced iFGFR1") +
ylab("Uninduced iFGFR4") +
scale_y_continuous(breaks = seq(-15,15,by = 1)) +
scale_x_continuous(breaks = seq(-15,15,by = 1)) +
geom_polygon(data = pol1, aes(x = x, y = y), color ="pink1") +
geom_polygon(data = pol2, aes(x = x, y = y), color ="powderblue") +
geom_abline(intercept = 1, slope = 1, color="blue", size = 1) +
geom_abline(intercept = 0, slope = 1, colour = "black", size = 1) +
geom_abline(intercept = -1, slope = 1, colour = "red", size = 1) +
theme_classic() +
theme(plot.title = element_text(hjust=0.5))
New scatterplot:
However, these polygons hide my data points in this area and I don't know how to keep the polygon color but see the data points as well. I have also tried adding "fill = NA" to the geom_polygon code but this makes the area white and only keeps a colored border. Also, these polygons shift my axis limits so how do I change the axes to begin at -15 and end at 15 rather than having that extra unwanted length?
Any help would be massively appreciated as I have struggled with this for a while now and asked friends and colleagues who were unable to help.
Thanks,
Liv
Your question has two parts, so I'll answer each in turn using a dummy dataset:
df <- data.frame(x=rnorm(20,5,1), y=rnorm(20,5,1))
Stop geom_polygon from hiding geom_point
Stefan had commented with the answer to this one. Here's an illustration. Order of operations matters in ggplot. The plot you create is a result of each geom (drawing operation) performed in sequence. In your case, you have geom_polygon after geom_point, so it means that it will plot on top of geom_point. To have the points plotted on top of the polygons, just have geom_point happen after geom_polygon. Here's an illustrative example:
p <- ggplot(df, aes(x,y)) + theme_bw()
p + geom_point() + xlim(0,10) + ylim(0,10)
Now if we add a geom_rect after, it hides the points:
p + geom_point() +
geom_rect(ymin=0, ymax=5, xmin=0, xmax=5, fill='lightblue') +
xlim(0,10) + ylim(0,10)
The way to prevent that is to just reverse the order of geom_point and geom_rect. It works this way for all geoms.
p + geom_rect(ymin=0, ymax=5, xmin=0, xmax=5, fill='lightblue') +
geom_point() +
xlim(0,10) + ylim(0,10)
Removing whitespace between the axis and limits of the axis
The second part of your question asks about how to remove the white space between the edges of your geom_polygon and the axes. Notice how I have been using xlim and ylim to set limits? It is a shortcut for scale_x_continuous(limits=...) and scale_y_continuous(limits=...); however, we can use the argument expand= within scale_... functions to set how far to "expand" the plot before reaching the axis. You can set the expand setting for upper and lower axis limits independently, which is why this argument expects a two-component number vector, similar to the limits= argument.
Here's how to remove that whitespace:
p + geom_rect(ymin=0, ymax=5, xmin=0, xmax=5, fill='lightblue') +
geom_point() +
scale_x_continuous(limits=c(0,10), expand=c(0,0)) +
scale_y_continuous(limits=c(0,10), expand=c(0,0))

how to change x-axis labels in ggboxplot

I have a data frame including multiple factors. I used ggboxplot to get a box plot with comparisons for different categories. I am not satisfied with the x axis labels. I tried different ways but failed to get what I expected.
The code used to create a plot is:
df <- data.frame(country=sample(LETTERS[1:4], 1000, TRUE),
rating=round(rnorm(1000,70,15),1),
sex =rep(c("Female","Male"),500),
school=sample(c("public","private"),1000,TRUE))
df$group <- paste(df$school,df$sex,sep=".")
df <- df[order(df$group),]
my_comparisons <- list(c("public.Female","public.Male") , c("private.Female","private.Male"))
library(ggpubr)
ggboxplot(df, x = "group",y = "rating",
color = "group", palette = "simpsons",
add = "jitter",facet.by="country",legend="none", ylab="Rating") +
theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_blank()) +
stat_compare_means(method = "t.test",comparisons = my_comparisons,
label.y = 110,label = "p.signif")
The expected plot looks like:
This gets you close to what you're looking for (I couldn't figure out the line separator). You may also have to play around with the positioning of the labels to get them just right, as well as sizes.
ggboxplot(df, x = "group",y = "rating",
color = "group", palette = "simpsons",
add = "jitter", facet.by="country", legend="none", ylab="Rating") +
scale_x_discrete(labels=rep(c("F","M"),4)) +
theme(strip.text.x=element_text(size=10, color="red", face="bold.italic"),
axis.title.x = element_blank(),
plot.margin=unit(c(2,2,15,2), "mm")) +
stat_compare_means(method = "t.test",comparisons = my_comparisons,
label.y = 110, label = "p.signif") +
coord_cartesian(ylim=c(20,120), xlim=c(1,4), clip="off") +
annotate("text", x=1.5, y=0, label=c("","","Private","Private")) +
annotate("text", x=3.5, y=0, label=c("","","Public","Public")) +
annotate("text", x=0.5, y=10, label=c("","","Sex",""), hjust=1) +
annotate("text", x=0.5, y=0, label=c("","","School",""), hjust=1)
Additions include scale_x_discrete() to change x-axis labels, plot.margin and coord_cartesian to allow annotations outside the plot area, and annotate for each annotation, where the labels for each facet panel are given as a vector, with blanks for panels which shouldn't get labels.
There may be a cleaner way to do this, but the faceted nature of the plot means that annotations get replicated across facets which you don't want in this case.

Cowplot: How to add tick marks and corresponding data labels to a marginal plot? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
R Packages: cowplot / ggplot2
Use Case: Scatter plot with marginal histograms.
Issue: For histograms, I can't add bin sizes or reference lower/ upper
class intervals in the x-axis. Without these histograms are difficult
to read.
In cowplot, is there any way to add tick marks and corresponding data
labels (in x-axis) to marginal plots, when required? E.g. for
histograms in marginal plots
Basic scatter + marginal histogram plot using cowplot
require(ggplot2)
require(cowplot)
Main Plot:
pmain <- ggplot(data = mpg, aes(x = cty, y = hwy)) +
geom_point() +
xlab("City driving") +
ylab("Highway driving") +
theme_grey()
Marginal plot:
xbox <- axis_canvas(pmain, axis = "x") +
geom_histogram(
data = mpg,
aes(x = cty),
colour = "black"
)
Combined Plot:
p1 <- insert_xaxis_grob(pmain, xbox, grid::unit(0.5, "in"), position = "top")
ggdraw(p1)
However, I'd want the following plot xbox2 to be displayed as x-axis marginal plot:
xbox2.1 <- ggplot() +
geom_histogram(
data = mpg,
aes(x = cty),
colour = "black"
)
hist_tab <- ggplot_build(xbox2.1)$data[[1]]
xbox2 <- xbox2.1 +
scale_x_continuous(
breaks = c(round(hist_tab$xmin,1),
round(hist_tab$xmax[length(hist_tab$xmax)],1))
) +
labs(x = NULL, y = NULL) +
theme(
axis.text.x = element_text(angle = 90, size=7,vjust=0.5),
axis.line = element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
xbox2
But I can't create a scatter + marginal histogram (xbox2). I get the same plot as the first one:
p2 <- insert_xaxis_grob(pmain, xbox2, grid::unit(0.5, "in"), position = "top")
ggdraw(p2)
Package author here. What you're seeing is the documented behavior. From the documentation of the grob argument of insert_xaxis_grob():
The grob to insert. This will generally have been obtained via get_panel() from a ggplot2 object, in particular one generated with axis_canvas(). If a ggplot2 plot is provided instead of a grob, then get_panel() is called to extract the panel grob.
This function is specifically not meant to stack plots. You could turn your entire plot into a grob and then insert using this function, but I'm not sure that makes a lot of sense. What you're trying to do is equivalent to stacking two plots with the same x-axis range. I think it's better to just code it like that explicitly.
library(cowplot)
xlimits <- c(6, 38)
pmain <- ggplot(data = mpg, aes(x = cty, y = hwy)) +
geom_point() +
xlab("City driving") +
ylab("Highway driving") +
scale_x_continuous(limits = xlimits, expand = c(0, 0)) +
theme_grey() +
theme(plot.margin = margin(0, 5.5, 5.5, 5.5))
xhist <- ggplot() +
geom_histogram(
data = mpg,
aes(x = cty),
colour = "black",
binwidth = 1,
center = 10
) +
scale_x_continuous(limits = xlimits, expand = c(0, 0), breaks = 8:35) +
labs(x = NULL, y = NULL) +
theme(
axis.text.x = element_text(angle = 90, size=7, vjust=0.5),
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = margin(5.5, 5.5, 0, 5.5)
)
plot_grid(xhist, pmain, ncol = 1, align = "v", rel_heights = c(0.2, 1))

How to plot already transformed data by only tranforming the scale (x or y axis) but not data?

I have a data from my RNAseq data. I trying to plot log2FoldChange against the genome position.
ggplot(result.withGeneMap.asDF, aes(x=start, y= log2FoldChange)) +
ggtitle("Allele Balance Plot") +
theme(plot.title = element_text(hjust = 0.5), legend.position = "bottom") +
geom_hline(yintercept = c(-4, -2, 2, 4) ,
color = c("red", "darkgreen", "darkgreen", "red")) +
geom_point(aes(x=start, y= log2FoldChange,
color = sig), position=position_jitter(w=0.1,h=0), size = 1)
which gives me:
The data on y-axis is already on log2 scale. Everything is good, except I want to calibrate only the scale on y-axis at log2 level, not the data.
Whenever I am using scale_y_continous(trans = ...) or coord_trans(x = "log2", y="log2") the already transformed data gets further log2 transformed giving me.
Any suggestions?

Resources