I want to create a plot that shows a phylogenetic tree on the left side, and and a ridge plot on the right side that visualizes the presence different DNA motifs (short stretches of important sequences), where each sequence is aligned to its corresponding leaf node in the phylogenetic tree. Basically I want to re-create the plot shown here (Fig. 9.4), with my own data and using geom_ridgeline_gradient.
However, the group aesthetic seems to be making problems, as it messes up the correct rendering of the plot.
The problem seems to be part of the geom_ridgeline_gradient.
Here is a minimal example of my problem:
library(ggridges)
library(ggtree)
library(data.table)
library(ggplot2)
minimal_data1 <- data.table(
seq = rep("seq1", 20),
id = rep(1, 20),
pos = rep(seq(1, 10), 2),
height = c(c(0, 0, 0, 1, 1, 1, 1, 0, 0, 0), c(0, 0, 0, 0, 0, 1, 1, 1, 1, 0)), # overlapping motifs
annotation = c(c(NA, NA, NA, "A", "A", "A", "A", NA, NA, NA), c(NA, NA, NA, NA, NA, "C", "C", "C", "C", NA))
)
minimal_data2 <- data.table(
seq = rep("seq_2", 10),
id = rep(2, 10),
pos = seq(1, 10),
height = c(0, 0, 0, 0, 1, 1, 1, 1, 0, 0),
annotation = c(NA, NA, NA, NA, "B", "B", "B", "B", NA, NA)
)
minimal_data3 <- data.table(
seq = rep("seq_3", 10),
id = rep(2, 10),
pos = seq(1, 10),
height = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), # sequence without any motifs
annotation = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)
minimal_data <- rbindlist(list(minimal_data1, minimal_data2, minimal_data3))
# this works fine
ggplot(minimal_data, aes(x=pos, y=seq, height=height, fill=annotation)) +
geom_ridgeline_gradient(scale=0.3) +
scale_fill_discrete(na.translate=FALSE)
# this messes up everything
ggplot(minimal_data, aes(x=pos, y=seq, group=seq, height=height, fill=annotation)) +
geom_ridgeline_gradient(scale=0.3) +
scale_fill_discrete(na.translate=FALSE)
Plotting without specifying group gives a perfectly fine plot, but adding group messes everything up.
The problem is that I apparently need to specify group in order to make the plot compatible with ggtree.
Given that I need to stick with the group parameter, what could be the reason of faulty rendering and how could one fix this?