Using ggplotly in R
1
0
Entering edit mode
5 months ago
jen ▴ 10

Hi all, I have a plot that pretty much gives me everything I want in terms of aesthetics (except the 'Not assigned' category should be shown in grey in the legend, but I never got that snippet of code to work... oh well...). Here is my code:

Picornas_for_interactive_graph3 <- ggplot(justPicorna_contiginfo, aes(x = kmer_cov, y = percid, size = querylength, color = family, text = subject_title)) + geom_point(alpha = 1) + geom_point(data = subset(justPicorna_contiginfo, family == "Not assigned"), aes(x = kmer_cov, y = percid), color = "darkgrey", shape = 16) + scale_x_log10() + scale_size(range = c(5, 20), name="query length") + scale_color_viridis_d(option = "turbo", alpha = 1, begin = 0.05, end = 0.5, direction = -1) + labs(x = "kmer coverage", y = "percent identity (%)", color = "family") +  # Update the color legend title theme(plot.margin = margin(2, 2, 2, 2, "cm"), axis.title.x = element_text(size = 18), axis.title.y = element_text(size = 18), legend.title.position = "top", axis.text = element_text(size=14), legend.title = element_text(size = 16, color = "darkblue"), legend.text = element_text(size = 16), legend.position = "right", legend.direction = "vertical", legend.spacing.y = unit(0, "lines")) + guides(color = guide_legend(override.aes = list(size = 5, alpha = 1)), size = guide_legend(override.aes = list(alpha = 0.7, color = "darkgrey")))

And this is what my output looks like: kmer cov x percent identity

However, I need to make my graph interactive and that is when I start experiencing trouble. I use this code:

Picornas_for_interactive_graph3_trial <- ggplotly(Picornas_for_interactive_graph3, tooltip = c("text","x","y", "size"))

And this is what my graph looks like:

enter image description here

As you can see, some of the features of my organized legend are lost. I need a "query length" category in my legend as it is crucial for an audience to get a feel for how long the sequences are. I also had to change some of the aesthetics of my original ggplot because ggplotly doesn't seem to support them. If anyone has insights on how to fix my legend (or any other tips and tricks for ggplotly), I would greatly appreciate the help!

Thanks so much!

ggplot ggplotly • 596 views
ADD COMMENT
1
Entering edit mode
5 months ago

The simple answer is to plot it natively with plotly rather than use ggplotly. See this stackoverflow answer for more guidance. Or this one for additional options.

ADD COMMENT
0
Entering edit mode

Thank you Jared for your response. I took a look at the documentation you provided and am trying my best to follow along, but can't seem to figure this out. I went ahead and created a plot similar to the one I did on ggplot using baseR (not exactly what I want it to look like yet, but almost):

unique_families <- unique(justPicorna_contiginfo$family)
family_colors <- rainbow(length(unique_families))
family_colors[match("Not assigned", unique_families)] <- "darkgrey"

# Define sizes for legend
legend_sizes <- c(5, 10, 15, 20)  

# Scale factor for size
scale_factor <- 3  

myplot <- plot(justPicorna_contiginfo$kmer_cov, justPicorna_contiginfo$percid, 
     xlab = "kmer coverage", ylab = "percent identity (%)",
     col = family_colors[match(justPicorna_contiginfo$family, unique_families)],
     pch = 19, 
     cex = justPicorna_contiginfo$querylength / max(justPicorna_contiginfo$querylength) * scale_factor,
     main = "Interactive Graph",
     log = "x") 

legend("topright", legend = unique_families,
   col = family_colors,
   pch = 19,
   title = "family",
   cex = 1.2) 

legend("bottomright", legend = legend_sizes,
   pch = 19,
   pt.cex = legend_sizes / max(legend_sizes) * scale_factor,
   title = "query length",
   cex = 1.2)
myplot

enter image description here

However, the issue once again arises when I try to make the graph interactive and am lost with how to add traces and markers for my situation. I have a class presentation for this research on Thursday so my main concern is not having enough time to work on my presentation if I keep troubleshooting this. With that said, I'd appreciate any additional feedback regarding my code, and already appreciate the help so much!

Thanks :)

ADD REPLY
0
Entering edit mode

Again, this is not particularly difficult to do with plotly itself - the syntax is pretty similar to ggplot.

ADD REPLY

Login before adding your answer.

Traffic: 1536 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6