wilkelab / ggtext

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

Image type not supported #50

Closed rccline closed 3 years ago

rccline commented 3 years ago

I have run the code suggested on stackoverflow: https://stackoverflow.com/questions/14070953/photo-alignment-with-graph-in-r Images do show up when using the stackoverflow code chunk, but the images are not separated as they appear in the stackoverflow content but are all contiguous. When running the ggtext code chunk, the images are black with Error message "object is not supported." The library markdown is loaded but R could not find element_markdown object.

Brief description of the problem or desired feature.

library(ggplot2)  #> Warning: package 'ggplot2' was built under R version 4.0.2
library(markdown)

set.seed(123)
myd<- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'phase' = c("Interphase", "Prophase", "Metaphase", "Anaphase", "Telophase"))
myd$value <- floor((rnorm(nrow(myd)))*100)
myd$value[myd$value < 0] <- 0

labels <- c(
  Interphase = "<img src=' img/interphase.png'  width='50' /><br>Interphase",
   Prophase =  "<img src=' img/prophase.png'    width='50' /><br>Prophase",
   Metaphase = "<img src=' img/metaphase.png'   width='50' /><br>Metaphase",
   Anaphase =  "<img src=' img/anaphase.png'    width='50' /><br>Anaphase",
   Telophase = "<img src=' img/telophase.png'   width='50' /><br>Telophase"
)

ggplot(myd, aes(phase, value, fill= cat))+
  geom_col(position = "dodge") +
  scale_x_discrete(name=NULL, labels=labels)  +
  theme(axis.text.x = element_markdown(lineheight = 1.2)) 
#> Error in element_markdown(lineheight = 1.2): could not find function "element_markdown"
Created on 2020-09-25 by the reprex package (v0.3.0)
clauswilke commented 3 years ago

You need to load the ggtext package.

rccline commented 3 years ago

It seems to have something to do with reading the jpg file.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.2
library(markdown)
library(ggtext)
#> Warning: package 'ggtext' was built under R version 4.0.2
library(jpeg)

set.seed(123)
myd<- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'phase' = c("Interphase", "Prophase", "Metaphase", "Anaphase", "Telophase"))
myd$value <- floor((rnorm(nrow(myd)))*100)
myd$value[myd$value < 0] <- 0

labels <- jpeg::readJPEG(c(
  Interphase = "<img src=' interphase.jpg'  width='50' /><br>Interphase",
   Prophase =  "<img src=' prophase.jpg'    width='50' /><br>Prophase",
   Metaphase = "<img src=' metaphase.jpg'   width='50' /><br>Metaphase",
   Anaphase =  "<img src=' anaphase.jpg'    width='50' /><br>Anaphase",
   Telophase = "<img src=' telophase.jpg'   width='50' /><br>Telophase"
))
#> Error in jpeg::readJPEG(c(Interphase = "<img src=' interphase.jpg'  width='50' /><br>Interphase", : unable to open <img src=' interphase.jpg'  width='50' /><br>Interphase

ggplot(myd, aes(phase, value, fill= cat))+
  geom_col(position = "dodge") +
  scale_x_discrete(name=NULL, labels=labels)  +
  theme(axis.text.x = element_markdown(lineheight = 1.2)) 

Created on 2020-09-25 by the reprex package (v0.3.0)