I'd like to add an annotation to my geom_density_ridges() plot which inidicates the defined limit for scale_x_continous()
In the example below there is some data to build the plot. The annotation named grob1 leads to the kind of annotation I would like to achieve unsing the value of variable limit_quantile. (The plots shall later on be drawn within a call of one of the apply-familie functions where different variables of my original data set need different plot parameters for a better readability of pthe lots.)
Is there a way to use the value of the variable limit_quantile may even a helper variable from as.character(limit_quantile)
as text input?
Or are ther other ways to achieve an automation friendly creation of a subscipt text in annotations?
## I like to add a subscript text from a variable value
# 2022-DEC-02
df1 <- data.frame(v1 = c(1:100), class = 1, v2 = rnorm(100) )
df2 <- data.frame(v1 = c(1:100), class = 2, v2 = rnorm(100)*5+10 )
df3 <- data.frame(v1 = c(1:100), class = 3, v2 = rnorm(100)*10+25 )
df <- rbind(df1,df2,df3)
limit_quantile <- 0.99
ridge_plot <- ggplot(df, aes(x = v2, y = as.factor(class), group = as.factor(class))) +
geom_density_ridges(stat = "binline", rel_min_height = 0.005 , binwidth = 1, alpha = 0.5,scale = 0.99) +
scale_y_discrete(expand = c(0.01, 0), breaks = c(1:3)) +
scale_x_continuous(expand = c(0.01, 0), limits = quantile(df$v2, c(0, limit_quantile))) +
theme_ridges() +
#ylab("Relative Häufigkeit je Teilbauflächenklasse") +
xlab("Values of v2") +
ylab(NULL) +
theme(axis.title.x = element_text(hjust = 0.5))
grob1 <- grobTree(textGrob(bquote("X-Axis-Limit: Q"[0.95]), x=0.9, y=0.1, hjust=1,
gp=gpar(fontsize = 10.5, fontface="italic")))
ridge_plot_text1 <- ridge_plot + annotation_custom(grob1)
ridge_plot_text1
grob2 <- grobTree(textGrob(bquote("X-Axis-Limit: Q"[limit_quantile]), x=0.9, y=0.1, hjust=1,
gp=gpar(fontsize = 10.5, fontface="italic")))
ridge_plot_text2 <- ridge_plot + annotation_custom(grob2)
ridge_plot_text2
# Credits for annotation: http://www.sthda.com/english/wiki/ggplot2-texts-add-text-annotations-to-a-graph-in-r-software
# Credits for bquote(): https://www.geeksforgeeks.org/superscript-and-subscript-axis-labels-in-ggplot2-in-r/
I tried to pass the value as charachter to the bquote():
limit_quantile_text <- as.character(limit_quantile)
, but that did not change the behaviour if bquote() and textGrob()
grobTree(textGrob(bquote("X-Axis-Limit: Q"[limit_quantile_text ]), x=0.9, y=0.1, hjust=1,
gp=gpar(fontsize = 10.5, fontface="italic")))
In addition I tried the use of get(), which works when defining aes()-elements
#second try
grob2 <- grobTree(textGrob(bquote("X-Axis-Limit: Q"[get(limit_quantile)]), x=0.9, y=0.1, hjust=1,
gp=gpar(fontsize = 10.5, fontface="italic")))
The examples here made the correct bquote() syntax clear to me now.
A variable containing character values/ a string can be used by a .()
notation. So for the example above the code is:
grob2 <- grobTree(textGrob(bquote("X-Axis-Limit: Q"[.(limit_quantile)]), x=0.9,
y=0.1, hjust=1,
gp=gpar(fontsize = 10.5, fontface="italic")))