Search code examples
rggplot2inverse-transform

Data Labels for Quantiles on an Inverse Hyperbolic Sine Scale in R (ggplot)


For better visibility on a plot, I transformed the scale to inverse hyperbolic sine (pseudo negative logarithmic scale) in ggplot and used both box and violin plots. I am not being able to add the data labels for the quantiles on that scale. Whenever, I am trying the following script, the numbers showing up do not match the actual quantile values. I would greatly appreciate if someone can help me with that. The sample data can be accessed here:

https://drive.google.com/file/d/1WTjiV1Q3HqlMXAjdrDSdcskc3uXxxRMt/view?usp=sharing

    library(scales)
asinh_trans <- scales::trans_new(
  "inverse_hyperbolic_sine",
  transform = function(x) {asinh(x)},
  inverse = function(x) {sinh(x)}
)

XData <- as.data.frame(read.csv("Sample.csv", header = TRUE))

XDataS1 <- subset.data.frame(XData, XData$Setup == "ND2" & XData$SensorLocation == "Head")

CheckData <- fivenum(XDataS1$Strain)
CheckData

NPlot <- ggplot(XData, aes(fill = `Setup`, x = `SensorLocation`, y = `Strain`)) + geom_violin(trim = TRUE, fill = "lightgray") +
  labs(x = "Sensor Location", y = "Strain (\u03BC\u03B5)\n- inverse hyperbolic sine scale") +
  geom_boxplot(width=0.2) +
#This where I tried sinh(asinh(..y..)) and ln(..y.. + sqrt(1 + (..y..^2))) to add the quantile data labels
  stat_summary(geom="text", fun=fivenum,
               aes(label=sprintf("%.1f", log(..y.. + sqrt(1 + (..y..^2)))), color=factor(`Setup`)),
               position=position_nudge(x=0.33), size=3.5) +
  theme_bw() +
  #coord_cartesian(ylim = quantile(XData$Bstrain, c(0, 1))) +
  scale_y_continuous(trans = asinh_trans, breaks = c(-1000, -100, -10, -1, -0.1)) +
  theme(axis.title = element_text(size = 12)) +
  theme(axis.text = element_text(size = 12, color = "black")) +
  theme(axis.title.x = element_text(vjust = -3)) +
  theme(axis.text.x = element_text(vjust = -1.5)) +
  theme(panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "dark grey"), 
        panel.grid.minor = element_line(size = 0.5, linetype = 'dashed', color = "grey"),
        panel.background = element_rect(colour = "black", size=1)) +
  theme(legend.position = "bottom") +
    guides(fill=guide_legend(title="Test Setup"),
           colour = guide_legend(title="Test Setup"))

####
NPlot

Solution

  • It may be simpler to put your quantile labels in a separate dataframe prior to plotting, then pass the quantile dataframe to the data argument of geom_text:

    library(tidyverse)
    library(scales)
    
    XDataFiveNum <- XData %>% 
      group_by(SensorLocation, Setup) %>% 
      summarize(Strain = fivenum(Strain), .groups = "drop")
    
    NPlot <- ggplot(XData, aes(fill = `Setup`, x = `SensorLocation`, y = `Strain`)) + 
      geom_violin(trim = TRUE, fill = "lightgray") +
      labs(x = "Sensor Location", y = "Strain (\u03BC\u03B5)\n- inverse hyperbolic sine scale") +
      geom_boxplot(width=0.2) +
      geom_text(
        data = XDataFiveNum, 
        aes(label = sprintf("%.1f", Strain), color = Setup),
        position=position_nudge(x=0.33), 
        size=3.5
      ) +
      theme_bw() +
      #coord_cartesian(ylim = quantile(XData$Bstrain, c(0, 1))) +
      scale_y_continuous(trans = asinh_trans, breaks = c(-1000, -100, -10, -1, -0.1)) +
      theme(axis.title = element_text(size = 12)) +
      theme(axis.text = element_text(size = 12, color = "black")) +
      theme(axis.title.x = element_text(vjust = -3)) +
      theme(axis.text.x = element_text(vjust = -1.5)) +
      theme(panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "dark grey"), 
            panel.grid.minor = element_line(size = 0.5, linetype = 'dashed', color = "grey"),
            panel.background = element_rect(colour = "black", size=1)) +
      theme(legend.position = "bottom") +
      guides(fill=guide_legend(title="Test Setup"),
             colour = guide_legend(title="Test Setup"))