Superscript before a letter using b quote [duplicate] - r

I am sorry to ask this, but I can't seem to get this expression right to include in the ylab() or xlab() arguments of a ggplot2-object. It needs to read:
"2q (rarefied)". The "2" needs to superscripted, though.
I have fiddled with expression() and paste(), but it seems impossible. Any help is much appreciated.
edit: Some example:
dat<-as.data.frame(matrix(runif(100), 10,10))
names(dat) <- LETTERS[1:10]
library(ggplot2)
ggplot(dat, aes(A,B)) +
geom_point()+
xlab("some title")

All necessary information can be found in help("plotmath").
ggplot(dat, aes(A,B)) +
geom_point()+
xlab(expression({}^2*italic(q)~textstyle(group("(", rarefied, ")"))))

Related

Apparent problem with string substitution when using bquote and atop in mulit-line label

I'm trying to create multi-line label in which the first line is a character string and the second line is an expression. I have searched through the archive and don't see any questions that seem to deal with the same problem. I create the first line using paste and then pass it to atop as shown the example code below:
testIt <- function(){
library(ggplot2)
aPP <- 100.1
firstLine <- paste("Areal Production = ",aPP,sep="")
pbase <- ggplot(mtcars, aes(x=wt, y=mpg))
p <- pbase +
geom_point() +
labs(x=bquote(atop(firstLine, ~(mgC/m^2/day))),
y="Foo")
print(p)
}
This is what I get:
I don't understand why the string I put into the variable named "firstLine" wasn't used and rather the variable name itself is in the label.
Maybe I have the order of the bquote and atop wrong?? Maybe I'm misusing atop? I've tried all sorts of combinations but nothing seems to work for me.
Thanks.
It needs .(objectname) in bquote - according to ?bquote
bquote quotes its argument except that terms wrapped in .() are evaluated in the specified where environment.
testIt <- function(){
library(ggplot2)
aPP <- 100.1
firstLine <- paste("Areal Production = ",aPP,sep="")
pbase <- ggplot(mtcars, aes(x=wt, y=mpg))
p <- pbase +
geom_point() +
labs(x=bquote(atop(.(firstLine), ~(mgC/m^2/day))),
y="Foo")
print(p)
}
-testing
testIt()

R: "Error in facet_grid" - "unused argument"

I am trying to plot some data from my experiment in R using ggplot2, and I am trying to split the graph in two parts using facet_grid().
Here is an MWE I built with the cars dataset:
data(mtcars)
ggplot(data=mtcars, aes(x=mtcars$mpg,y=mtcars$cyl)) +
geom_point()+
facet_grid(rows=mtcars$disp)
I get the following error:
Error in facet_grid(rows = mtcars$disp) :
unused argument (rows = mtcars$disp)
I really have no idea why this is happening. I used this function before, and it worked fine. Would appreciate ideas on how to solve this.
edit:
I accepted the second answer, because it provides some more context, but as I see it, both are equally correct in pointing out that I need to quote the variable name. The actual error was resolved after installig R and all packages again. Now I have a new error, but that is another story. Thanks again!
First, don't explicitly refer to mtcars within the aes() call.
Second, quote the facet argument.
library(ggplot2)
ggplot(data=mtcars, aes(x=mpg,y=cyl)) +
geom_point()+
facet_grid(rows="disp")
Also, consider creating a new variable that collapses disp into fewer values for the facet to be more meaningful & readable.
Here's an example of four arbitrary cut points.
mtcars$disp_cut_4 <- cut(mtcars$disp, breaks=c(0, 200, 300, 400, 500))
ggplot(data=mtcars, aes(x=mpg,y=cyl)) +
geom_point()+
facet_grid(rows="disp_cut_4")
This should do:
ggplot(data=mtcars, aes(mpg, cyl)) +
geom_point()+
facet_grid(rows = "disp")
alternatively:
ggplot(data=mtcars, aes(mpg, cyl)) +
geom_point()+
facet_grid(~disp)

ggplot facet_grid label superscript

