Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: ggplot2 minus sign instead of hyphens (-) in ylim axis

Tags:

r

ggplot2

Im trying to change in ggplot2 ylim axis to minus signs (−) instead of hyphens (-). I tried many different options but still it does not show − sign and numbers in a plot..

plot <- ggplot(df_1, aes(x=t, y=RMS, color=Outcome, 
                                  fill=Outcome, shape=Outcome)) 
plot <- plot + scale_y_continuous(limits=c(-0.02, 0.02),
                                      breaks=seq(-0.02, 0.02, by=0.01),
                                      labels=c("−0.02", "−0.01", "0", "0.01", "0.02"))

like image 805
Newcomer Avatar asked Oct 20 '25 00:10

Newcomer


1 Answers

You could use the unicode glyph \u2212. Just swap it in using a custom labelling function inside scale_y_continuous

ggplot(data.frame(x = letters[1:5], y = seq(-0.2, 0.2, 0.1)), aes(x, y)) +
  geom_point() +
  theme_gray(base_size = 20) +
  scale_y_continuous(breaks = seq(-0.2, 0.2, 0.1), 
                     labels = ~sub("-", "\u2212", .x)) 

enter image description here

like image 162
Allan Cameron Avatar answered Oct 22 '25 06:10

Allan Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!