Ggplot by inserting a space before the degree symbol on the axis label

I would like to place the degree symbol on the x axis, but the result has extra space that I cannot get rid of. The text should read "Temperature (* C)", not "Temperature (* C)". I tried two different solutions, but I can not get rid of space.

ggdat<-data.frame(x=rnorm(100),y=rnorm(100)) #neither of these approaches work xlab <- expression(paste('Temperature (',~degree,'C)',sep='')) xlab <- expression('Temperature ('*~degree*C*')') ggplot(data=ggdat,aes(x=x,y=y)) + geom_point() + labs(x=xlab) 

Scatter plot

Any help is appreciated!

Ben

+6
source share
2 answers

Do you need your xlabel for expression? You can try to paste it directly. Something like this works:

 set.seed(1) ggdat<-data.frame(x=rnorm(100),y=rnorm(100)) xlab <- "Temperature (°C)" ggplot(data=ggdat,aes(x=x,y=y)) + geom_point() + labs(x=xlab) 

enter image description here

+6
source

When using expression() the ~ character creates a space, and the * character combines things. See the following code:

 ggdat<-data.frame(x=rnorm(100),y=rnorm(100)) ylab <- expression('stuck'*'together'*'eg:'*mu*'liter') xlab <- expression('sep'~'par'~'at'~'ed'~'eg:'~mu~'liter') ggplot(data=ggdat,aes(x=x,y=y)) + geom_point() + labs(x=xlab, y=ylab) 

Plot

0
source

All Articles