Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: ggplot for plotLoadings #345

Open
samuelmontgomery opened this issue Nov 13, 2024 · 2 comments
Open

feature request: ggplot for plotLoadings #345

samuelmontgomery opened this issue Nov 13, 2024 · 2 comments
Assignees
Labels
feature-request Can be implemented if there's enough interest

Comments

@samuelmontgomery
Copy link

Hi,

Just wondering if it would be possible to output the plotLoadings function as a ggplot object?
Currently it uses base R within the plotLoadings_barplot function to plot a barplot, which makes it more difficult to customise and is resulting in the feature names for some of my plots being cut off on the right axis if they're too long

Both the plotIndiv and plotVar are already output as ggplot objects so it would also make it uniform across those plots

Thanks!

@samuelmontgomery samuelmontgomery added the feature-request Can be implemented if there's enough interest label Nov 13, 2024
@evaham1 evaham1 assigned evaham1 and unassigned aljabadi Nov 18, 2024
@evaham1
Copy link
Collaborator

evaham1 commented Nov 18, 2024

Hi @samuelmontgomery I agree a ggplot output would be much better, I will look into implementing this in the next couple of weeks

@samuelmontgomery
Copy link
Author

samuelmontgomery commented Nov 20, 2024

Thanks - I have made a workaround function for the moment that uses the plotLoadings output to replot using ggplot (only for splsda at the moment!)

ggloadingplot <- function(object,
comp,
method = 'median',
contrib = 'max',
size.name = 0.8,
ndisplay = 20,
title)
{
loading_plot <- plotLoadings(object,
comp = comp,
block = "X",
method = method,
contrib = contrib,
size.name = size.name,
legend = T,
ndisplay = ndisplay,
title = title,
size.title = 1.5,
xlim = c(-0.8, 0.8))

loading_plot <- as.data.frame(loading_plot$X)
loading_plot$component <- factor(rownames(loading_plot), levels = rownames(loading_plot)[order(-abs(loading_plot$importance))])

p <- ggplot(loading_plot, aes(x = component, y = importance, fill = GroupContrib)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = setNames(unique(loading_plot$color), unique(loading_plot$GroupContrib))) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
coord_flip() +
labs(y = "Importance", x = NULL, title = title, fill = "Outcome")
return(p)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Can be implemented if there's enough interest
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

4 participants
@aljabadi @evaham1 @samuelmontgomery and others