I am having trouble with putting subscript in facet_grid label. Here is
an example of the work I have been trying to do.
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=c('N1P1 var','N2P1 var')),
value=rnorm(40))
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species)
Now I want to make by vertical facet label as 'N[1]P[1] var' and so on,
where the numbers in the squared bracket means subscript.
I have consulted some helps in this platform regarding this, but none helped me. I have used expression, bquote as suggested, but nothing worked!
You need to do 2 things:
first, make your labels as plotmath expressions
variable_labels <-
c(expression(paste(N[1],P[1]~var)), expression(paste(N[2],P[1]~var)))
df <- data.frame(species=gl(2,10,labels=c('sp1','sp2')),
age=sample(3:12,40,replace=T),
variable=gl(2,20,labels=variable_labels),
value=rnorm(40))
And then change the default labeller function in facet_grid to "label_parsed"
test.plot <- ggplot(data=df,aes(x=age,y=value)) +
geom_point() +
facet_grid(variable~species, labeller = "label_parsed")

Expression of italics and superscripts in ggplot axis title

I am sorry to ask this, but I can't seem to get this expression right to include in the ylab() or xlab() arguments of a ggplot2-object. It needs to read:
"2q (rarefied)". The "2" needs to superscripted, though.
I have fiddled with expression() and paste(), but it seems impossible. Any help is much appreciated.
edit: Some example:
dat<-as.data.frame(matrix(runif(100), 10,10))
names(dat) <- LETTERS[1:10]
library(ggplot2)
ggplot(dat, aes(A,B)) +
geom_point()+
xlab("some title")
All necessary information can be found in help("plotmath").
ggplot(dat, aes(A,B)) +
geom_point()+
xlab(expression({}^2*italic(q)~textstyle(group("(", rarefied, ")"))))

Refactoring recurring ggplot code

I'm using R and ggplot2 to analyze some statistics from basketball games. I'm new to R and ggplot, and I like the results I'm getting, given my limited experience. But as I go along, I find that my code gets repetitive; which I dislike.
I created several plots similar to this one:
Code:
efgPlot <- ggplot(gmStats, aes(EFGpct, Nrtg)) +
stat_smooth(method = "lm") +
geom_point(aes(colour=plg_ShortName, shape=plg_ShortName)) +
scale_shape_manual(values=as.numeric(gmStats$plg_ShortName))
Only difference between the plots is the x-value; next plot would be:
orPlot <- ggplot(gmStats, aes(ORpct, Nrtg)) +
stat_smooth(method = "lm") + ... # from here all is the same
How could I refactor this, such that I could do something like:
efgPlot <- getPlot(gmStats, EFGpct, Nrtg))
orPlot <- getPlot(gmStats, ORpct, Nrtg))
Update
I think my way of refactoring this isn't really "R-ish" (or ggplot-ish if you will); based on baptiste's comment below, I solved this without refactoring anything into a function; see my answer below.
The key to this sort of thing is using aes_string rather than aes (untested, of course):
getPlot <- function(data,xvar,yvar){
p <- ggplot(data, aes_string(x = xvar, y = yvar)) +
stat_smooth(method = "lm") +
geom_point(aes(colour=plg_ShortName, shape=plg_ShortName)) +
scale_shape_manual(values=as.numeric(data$plg_ShortName))
print(p)
invisible(p)
}
aes_string allows you to pass variable names as strings, rather than expressions, which is more convenient when writing functions. Of course, you may not want to hard code to color and shape scales, in which case you could use aes_string again for those.
Although Joran's answer helpt me a lot (and he accurately answers my question), I eventually solved this according to baptiste's suggestion:
# get the variablesI need from the stats data frame:
forPlot <- gmStats[c("wed_ID","Nrtg","EFGpct","ORpct","TOpct","FTTpct",
"plg_ShortName","Home")]
# melt to long format:
forPlot.m <- melt(forPlot, id=c("wed_ID", "plg_ShortName", "Home","Nrtg"))
# use fact wrap to create 4 plots:
p <- ggplot(forPlot.m, aes(value, Nrtg)) +
geom_point(aes(shape=plg_ShortName, colour=plg_ShortName)) +
scale_shape_manual(values=as.numeric(forPlot.m$plg_ShortName)) +
stat_smooth(method="lm") +
facet_wrap(~variable,scales="free")
Which gives me:

Resources