subscripts in facet labels when subsetting with geom_text

1,033 views
Skip to first unread message

Susan Kamp

unread,
Jun 8, 2017, 7:34:08 PM6/8/17
to ggp...@googlegroups.com

Hello,

This may reek of an ”asked and answered” post but please bear with me – after hours of searching and failed attempts, have not found a solution. Need to label facets with mix of regular text and subscript—there are clearly ways to do this using calls to “annotate”  or ”expression.” But for this plot need to label facets for a subset of the data – to date I’ve only been able to do this using geom_text.

 

#  data file attached (df1.csv). “id” variables created to allow crisp (vs. ugly overwritten) facet labels. Even if there is a more elegant way to do this -- in which case please advise! -- for other reasons it’s necessary to subset these data

 

Main plot call:

 > p1 <- ggplot(df1, aes(SC, depth))

                    + geom_point()
                    + facet_wrap(~well, scales="free")
                    + geom_path()
                    + scale_y_reverse()
                    + geom_segment(data=df1, aes(x=-Inf, xend=-Inf, y=tscr, yend=bscr))
                    + labs(x="SC (\u03BCS/cm)", y="Depth (bgs)", title="SC profiles in well subset; left vertical line denotes screened interval (SI)", subtitle=expression(paste("Desired facet label format: ", CV[SI]," = …")))
 
# line of code I’m trying to figure out…
p1 + geom_text(data=df1[df1$interval=="scr" & df1$id2=="1",], aes(x = Inf, y = -Inf, label = paste("CVSI =", cv2)), size=3, vjust=2, hjust=1.1)

 

How change CVSI to CVSI  AND retain capacity to subset the data??  Suspect there’s an easy answer, but this one eludes me.

 

many thanks,

Susan

df1.csv

Brandon Hurr

unread,
Jun 8, 2017, 8:16:57 PM6/8/17
to Susan Kamp, ggplot2

On Thu, Jun 8, 2017 at 3:33 PM, Susan Kamp <ska...@gmail.com> wrote:

ggplot(df1, aes(SC, depth))

                    + geom_point()
                    + facet_wrap(~well, scales="free")
                    + geom_path()
                    + scale_y_reverse()
                    + geom_segment(data=df1, aes(x=-Inf, xend=-Inf, y=tscr, yend=bscr))
                    + labs(x="SC (\u03BCS/cm)", y="Depth (bgs)", title="SC profiles in well subset; left vertical line denotes screened interval (SI)", subtitle=expression(paste("Desired facet label format: ", CV[SI]," = …")))

Susan, 

I'm not 100% sure on this, but does this labeler do what you want?

library(tidyverse)

df1 <- read_csv("~/Downloads/df1.csv")

faclabs <- levels(as.factor(df1$well))

ggplot(df1, aes(SC, depth)) +
geom_point() +
facet_wrap(~well, scales="free", labeller=label_bquote(.(well)~CV[SI]))+
geom_path() +
scale_y_reverse() +
geom_segment(data=df1, aes(x=-Inf, xend=-Inf, y=tscr, yend=bscr)) +
labs(x="SC (\u03BCS/cm)", y="Depth (bgs)", title="SC profiles in well subset; left vertical line denotes screened interval (SI)", subtitle=expression(paste("Desired facet label format: ", CV[SI]," = …")))


See last answer here and ?facet_wrap and ?label_bquote for what it's doing and why. 

HTH,
B

Susan Kamp

unread,
Jun 9, 2017, 1:09:09 PM6/9/17
to Brandon Hurr, ggplot2
Brandon, thank you for your response. I apologize for not concisely defining the problem in yesterday's post - it's the annotations within the facets (not the facet labels) where I'd like the subscript formatting. Which isn't a problem if I don't subset the data. But, using yesterday's example, how do I achieve this (subscript of "SI") in this final line of code?
Have tried to use various permutations of "expression" to no avail.

p1 + geom_text(data=df1[df1$interval=="scr" & df1$id2==1,], aes(x=Inf, y=-Inf, label = paste("CVSI =", cv2)), size=3, vjust=2, hjust=1.1)
hope this makes more sense!
Best, Susan

Brandon Hurr

unread,
Jun 12, 2017, 7:02:21 PM6/12/17
to Susan Kamp, ggplot2

On Fri, Jun 9, 2017 at 10:08 AM, Susan Kamp <ska...@gmail.com> wrote:
geom_text(data=df1[df1$interval=="scr" & df1$id2==1,], aes(x=Inf, y=-Inf, label = paste("CVSI =", cv2)), size=3, vjust=2, hjust=1.1)

Susan, 

Maybe this? 

library(tidyverse)

df1 <- read_csv("~/Downloads/df1.csv")

# isolate the labels
dflabels <- 
df1 %>%
select(well, cv2) %>%
group_by(well) %>%
slice(1) %>%
mutate(labels = paste0("CV[SI] == ", cv2))

ggplot(df1, aes(SC, depth)) +
geom_point() +
facet_wrap(~well, scales="free", labeller=label_bquote(.(well)~CV[SI]))+
geom_path() +
scale_y_reverse() +
geom_segment(data=df1, aes(x=-Inf, xend=-Inf, y=tscr, yend=bscr)) +
labs(x="SC (\u03BCS/cm)",
y="Depth (bgs)",
title="SC profiles in well subset; left vertical line denotes screened interval (SI)",
subtitle=expression(paste("Desired facet label format: ", CV[SI]," = …"))
) +
geom_text(data=dflabels, aes(x=Inf, y=-Inf, label = labels), size=3, vjust=2, hjust=1.1, parse = TRUE) #parse it https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html

HTH,
B

Susan Kamp

unread,
Jun 13, 2017, 9:48:25 AM6/13/17
to Brandon Hurr, ggplot2
Brandon,

Yes- that works! :-)  Added another filter (interval=="scr") to dflabels pipe chain and that solved it - MUCH better than my previously clunky way of subsetting the dataframe and avoiding overwritten labels.
Wasn't needing special formatting of facet strip text (poor problem definition on my part), but your labeller recommendation helped with another unrelated long-standing issue. So thank you on several counts -- you helped a lot!

~cheers,
Susan 


Reply all
Reply to author
Forward
0 new messages