R 中反双曲正弦刻度上分位数的数据标签 (ggplot)
为了更好地了解绘图,我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在绘图之前将分位数标签放入单独的数据框中,然后将分位数数据框传递给
geom_text
的data
参数可能会更简单: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 ofgeom_text
: