R ylab line modified with italic - r

I have trouble making my plots looking similar because my labels (both xlab and ylab) move if I use italic. Consider the following short example:
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab=expression(italic(p)~"-value"))
The problem is that "p-value" is slightly nearest from the axis than "p-value". I believe it's because the p tail defines the writing line which is considered different without italic. How can I fix that easily?
I frequently use both strings and expressions for my plots and that would be complicated to use mtext (with the line argument) to manage each label for each plot.

I found a workaround playing with unicode characters.
p is \U1D631 and P is \U1D617
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab="\U1D631-value")
This solved my problem.

Related

Formatting line breaks when using superscripts in tmap legend

I am trying to create a map with the tmap showing the population density. Therefore, I want the legend title of the map to be "Population density in n\ [people per km^2]". This map is the closest I have gotten to my desired final result.
Now, as you see, I manage to insert a line break but somehow the last "]" jumps all to the right and the title is cut half off in the first line. I think the latter is a problem I can solve by myself, but I don't find a solution to get rid off the former problem.
To create this map, I used the following code
tm_polygons("Pop_dens",title=expression(paste("Population density \n[people per km"^"2","]")),breaks=c(0,50,100,500,1000,3000,5000,25000),border.alpha = 0)+
tm_shape(border_plateau)+
tm_polygons(alpha=0)+
tm_scale_bar(breaks=c(0,10,20,50,100),position = c(0.56,0.04))+
tm_compass(position = c(0.4,0.04))+
tm_legend(legend.position=c(0.71,0.11))
How can I remove the space (ergo make it left-binding)?
I am familiar with making new lines in the legend using \n and pasting expressions using expression(paste(..)), but combining both doesn't seem to work.
One option would be to use:
title=expression(atop("Population density","(people per km"^2*")"))
Although the space between lines may be excessively large
I faced the same problem and solved it by using HTML tags with the package ‘htmltools’:
title=paste('Line 1', br('Line 2'))

Both custom tickmarks AND custom fontface in ggplot2

Apologies if this is straightforward or has already been answered, but I've searched high and low and haven't been able to find a solution anywhere.
I'm using ggplot2, trying to (1) specify custom tick marks to an axis which involve subscripts, and (2) trying to specify that some of these tick marks should be bold and others italic. I know how to do (1) and (2) separately, but when I try to do both together, only (1) succeeds.
y.labels<-c(expression(A[Subscript]),expression(B^Superscript))
y.face<-c("bold","italic")
testdf<-data.frame(x=rnorm(100),y=1:2)
Bold/italics don't appear when I supply custom labels:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=y.labels)
theme(axis.text.y=element_text(face=y.face))
But when I let the labels take their default values, bold/italics do appear:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
theme(axis.text.y=element_text(face=y.face))
As far as I can tell, the problem doesn't have anything to do with subscripting/superscripting,
even though that is my aim, here (and which is why I'm passing the tick marks directly, rather than creating a factor variable with the desired labels, which does work). This doesn't work, either:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=c("Custom1","Custom2"))+
theme(axis.text.y=element_text(face=y.face))
If anyone can explain what's going on, here, and provide a solution whereby I can do both, I'd be much obliged. Thanks.
plotmath doesn't honour fontface, you want to use bold() or italic() in your expressions,
y.labels<-c(expression(bold(A)[Subscript]),expression(italic(B)^Superscript))
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=y.labels)

Spacing in axis label when using expression(paste(...))

