You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! So I would like to manipulate some labels I have added to a stacked bar plot through repel. The problem is, that some names will have to be in italics, while others don't so I am trying to specify each and I can not succeed so far.
I previously created this plot with the normal legend where it worked:
I have modified the code to have clear labels indicated with repel:
###function to stack the repels (thank you genius!!): https://github.com/slowkow/ggrepel/issues/161
position_stack_and_nudge <- function(x = 0, y = 0, vjust = 1, reverse = FALSE) {
ggproto(NULL, PositionStackAndNudge,
x = x,
y = y,
vjust = vjust,
reverse = reverse
)
}
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @noRd
PositionStackAndNudge <- ggproto("PositionStackAndNudge", PositionStack,
x = 0,
y = 0,
setup_params = function(self, data) {
c(
list(x = self$x, y = self$y),
ggproto_parent(PositionStack, self)$setup_params(data)
)
},
compute_layer = function(self, data, params, panel) {
# operate on the stacked positions (updated in August 2020)
data = ggproto_parent(PositionStack, self)$compute_layer(data, params, panel)
x_orig <- data$x
y_orig <- data$y
# transform only the dimensions for which non-zero nudging is requested
if (any(params$x != 0)) {
if (any(params$y != 0)) {
data <- transform_position(data, function(x) x + params$x, function(y) y + params$y)
} else {
data <- transform_position(data, function(x) x + params$x, NULL)
}
} else if (any(params$y != 0)) {
data <- transform_position(data, function(x) x, function(y) y + params$y)
}
data$nudge_x <- data$x
data$nudge_y <- data$y
data$x <- x_orig
data$y <- y_orig
data
},
compute_panel = function(self, data, params, scales) {
ggproto_parent(PositionStack, self)$compute_panel(data, params, scales)
}
)
library(dplyr)
library(ggplot2)
library(ggrepel)
# Create dummy entries for spacing
dummy_row_1 <- data.frame(
x = 1.5, # Position for the first dummy row (between 1 and 2)
predicted = 0, # Height for the dummy bar
response.level = " " # Empty space to avoid NA
)
dummy_row_2 <- data.frame(
x = 3.5, # Position for the second dummy row (after the last bar)
predicted = 0, # Height for the dummy bar
response.level = " " # Empty space to avoid NA
)
# Combine the original predictions with the dummy rows
predictions_with_dummy <- bind_rows(predictions, dummy_row_1, dummy_row_2)
# Ensure the x factor levels include the dummies
predictions_with_dummy$x <- factor(predictions_with_dummy$x,
levels = c(1, 1.5, 2, 3.5), # Use 1.5 and 3.5 for spacing
labels = c("IR.contemporary", "", "IR.historical", "")) # Correct labels
# Plot
g <- ggplot(predictions_with_dummy) +
aes(x = x, y = predicted, fill = response.level) + # Use factor for x-axis
geom_bar(stat = "identity", position = "fill", color = "black", width = 0.8) + # Adjust the bar width
labs(x = 'Sampling period', y = 'Proportion of predicted probabilities', fill = 'Species') + # Adjust labels
theme_classic(base_size = 22) +
ylim(c(0, 1)) +
# Expand scale to create more space on the right side
scale_x_discrete(expand = expansion(mult = c(0.3, 1.1))) + # Add space only on the right side
geom_text_repel(data = predictions_with_dummy[!predictions_with_dummy$response.level == " ", ], # Filter out the dummy rows
aes(label = response.level, color = response.level), size = 5,
segment.color = "grey50", # Color for the segments
position = position_stack_and_nudge(vjust = 0.5, y = 0, x = 0.5), direction="y", hjust=0,segment.curvature = 0,segment.ncp = 10,
box.padding = 0, max.overlaps = Inf) +
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.ticks.x = element_blank())
g
Which leads to this plot that I am in general quite happy with:
I am not unfortunately not able to edit the species labels in the same way as before... any advise how I could achieve what I would like? predict is the result of ggeffect function on my model. Even if I change the original names already before the modelling I would need to manipulate some in italics and some not, so not sure if that would be helpful at all... Any advise would be much appreciated.
I have for example tried, or putting the case when or an ielse directly into the repel but it didnt run either way.
If you're in a rush, consider using a program like Illustrator to modify the text in a saved PDF file.
Mathematical expressions should also work, but I don't have time to look at your code right now. (Please consider sharing a minimal reproducible example, with only the minimum amount of code needed to demonstrate your issue, and nothing else.)
In the future, I'd like to support ggtext which should make this easier — something like "*italics* normal". Pull requests are welcome!
Thank you for your quick reply!
From: Kamil Slowikowski ***@***.***>
Sent: 09 October 2024 20:52
To: slowkow/ggrepel ***@***.***>
Cc: SOFIA ALBRECHT - RESEARCH ***@***.***>; Author ***@***.***>
Subject: Re: [slowkow/ggrepel] stacked barplot with repel legend manipulate single labels e.g. in italics (Issue #264)
If you're in a rush, consider using a program like Illustrator to modify the text in a saved PDF file.
Mathematical expressions should also work, but I don't have time to look at your code right now. (Please consider sharing a minimal reproducible example, with only the minimum amount of code needed to demonstrate your issue, and nothing else.)
In the future, I'd like to support ggtext<https://wilkelab.org/ggtext/> which should make this easier - something like "*italics* normal". Pull requests are welcome!
-
Reply to this email directly, view it on GitHub<#264 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BBZK44TKZ5L7DBKZIC7DI73Z2WCNXAVCNFSM6AAAAABPU4UBV6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMBTGMYTEMBSHE>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
T? an r?omhphost seo faoi r?ir an ts?anta seo leanas at? le f?il ag S?anadh R?omhphost Ollscoil Teicneola?ochta an Atlantaigh<https://www.atu.ie/email-disclaimer>
This e-mail is subject to the following disclaimer available at Atlantic TU Email Disclaimer<https://www.atu.ie/email-disclaimer>
Hello! So I would like to manipulate some labels I have added to a stacked bar plot through repel. The problem is, that some names will have to be in italics, while others don't so I am trying to specify each and I can not succeed so far.
I previously created this plot with the normal legend where it worked:
and I have this image
I have modified the code to have clear labels indicated with repel:
Which leads to this plot that I am in general quite happy with:
I am not unfortunately not able to edit the species labels in the same way as before... any advise how I could achieve what I would like? predict is the result of ggeffect function on my model. Even if I change the original names already before the modelling I would need to manipulate some in italics and some not, so not sure if that would be helpful at all... Any advise would be much appreciated.
I have for example tried, or putting the case when or an ielse directly into the repel but it didnt run either way.
this one did not work neither:
predictions would look like:
question also posted here: https://stackoverflow.com/questions/79071007/stacked-barplot-with-repel-legend-manipulate-single-labels-e-g-in-italics
sorry for the spam, I am a stressed PhD student... X_x xoxo
The text was updated successfully, but these errors were encountered: