R 中反双曲正弦刻度上分位数的数据标签 (ggplot)

发布于 2025-01-10 20:03:55 字数 2098 浏览 0 评论 0原文

为了更好地了解绘图,我将 ggplot 中的标度转换为反双曲正弦(伪负对数标度),并使用箱线图和小提琴图。我无法为该比例的分位数添加数据标签。每当我尝试以下脚本时,显示的数字与实际分位数值不匹配。如果有人能帮助我,我将不胜感激。您可以在此处访问示例数据:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2025-01-17 20:03:55

在绘图之前将分位数标签放入单独的数据框中,然后将分位数数据框传递给 geom_textdata 参数可能会更简单:

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"))

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"))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文