Consider the following example:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g ',CO[2]~m^{-2}~h^{-1},')')))
Obviously I want a full space between "g" and "CO", but for some reason I get a smaller (with some labels even zero) space in the graph label.
The problem is even more obvious, if I do it like this:
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux (g C',O[2]~m^{-2}~h^{-1},')')))
Am I doing something wrong? Is there a way to fix the spacing or even a better way to create labels with lots of sub/superscripts and greek letters?
In all likelihood you are getting a typographically correct "space", in the font your OS uses for non-serif display. You can change fonts or you can insert blank space that is sufficient to hold a particular character string with plotmath phantom():
plot(c(2,4,6)~c(1,2,3),xlab="x",
ylab=expression(paste('flux',phantom(x),'(g ',CO[2]~m^{-2}~h^{-1},')')))
Or as #baptiste points out this can be done without plomath paste using ordinary plotmath separators because a tilde in a true R expression gets handled as a "space":
ylab=expression(flux*phantom(x)*(g~CO[2]~m^{-2}~h^{-1})))

equivalent to MatLab "bar" function in R?

Is there an function in R that does the same job as Matlab's "bar" function?
R does have a "barplot" function in the library graphics, however, it is not the same.
The Matlab bar(X,Y) (verbatim excerpt from MATLAB documentation) "draws a bar for each element in Y at locations specified in X, where X is a vector defining the x-axis intervals for the vertical bars." (emphasis mine)
However, the R barplot function does not allow one to specify locations.
Perhaps there is a method in ggplot2 that supports this? I am only able to find standard bar charts in ggplot2.
No, barplot is not the same as bar, but you should read the whole help. You can do many things to position the bars. The first is simply their order in Y. You could insert spaces if you wish (additional 0s). If you have X and Y then sort Y on X (Y[order(X)]) and plot it. If you need to change positions use the "space" and "width" arguments. It's not as straightforward as specifying X values I suppose but it's definitely more useful in most situations. Generally what you want to adjust is widths of bars and spaces between bars. Their position on the X-axis should be arbitrary. If the position on the X-axis is really meaningful then you should be using line plots, not bar graphs.
In R:
barplot(rbind(1:10, 2:11), beside=T, names.arg=1:10)
In MATLAB:
>> bar(1:10, [(1:10)' (2:11)'])
Read up on par . Then observe, for example:
x<-c(1,2,4,5,6)
y<-c(3,4,3,4,2)
plot(x,y,type='h',lwd=6)
Edit: yes, I know this doesn't (yet) plot multiple data sets, but I would hope you can see simple ways to make that happen, with spacings, colors, etc. specified to your exact liking :-)
Sounds vaguely like the R stepfun. On the other hand one would need to know what "draws a bar" means before saying it is not the same as barplot(..., horiz=TRUE) One would, of course, need to examine some more detailed evidence such as data and plots before arriving at a conclusion, however. #John Colby should be congratulated for adding some specificity to the discussion. The axis function is probably what Quant Guy needs education regarding.

Plots without titles/labels in R

In R is there any way to produce plots which have no title and which use the space the title would otherwise have taken up?
In plot(), main, sub, xlab, and ylab all default to NULL, but this just leaves blank space where they would have been, ditto for setting them to ''. It would be nice if not including them meant that the entire plot space was utilized rather than leaving extra empty space on the edges. This is all especially relevant in printing plots to file devices like pdf(), png(), etc.
See tip 7 about adjusting the margins.
Excerpt:
To remove the space reserved for labels, use par(mar=...). For example
png(file="notitle.png",width=400, height=350)
par(mar=c(5,3,2,2)+0.1)
hist(rnorm(100),ylab=NULL,main=NULL)
dev.off()
If you're willing to entertain an alternate plotting package, ggplot2 does this automatically when you set xlab/ylab to NULL (and there is no plot title/main by default). For simple plots, just require(ggplot2) and replace plot by qplot.
Really, ggplot2 is the most fun I've had with plotting in years and I can't resist the opportunity to evangelize it to everyone I meet. :-)
With lattice, it's just a matter of setting the xlab, ylab, and main arguments to NULL:
library(lattice)
bwplot(rnorm(100),xlab=NULL,ylab=NULL,main=NULL)
plot(anything, main=NULL)
Still works.
I usually use
par(mar=c(1,1,1,1))
when I keep the border to a minimum.

Resources