plot axis label formatting units in R - r

I'm plotting photosynthetically active radiation which has units of umol/m2/s
I can get R to plot "PAR umol m-2" with the u as mu and -2 in superscript using:
bp_m + ylab(expression(paste('PAR ',mu,'mol m')^-2))
where bpm is the rest of the plot, but I can't get it to add the s^-1, I've tried various forms of expressions, paste etc

Finally got there - R seems to have so many different ways of doing the same thing, taking me ages to get my head around the syntax etc
bp_m+labs(y = ~paste("PAR ", mu, "mol m"^-2,"s"^-1))
bp_m+ylab(~paste("PAR ", mu, "mol m"^-2,"s"^-1)))
both work

Related

R: How does stat_density_ridges modify and plot data?

<Disclaimer(s) - (1) This is my first post, so please be gentle, specifically regarding formatting and (2) I did try to dig as much as I could on this topic before posting the question here>
I have a simple data vector containing returns of 40 portfolios on the same day:
Year Return
Now -17.39862061
Now -12.98954582
Now -12.98954582
Now -12.86928749
Now -12.37044334
Now -11.07007504
Now -10.68971539
Now -10.07578182
Now -9.984867096
Now -8.764036179
Now -8.698093414
Now -8.594026566
Now -8.193638802
Now -7.818599701
Now -7.622627735
Now -7.535216808
Now -7.391239166
Now -7.331315517
Now -5.58059597
Now -5.579797268
Now -4.525201797
Now -3.735909224
Now -2.687532902
Now -2.65363884
Now -2.177522898
Now -1.977644682
Now -1.353205681
Now -0.042584345
Now 0.096564181
Now 0.275416046
Now 0.638839543
Now 1.959529042
Now 3.715519428
Now 4.842819691
Now 5.475946426
Now 6.380955219
Now 6.535937309
Now 8.421762466
Now 8.556800842
Now 10.39185524
I am trying to plot these returns to compare versus other days (so the rest of my history e.g.). I tried to use stat_density_ridges as per the code block below
ggplot(data = data.plot, aes(x = Return, y = Year, fill = factor(..quantile..))) +
stat_density_ridges(geom = "density_ridges_gradient",calc_ecdf = TRUE,
quantiles = c(0.025, 0.5, 0.975),
quantile_lines = TRUE)
As you can see - the "year" in this case is the same i.e. there is no height parameter, yet I get a nice ridg(y) chart. While the chart is beautiful to behold, and very very awesome, I am at a loss to determine how the plotting function is computing the density in this case, specially the height.
This is the output chart I get (I have omitted the formatting code here since it doesn't make a difference to my question):
Portfolio Return Distribution Plots - US versus Europe
I tried digging into the code of the function itself, but came up with a total blank. The documentation didn't help (except perhaps give me a hint that the function plots continous distributions).
Any help, or guidance, or even a nudge in the right direction would be extremely helpful.

When using the units package, how can I get the unit labels on the axes to render as Greek letters?

The units package in R is very useful in dealing with unit conversions and can be used for plotting in base plot and using the ggplot + ggforce combo. However, in the current form, units that should include Greek letters are not typeset correctly. Is there a way to have Greek letters in units displayed on the graph using the units package?
library(units)
library(ggplot2)
library(ggforce)
df = cars
df$Distance = set_units(df$dist, ft)/1000
df$Speed = set_units(df$speed, mph)
qplot(x=Speed, y=Distance, data=df) +
scale_y_unit(unit = "um")
# The Y-axis labels uses the latin u instead of the Greek mu
This answer shows how to use greek letters in ggplot. mu can be added to the y axis label by using expression
ggplot(df, aes(x=Speed, y=Distance)) +
geom_point() +
scale_y_unit(name = expression(paste("Distance [", mu, "m]")), unit = "um")
However, the origin text [um] in the label can not be removed. scale_*_unit add [<unit>] to the axis label by default.
It is now possible to insert Greek letters directly into R code which should allow one to have Greek letters on the axes.
qplot(x=Speed, y=Distance, data=df) +
scale_y_unit(unit = "μm")
This did not work for me with R version 3.5.1 (2018-07-02) (see error message below). In this case, upgrade your R installation.
Error: In ‘µm’, ‘µm’ is not recognized by udunits.
See a table of valid unit symbols and names with valid_udunits().
Add custom user-defined units with install_symbolic_unit().
Credits to Edzer Pebesma for finding the solution (see https://github.com/r-quantities/units/issues/238)

Problem in Greek letter on R plots using Rqtl package

I am trying to plot a QTL graph with a greek symbole in my legend like this :
LOD(π,μ)
Here is my code :
plot(outFW.2p14a,lodcolumn=1:3, col = c("black","blue","green"),
ylab="LOD scores for 2014",ylim=c(0,5), main="B")
legend("topright",
legend=c("LOD(π,μ)", "LOD(π)","LOD(μ)"," ",
"LOD threshold (μ)","LOD threshold (π)","LOD threshold (π,μ)"),
col=c("black","blue","green","white","red","red","red"),
lty=c(1,1,1,1,2,3), cex=1.2)
When I use this code I get the right plot but the legend where the greek symbole π appears is wrong and show a 'p' instead...Just like this :
Legend example with error and for μ it works just fine !
I am using genetic data and tables from Rqtl package but I don't think that the problem comes from there but more from the plot function. Do you have any idea why μ works and not π ?
I would appreciate some help for this because I have tried many things and I can not get it right. I need it for a publication in a scientific journal so it needs to be right.
Best regards,
Diana
I got the symbol correctly if i use your code..
COLS=c("black","blue","green","white","red","red","red")
LABEL1 = c("LOD(π,μ)", "LOD(π)","LOD(μ)"," ",
"LOD threshold (μ)","LOD threshold (π)","LOD threshold (π,μ)")
plot(NULL,ylab="LOD scores for 2014",ylim=c(0,5), main="B",xlim=c(0,5))
legend("topright",
legend=LABEL1,
col=COLS,
lty=c(1,1,1,1,2,3), cex=1.2)
Might be something weird with your keyboard, try this, where you call the symbol through expression:
LABEL2 = c(expression(paste("LOD(",pi,",",mu,")")),
expression(paste("LOD(",pi,")")),
expression(paste("LOD(",mu,")")),
expression(paste("LOD threshold(",pi,",",mu,,")")),
expression(paste("LOD threshold (",pi,")")),
expression(paste("LOD threshold (",mu,")"))
)
plot(NULL,ylab="LOD scores for 2014",ylim=c(0,5), main="B",xlim=c(0,5))
legend("topright",
legend=LABEL2,
col=COLS,
lty=c(1,1,1,1,2,3), cex=1.2)

Add an equation into scatter plots in R

I have a little trouble to add the equations into scatter plots using "legend"
A simple example is as follow:
plot(1:100)
# The below code can work if I add "= 0.1234" directly.
legend(locator(1), expression(paste("Linear model: ", R^2, "= 0.1234",sep="")),
text.col= "black",cex=1,bty="n")
# The below code cannot work if I add the "ps".
ps = "= 0.1234"
legend(locator(1), expression(paste("Linear model: ", R^2, ps, sep="")),
text.col= "red",cex=1,bty="n")
The real issue I have is a little complex with this example.
So how should I revise this code?
The "ps"-object is being handled as an expression, i.e. not being evaluated. To get around this use bquote and .()
legend(locator(1), legend= bquote("Linear model: "* R^2*.(ps)),
text.col= "red",cex=1,bty="n")
BTW the first version would be more compactly represented without the paste:
legend(locator(1), expression(Linear~model*":"~ R^2 == 0.1234),
text.col= "black",cex=1,bty="n")
The only thing needing to be quoted is the semi-colon.

Concatenate a list for plot labels

I want to create intervals (discretize/bin) of continuous variables to plot a choropleth map using ggplot. After reading various threads, I decided to use cut and quantile to eliminate the problems of: a) manually creating bins, and b) taking care of dominant states (otherwise, I had to manually to create bins and see the map and readjust the bins).
However, I am facing another problem now. Intervals coming out of cut are hardly pretty. So, I am trying to follow this example and this example to come up with my pretty labels.
Here is my list:
x <- seq(1,50)
Rounded quantiles:
qs_x <- round(quantile(x, probs=c(seq(0,0.8,by=0.2),0.9)))
which results:
0% 20% 40% 60% 80% 90%
1 11 21 30 40 45
Using these cuts, I want to come up with these labels:
1-11, 12-21, 22-30, 31-40, 41-45, 45+
I am sure there is an easy solution to convert a list using some apply function, but I am not well-versed with those functions.
Help appreciated.
A 3-liner produces the output you want, without using apply.
labels <- paste(qs_x+1, qs_x[-1], sep="-")
labels[1] <- paste(qs_x[1], qs_x[2], sep="-")
labels[length(labels)] <- paste(tail(qs_x, 1), "+", sep = "")
The first line constructs labels of the form (x1 + 1) - x2, the second line fixes the first label, and the third line fixes the last label. Here is the output
> labels
[1] "1-11" "12-21" "22-30" "31-40" "41-45" "45+"

Resources