1

I am trying to put a pin with information in a position on a static image by using R. Does someone have any suggestion how to do this?

Thanks in advance

code

#load libraries

library(ggplot2)
library(plotly)
library(png)
library(grid)
library(ggthemes)
library(gridExtra)

#create an example dataframe

df<-data.frame(column1=c('A','B','C'),
               column2=c(50,40,50))

#create an gggplot object

plot<-ggplot(df,aes(x=column1,y=column2,fill=column2))+
geom_bar(stat="identity",width=1)+
theme(panel.background=element_blank(),
      panel.border=element_blank(),
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      plot.background=element_blank(),
      legend.position = 'none')

plot_list<- list(plot,plot,plot)

#put it on a image

plot_list_test<- function(plot_list){
ima<- readPNG("testeimage.png")
png("rplot.png", width = 4500, height = 2500, units = "px")
plot.new()
lim <- par()
rasterImage(ima, lim$usr[1],lim$usr[3] ,lim$usr[2] , lim$usr[4])
  
vp <- viewport(.2, 0.55,width =.04, height = .12) #onde quer que fique
pl <- plot_list[1]
print(pl, vp = vp)

pl2 <- plot_list[2]
vp2 <- viewport(.53, 0.87, width =.04, height = .12)
print(pl2, vp = vp2)

pl5 <- plot_list[3]
vp5 <- viewport(.5, 0.78,width =.04, height = .12)
print(pl5, vp = vp5)

dev.off()
}
plot_list_test(plot_list)

##[Alternative]plottly to make it interactive to see value in the plots, but i can not plot them on the static image.

ggplotly(plot)

#image

enter image description here

Lola
  • 11
  • 2
  • Hi, I am not sure to understand what you wish to get. Could you please add what you have tryied so far (with a [reproducible example](https://stackoverflow.com/q/5963269/10264278)) and, if possible an image of the expected output. This will greatly help us to help you :) As a start, package [`magick`](https://docs.ropensci.org/magick/articles/intro.html) could be what you are looking for. – Paul Aug 11 '21 at 06:19
  • 1
    Hi, Paul. I tried to improve the post that I send. I would like to see values in the plots that I put on a image. I tried with plotly to transform in more interactive way, but i can not get plot them on a static image. So, i think i could put a pin or a marker that it will appear when point to it or something like that. Thanks so much for your reply. – Lola Aug 11 '21 at 16:01
  • 1
    Just update, I solved this use by using Shiny app in R. So there, you could make a dashboard with the image and put popover or tootip as you wish in a certain position. Maybe it helps someone :) – Lola Aug 27 '21 at 15:39

0 Answers0