Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot axis with midline decimal

I need to output ggplot figures with midline decimals for my y axis.

The unicode \U00B7 allows to get midline decimal dots but I don’t know how to get an automated result without some unclean solutions as I show in my attempt.

My code is not reproducible for examples where the numbers don’t have the same sign (positive vs negative), or the same number of digits (2 digits vs 1 digit for example).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Reproducible example

##### Import library
library(ggplot2)


##### Initiating data
### y-axis labels
yaxisLabels <- format(seq(3, 7, 0.5), nsmall=1)

### Dataset
set.seed(1)
dftmp <- data.frame(values=rnorm(100, 5, 0.5),
                    group=rep(c("A", "B"), each=50))


##### Initiating plot
### Original plot
ggplot(data=dftmp, aes(y=values, x=group)) + 
  geom_boxplot() + 
  scale_y_continuous(limits=c(3, 7), breaks=seq(3, 7, 0.5)) + 
  theme_minimal()

### My attempt
ggplot(data=dftmp, aes(y=values, x=group)) + 
  geom_boxplot() + 
  scale_y_continuous(limits=c(3, 7), breaks=seq(3, 7, 0.5), 
                     labels=paste0(substr(yaxisLabels, 1, 1), "\U00B7", substr(yaxisLabels, 3, 3))) + 
  theme_minimal()

Original plot
enter image description here

My attempt
enter image description here

### Mikko Martila solution
ggplot(data=dftmp, aes(y=values, x=group)) + 
  geom_boxplot() + 
  scale_y_continuous(limits=c(3, 7), breaks=seq(3, 7, 0.5),
                     labels=scales::label_number(decimal.mark="\U00B7")) + 
  theme_minimal()

>Solution :

The labels argument can be a function. scales::label_number(decimal.mark = "\U00B7") would do the trick.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading