Home > OS >  reduce a string in r
reduce a string in r

Time:11-24

I would like to reduce a string size (i.e., y and x) in one character for the axis created by ``ggplot` as picture below

Anyone has suggestions for me, please?

enter image description here

CodePudding user response:

One potential solution is to use expression(), e.g.

library(tidyverse)
library(palmerpenguins)

penguins %>%
  na.omit() %>%
  ggplot(., aes(x = bill_length_mm, y = body_mass_g))  
  geom_point()  
  theme_classic(base_size = 16)  
  ylab(expression(P[y]))  
  xlab(expression(P[x]))  
  theme(axis.title = element_text(hjust = 1),
        axis.line = element_line(arrow = arrow(type='closed', length = unit(12,'pt'))))

Created on 2021-11-24 by the reprex package (v2.0.1)

Another potential solution is to use unicode symbols (e.g. from https://unicode-table.com/en/sets/superscript-and-subscript-letters/):

penguins %>%
  na.omit() %>%
  ggplot(., aes(x = bill_length_mm, y = body_mass_g))  
  geom_point()  
  theme_classic(base_size = 16)  
  xlab(paste("P", "\u1D6A", sep = ""))  
  ylab(paste("P", "\u1D67", sep = ""))  
  theme(axis.title = element_text(hjust = 1),
        axis.line = element_line(arrow = arrow(type='closed', length = unit(12,'pt'))))

Created on 2021-11-24 by the reprex package (v2.0.1)

  • Related