Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot

Creating a facet_wrap plot with ggplot2 with different annotations in each plot

To fix the second problem use

annotate("text", 0.375, -1.25,
label=paste("rho==", round(cor(abm.data$area, abm.data$U1_2), 2)),
parse=TRUE)

i.e. "rho==".

Edit: Here is a solution to solve the first problem

library("plyr")
library("ggplot2")

set.seed(1)
df <- data.frame(x=rnorm(300), y=rnorm(300), cl=gl(3,100)) # create test data
df.cor <- ddply(df, .(cl), function(val) sprintf("rho==%.2f", cor(val$x, val$y)))

p1 <- ggplot(data=df, aes(x=x)) +
geom_point(aes(y=y, colour="col1", alpha=0.4)) +
facet_wrap(~ cl, ncol=3) +
geom_text(data=df.cor, aes(x=0, y=3, label=V1), parse=TRUE) +
scale_colour_manual(values=c("col1"="red")) +
opts(legend.position="none")
print(p1)

Creating a facet_wrap plot with ggplot2 with less annotations than plots

This would be a minimum example.
What is important is the data for geom_text.

dat<-data.frame(fa=gl(4,3),x=runif(12),y=runif(12))
q<-ggplot(dat,aes(x=x,y=y))+geom_point()+facet_wrap(~fa)+
geom_text(data=data.frame(fa=gl(4,1),sig=c("","*","","+")),aes(x=0.5,y=0.5,label=sig))
print(q)

HTH.

ggplot2 facets: Different annotation text for each plot

You can put the expressions as character values in a new dataframe with the same unique Type's as in your data-dataframe and add them with geom_text:

regrDF <- data.frame(Type = c('a','b','c'), lbl = c('Regression_a', 'Regression_b', 'Regression_c'))

ggplot(Raw_Data, aes(x = Time, y = Velocity)) +
geom_point() +
geom_text(data = regrDF, aes(x = 10, y = 10, label = lbl), hjust = 0) +
facet_grid(Type ~.)

which gives:

Sample Image

You can replace the text values in regrDF$lbl with the appropriate expressions.

Annotating text on individual facet in ggplot2

Function annotate() adds the same label to all panels in a plot with facets. If the intention is to add different annotations to each panel, or annotations to only some panels, a geometry has to be used instead of annotate(). To use a geometry, such as geom_text() we need to assemble a data frame containing the text of the labels in one column and columns for the variables to be mapped to other aesthetics, as well as the variable(s) used for faceting.

Typically you'd do something like this:

ann_text <- data.frame(mpg = 15,wt = 5,lab = "Text",
cyl = factor(8,levels = c("4","6","8")))
p + geom_text(data = ann_text,label = "Text")

It should work without specifying the factor variable completely, but will probably throw some warnings:

Sample Image

Add another ggplot to/annotate a facet_wrap plot

g1 <- ggplotGrob(plot1)
g2 <- ggplotGrob(plot2)

g1 <- gtable::gtable_add_grob(g1, g2, t = 8, l=7)
grid.newpage()
grid.draw(g1)

How to add different annotations on figures with facet_wrap

As @stefan noted, your text layer's data should reference the faceting variable:

ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
facet_wrap(~am) +
geom_text(data = data.frame(wt = 2, mpg = c(30, 12), am = c(0,1),
label = c("one note", "and another")),
aes(label = label))

Sample Image

annotate r squared to ggplot by using facet_wrap

You can't apply different labels to different facet, unless you add another r^2 column to your data.. One way is to use geom_text, but you need to calculate the stats you need first. Below I show an example with iris, and for your case, just change Species for Variety, and so on

library(tidyverse)
# simulate data for 2 treatments
# d2 is just shifted up from d1
d1 <- data.frame(iris,Treatment="A")
d2 <- data.frame(iris,Treatment="B") %>%
mutate(Sepal.Length=Sepal.Length+rnorm(nrow(iris),1,0.5))
# combine datasets
DF <- rbind(d1,d2) %>% rename(Variety = Species)

# plot like you did
# note I use "free" scales, if scales very different between Species
# your facet plots will be squished
g <- ggplot(DF,aes(x=Sepal.Width,y=Sepal.Length,col=Treatment))+
geom_point(shape=1,size=1)+
geom_smooth(method=lm)+
scale_color_brewer(palette = "Set1")+
facet_wrap(.~Variety,scales="free")

# rsq function
RSQ = function(y,x){signif(summary(lm(y ~ x))$adj.r.squared, 3)}
#calculate rsq for variety + treatment
STATS <- DF %>%
group_by(Variety,Treatment) %>%
summarise(Rsq=RSQ(Sepal.Length,Sepal.Width)) %>%
# make a label
# one other option is to use stringr::str_wrap in geom_text
mutate(Label=paste("Treat",Treatment,", Rsq=",Rsq))

# set vertical position of rsq
VJUST = ifelse(STATS$Treatment=="A",1.5,3)
# finally the plot function
g + geom_text(data=STATS,aes(x=-Inf,y=+Inf,label=Label),
hjust = -0.1, vjust = VJUST,size=3)

For the last geom_text() call, I allowed the y coordinates of the text to be different by multiplying the Treatment.. You might need to adjust that depending on your plot..

Sample Image

Using annotate to add different annotations to different facets

With annotate, you can't. But by setting up a data.frame and using it as the data source for a geom_text, it is easy (with a few bookkeeping aspects).

d1 + geom_text(data=data.frame(x=0.25, y=1.5e+04, label=1:7, 
color=c("D","E","F","G","H","I","J")),
aes(x,y,label=label), inherit.aes=FALSE)

Sample Image

How to add single annotation to overall ggplot plot and not to each facet

Note, any annotation of that sort will be somewhat hacky and will require careful adjustment of the position of your labels. I think most convenient should be using either patchwork or cowplot for custom annotation. Cowplot offers specific functionality for plot labelling.

library(tidyverse)
library(cowplot)

p <- mtcars %>%
ggplot(aes(x = mpg, y = disp, colour = am)) +
geom_point() +
geom_vline(aes(xintercept = 15),
linetype = "dotted",
colour = "grey20") +
geom_vline(aes(xintercept = 25),
linetype = "dotted",
colour = "grey20") +
facet_wrap(~vs, nrow = 2) +
theme(legend.position = c(x = 0.9, y = 0.5))

# desired behaviour is to position labels using x and y of overall plot area, as per positioning of legend
ggdraw(p) + draw_plot_label(x = c(.2,.6), y = c(.6,.6),
label = c("This label\nshould be on midpoint of y", "This label\nshould be 3/4 up plot"),
hjust = 0, size = 9)

Created on 2021-03-03 by the reprex package (v1.0.0)



Related Topics



Leave a reply



Submit