為了在繪圖上獲得更好的可見性,我在 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
uj5u.com熱心網友回復:
在繪圖之前將分位數標簽放在單獨的資料框中可能更簡單,然后將分位數資料框傳遞給 的data引數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"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/435052.html
