13

Is it possible to add a joyplot as a panel to a plot that includes a ggtree, as shown in these examples? Examples of joyplots are here.

I realize that I could manually put the species labels for the joyplot in the same order as the tree tip labels, but I am looking for an automatic solution. I would like to associate the joyplot rows with the tips of the trees automatically, akin to how the boxplot data are associated with the tip labels.

I think that Guangchuang Yu's examples at the above link provide suitable data:

require(ggtree)
require(ggstance)

# generate tree
tr <- rtree(30)

# create simple ggtree object with tip labels
p <- ggtree(tr) + geom_tiplab(offset = 0.02)

# Generate categorical data for each "species"
d1 <- data.frame(id=tr$tip.label, location=sample(c("GZ", "HK", "CZ"), 30, replace=TRUE))

#Plot the categorical data as colored points on the tree tips
p1 <- p %<+% d1 + geom_tippoint(aes(color=location))

# Generate distribution of points for each species
d4 = data.frame(id=rep(tr$tip.label, each=20), 
            val=as.vector(sapply(1:30, function(i) 
                            rnorm(20, mean=i)))
            )               

# Create panel with boxplot of the d4 data
p4 <- facet_plot(p1, panel="Boxplot", data=d4, geom_boxploth, 
        mapping = aes(x=val, group=label, color=location))           
plot(p4)

This produces the plot below: demo ggtree plot

Is it possible to create a joyplot in place of the boxplot?

Here is code for a quick joyplot of the demo dataset d4 above:

require(ggjoy)

ggplot(d4, aes(x = val, y = id)) + 
geom_joy(scale = 2, rel_min_height=0.03) + 
scale_y_discrete(expand = c(0.01, 0)) + theme_joy()

The result is: demo joyplot

I am new to ggplot2, ggtree, and ggjoy so I am totally at a loss with how to even begin doing this.

2
  • Can you provide some suitable sample data to work with? Paste into your question the output of dput(data_sample)
    – eipi10
    Commented Jul 29, 2017 at 2:47
  • @eipi10 I have added some demo code written by the ggtree package's creator. I'm not sure what you're referring to by dput(data_sample)
    – LCM
    Commented Jul 29, 2017 at 3:30

1 Answer 1

14

Note: As of 2017-09-14, the ggjoy package has been deprecated. Instead, use the ggridges package. For the code below to work with ggridges, use geom_density_ridges instead of geom_joy.


It looks like you can just replace geom_boxplot with geom_joy in facet_plot:

facet_plot(p1, panel="Joy Plot", data=d4, geom_joy, 
           mapping = aes(x=val, group=label, fill=location), colour="grey50", lwd=0.3) 

enter image description here

If you're new to ggplot2, the visualization chapter of Data Science with R (an open-source book by the author of ggplot2) should be helpful for learning the basics.

ggjoy and ggtree extend the capabilities of ggplot2. When such extensions are done well, the "obvious" thing to do (in terms of the usual ggplot "grammar of graphics") often works, because the extension package is written in a way that tries to be faithful to the underlying ggplot2 approach.

Here, my first thought was to just substitute geom_joy for geom_boxplot, which turned out to get the job done. Each geom is just a different way to visualize the data, in this case box plot vs. density plot. But all of the other "structure" of the plot stays the same, so you can just change geoms and get a new plot that follows the same axis ordering, color mappings, etc. This will make more sense once you get some experience with the ggplot2 grammar of graphics.

Here's a slightly different labeling approach for the left-hand plot:

p1 = ggtree(tr) %<+% d1 +
  geom_tippoint(aes(color=location), size=6) +
  geom_tiplab(offset=-0.01, hjust=0.5, colour="white", size=3.2, fontface="bold") 

facet_plot(p1, panel="Joy Plot", data=d4, geom_joy, 
           mapping = aes(x=val, group=label, fill=location), colour="grey40", lwd=0.3) 

enter image description here

UPDATE: This is in response to your comment asking how to get the same custom colors in both facet panels. Here's code to do that with the example data in your question:

p1 = ggtree(tr) %<+% d1 +
  geom_tippoint(aes(color=location), size=5) +
  geom_tiplab(offset=-0.01, hjust=0.5, colour="white", size=3, fontface="bold") +
  scale_colour_manual(values = c("grey", "red3", "blue")) +
  scale_fill_manual(values = c("grey", "red3", "blue"))

facet_plot(p1, panel="Joy Plot", data=d4, geom_joy, 
           mapping = aes(x=val, group=label, fill=location), colour="grey40", lwd=0.3) 

enter image description here

5
  • Thanks so much! How might I set a custom color scale and make it perpetuate through to the joyplot? I have a tree with binary categories for geom_tippoint(); I can choose colors using + scale_colour_manual(values = c("no" = "grey", "yes" = "red3")) but I can't figure out how to make that color scheme extend through to the joyplot. If I use scale_colour_manual() I get the error "Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale." But the joyplot keeps the default colors.
    – LCM
    Commented Jul 29, 2017 at 18:38
  • 1
    Add scale_fill_manual(values = c("no" = "grey", "yes" = "red3")) to the initial plot and it will carry through when you do facet_plot. geom_joy is using a fill aesthetic, so you need to set the fill colours with scale_fill_manual, rather than scale_colour_manual. Just use the same colors for colour and fill and they will match when the facets are plotted. scales (colour scales, shape scales, axis ranges and breaks, etc.) are another aspect of the "grammar of graphics" embodied in ggplot2.
    – eipi10
    Commented Jul 29, 2017 at 19:09
  • When I do that, I get the desired colors (grey and red) for the joyplot but not for the tree tips. I don't have a reproducible example right now, but maybe you can tell from the syntax: baseplot <- p %<+% d1 + geom_tippoint(aes(color=factor(HybridizesLax), cex = 1)) + scale_fill_manual(values = c("no" = "grey", "yes" = "red3")) + theme(legend.position = "left") and tree_joyplot <- facet_plot(baseplot, panel="Annual mean temperature (C)", data=amt_data, geom_joy, mapping = aes(x=amt, group=label, fill = factor(HybridizesLax)), colour="grey50", lwd=0.3)
    – LCM
    Commented Jul 29, 2017 at 21:05
  • 1
    See update at the end of my answer. The problem with the code in your comment is that the scale_colour_manual statement is missing from the first plot, so only the fill aesthetic in the geom_joy facet has the custom colors, but the colour aesthetic for the points in the tree plot is still using the default colors.
    – eipi10
    Commented Jul 30, 2017 at 1:04
  • Thanks for updating this with the correct syntax for ggridges!
    – LCM
    Commented Oct 4, 2017 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.