Expression of italics and superscripts in ggplot axis title - 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

How can I partly italicise a facet title / strip text in ggplot using ggtext

I want to have an expression partly italicised in a facet label in ggplot2, but I don't seem to be able to do it the way I have seen it here around.
I tried using element_markdown() as I have seen here around like this:
levels(iris$Species) <- c("look at *I. setosa*",
"and also at *I. versicolor*",
"finally, at *I. virginica*")
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
facet_wrap(~Species) +
theme(strip.text = ggtext::element_markdown())
Am I missing something?! The "*" are gone but the species are not italicised! Thank You all!
I had to update all packages related to ggplot (I updated tidyverse ), as well as ggtext (to 0.1.2), gridtext (to 0.1.5) and R itself (to 4.2.2) . Now it works :-)
Thanks to #stefan and #MrFlick

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)

Superscript before a letter using b quote [duplicate]

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, ")"))))

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")

overlapping y-scales in facet (scale="free")

I've been learning ggplot in the last few weeks. Generally, I'm getting things done (slowly though), but now I'm stuck. I created the following facetted plot: http://dl.dropbox.com/u/7752237/example_bad_y_scales.pdf
Faceting is done by
pl <- pl + facet_wrap(~sci_name,ncol=1,scale="free")
The Problem: Numbers on the y-scale don't look good, especially the scales that go from 0-70 (numbers overlapping).
I'd like the somehow change the number of breaks on the y-scale (to let's say just 1 or 2 breaks). Does anybody maybe have an idea how to do that? Any help would be very much appreciated. :)
PS: I didn't include a minimal example because I think it wouldn't help much to solve that specific problem.
Edit after Kohskes answer:
Hi Kohske,
Wow, that was a really fast answer, thanks! However, I think it doesn't work well with facetted plots. Look at
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
p <- p + facet_wrap(~gear,ncol=1,scale="free")
On the y-scale, it gives 3 breaks in the middle plot and 8 breaks in the lower plot… not very consistent (but at least not overlapping as in my example).
p2 <- p + scale_y_continuous(breaks=c(15,30),minor_breaks=c(10,20,25))
isn't really good neither: two major ticks on lower plots, only one in middle and upper plot.
When having scales with bigger differences than in mtcars, the result would be even less satisfying. Any other ideas? ;)
Edit after Kohskes edit:
Hi, I can't see how to implement this. Searching for ggplot and input_break on google yielded only 10 results, none of them did help.
I tried
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
p <- p + facet_wrap(~gear,ncol=1,scale="free")
p$input_breaks<-function(., range) {
pretty(range, n=3)
}
print(p)
However, I can't see any effects in the graph (tried for n=1, 3, 15). Could you describe how to implement this on the mtcars example? Thanks!
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
dev.new(height=1)
print(p)
dev.new(height=1)
p <- p + scale_y_continuous(breaks=c(15,30),minor_breaks=c(10,20,25))
print(p)
the trick is scale_y_continuous and you can specify the breaks and minor breaks in it.
edited:
probably you cannot specify the breaks separately for each facet.
one workaround is to control the prettiness of the breaks by:
Trans$input_breaks<-function(., range) {
pretty(range, n=3)
}
print(p)
changing the "n=3" yields different prettiness.
edited again:
here is full example:
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))+geom_point()
Trans$input_breaks<-function(., range) {
    pretty(range, n=100)
}
print(p)
in this case, probably you can see a hundred of ticks.
by changing n=100, you can custom it.
note that this has side-effect. all plots after this has same number of ticks, and also x and y axis have the same number of ticks.

Resources