wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
651 stars 37 forks source link

Error while trying to color code duplicate entries. #66

Closed lucolotto closed 3 years ago

lucolotto commented 3 years ago

Hi, thanks for the great and useful package. I have a problem with the code and was wondering whether you might help me out. This is a sample of my data frame:

structure(list(FeatureImportance = c(0.228123354556408, -0.243139074107349, 
0.257935612595988, 0.240474981815569, 0.251081702710318, 0.230941794398319, 
0.251649807646596, 0.233973018619464, 0.274102816039728, 0.2694826072062, 
0.195827445051334, -0.289888363766231, 0.200151749558119, -0.20335609344819, 
0.334597298263201, 0.214621658966548, -0.238437130784274, 0.232688568345825, 
0.230981255453348, -0.220299270904098, -0.274159045485752, 0.216976232441559, 
0.250056867996829, 0.239976327242484, 0.219727806825915, 0.21153536166106, 
0.220087025905449, 0.210486578066946, 0.189692296921425, 0.190981969460222
), OTU = c("02ed196d45808c039b0d4a06d99c9df9", "4693d2ce964dfe2effd506202ae5d0df", 
"4a50d0fd106236fb8d7975bf08d7e212", "b39387df4a735155fe0f4551e73a0ca1", 
"e41cf024b97c3019e7187d75b1582822", "1c896a379ca3a788ccce99ad06b50135", 
"72cdd21a388616465137e868da591d67", "9b39afbf79e3282aa0ef08bf912cdf6a", 
"b411d98091d4c38f181d8e588479fccc", "e5f8c602955a13b0f212f4e6790026ba", 
"08894f466c1bed6d7649bb8e50e1e819", "ec16ae4fbbd298f7a109978474adb944", 
"113cbf5e2aeb0f509cbfd6b0df5f48b9", "13fb2daae81fc03061104cd28fcd9ea2", 
"78d3a33068333a8453092740782b3a70", "9637c475dfc1ceb41b4bd59b09bba802", 
"ad60864226184113e4c7ebfd147fbd01", "ec16ae4fbbd298f7a109978474adb944", 
"0f17b2c0954bc26fde587360c7c22c89", "b39387df4a735155fe0f4551e73a0ca1", 
"08208079dff8dbbee4a10904c390cab6", "40b0c6e30d6d5f89724601a893c2ecfc", 
"bafdd9b34a3034c01adb7c5a7c13cf3e", "8599ee696498ef4336ae8e2ec2d5e0a2", 
"d7772079c4ec34f08a0a7a5e224f3625", "14f6b085a55546ec0b3c77d6f51f8a3d", 
"7d72a2917b4c6fe53c7d0d07a7869118", "b63a91aee8591d5834514aac34b39f95", 
"3aff9783bed1e7c3492353d62a6b4f36", "a17c7fb448f861b738fa546224acca28"
), Location = c("PM", "PM", "PM", "PM", "PM", "CO", "CO", "CO", 
"CO", "CO", "GO", "GO", "GO", "GO", "GO", "CL", "CL", "CL", "CL", 
"CL", "SC", "SC", "SC", "SC", "SC", "MA", "MA", "MA", "MA", "MA"
)), row.names = c("10", "121", "132", "448", "464", "43", "193", 
"250", "286", "465", "15", "366", "395", "397", "430", "243", 
"278", "3661", "394", "4481", "14", "114", "299", "432", "461", 
"22", "209", "290", "407", "443"), class = "data.frame")

I am trying to do a bar plot of FeatureImportance and OTU by Location and I would like to label in red duplicated OTUs. Assuming my data frame is named results, the code I managed to get so far is:

library(glue)
library(ggtext)
plot = 
  results %>%
  mutate(Location = fct_relevel(Location, "PM", "CO", "GO", "CL", "SC", "MA")) %>%
  mutate(OTU = ifelse(
    duplicated(OTU),
    glue("<span style = 'color:red'>{OTU}</span>"),
    glue("<span style = 'color:black'>{OTU}</span>")
  )) %>%
  ggplot(aes(x=FeatureImportance, y=OTU)) +
  geom_col(colour = "#669933", fill = "#669933") + 
  geom_vline(xintercept = 0) + 
  facet_wrap(~ Location, ncol = 6) +
  theme_linedraw() +
  theme(axis.text.y = element_markdown())

plot 

However the plot is not correct: example.pdf

As you can see first and third OTUs (i.e. ec16ae4fbbd298f7a109978474adb944) are duplicated but only the first one is in red while the third one is in black, plus the third line should be merged with the first, but instead is present... Can you help me fixing this? If I do not try to label duplicated entries in red the code works fine and OTU ec16ae4fbbd298f7a109978474adb944 appears only in one line of the plot, so I guess it has something to do with ggtext.

Thanks a lot in advance for your support Best Luca

clauswilke commented 3 years ago

This is not a ggtext issue. Consider asking for help on stackoverflow.com.