Search code examples
rpdfggplot2r-markdownxelatex

Image transparency is affected (transparent) once alpha is modified! ggplot2


I am trying to add the following image to this barchart, however, as you can see when I knight it to produce a pdf the image gets transparent! with that alpha of .45. If I set alpha to 1 then it shows as it should be!

In this example I am using alpha only to make the second dodge barchart transparent, but the included png, should be 100% in color not faded! Do you know of any way to achieve this, i still want those sets of bars faded away with alpha but the image intact.

Note that if you just run this in your Rmarkdown the image in the preview looks excellent, the problem comes when we knit and produce the pdf!

Please Help

This is the faded unwanted result of the horse image on the chart when knitr to pdf:

enter image description here

The actual icon is: enter image description here

The code I used is here:

library(ggplot2)
library(reshape2)
library(grid)

#Create DataFrame
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(39,6,3,4,1,6,7,2,9,10,3,4)
day <- c("d1","cd2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12")
df1 <- data.frame(x, y, day)

#Sort the table according to x values
df1$day <- factor(df1$day, levels = df1[order(-df1$x), 3])

#for each day (e.g. 1) you will have all combinations of x and y (e.g. day = 1, x = 5, y = 5), (e.g. day = 12, x=34, y=4). variable becomes x and y, and value becomes the value of either x or y.
df2 <- melt(df1, id.var="day", measure.var=c("x","y"))
#head(df2)

#Image
img <- readPNG("imgs/unnamed.png")
g <- rasterGrob(img, interpolate=TRUE)


barcolors <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363", "#D0B100", "#CE2929", "#428953")

ggplot(df2, aes(x=day, y=value)) + 
    geom_bar(aes(fill=day, alpha=variable), stat="identity", width = .75, position = position_dodge(width=.8)) +
    scale_alpha_manual(values=c(1, .45)) +
    scale_fill_manual(values= barcolors) +
    theme_bw() + 
    theme( panel.grid.major.x = element_blank(),
       legend.position="none",
       legend.text=element_text(size=14),
       legend.title = element_blank(),
       axis.title.x = element_blank()) +
    annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40) + ylim(0,40)   

Solution

  • This may not be an explanation of the cause, but a working solution.

    library(tidyverse)
    library(reshape2)
    library(grid)
    library(png)
    
    df = tibble(
      x = c(5,17,31,9,17,10,30,28,16,29,14,34),
      y = c(39,6,3,4,1,6,7,2,9,10,3,4),
      day = c("d1","cd2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12")
    ) %>% mutate(
      day = day %>% fct_reorder(x)
    ) %>% pivot_longer(cols=1:2, names_to = "variable", values_to = "value")
    
    img <- readPNG("imgs/unnamed.png")
    g <- rasterGrob(img, interpolate=TRUE)
    
    barcolors <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363", "#D0B100", "#CE2929", "#428953")
    df %>% ggplot(aes(x=day, y=value, alpha=variable)) + 
      geom_bar(aes(fill=day), stat="identity", width = .75, position = position_dodge(width=.8))+
      annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40)+
      annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40)+
      scale_alpha_manual(values=c(1, .45)) +
      scale_fill_manual(values= barcolors) +
      theme_bw() + 
      theme( panel.grid.major.x = element_blank(),
             legend.position="none",
             legend.text=element_text(size=14),
             legend.title = element_blank(),
             axis.title.x = element_blank()) + 
      ylim(0,40)
    

    Below is a excerpt from a pdf file. enter image description here And here is a snippet from an HTML file. enter image description here