5

Gero está figura:

inserir a descrição da imagem aqui

ao usar esse código:

library(tidyverse)
library(plotly)



 tres <- readRDS(
url("https://rawcdn.githack.com/fsbmat/StackOverflow/b4fbf2fe0a59555a2cf77776ab9454a4fb06ba83/tres.rds"))
    
plot <- tres %>% ggplot(aes(Time, Pontos, 
                            fill=Time, 
                            text=paste("Time:", Time, "<br>", 
                                       "Pontuação: ", Pontos)))+
  geom_col(show.legend = FALSE)+
  theme_bw()+
  geom_text(aes(label=Pontos),nudge_y = 2)
ggplotly(plot, tooltip = "text", width = 600, height = 600)%>% 
layout(showlegend = FALSE, 
title = list(
text = paste0('Os três primeiros colocados', '<br>', '<sup>', 'Campeonato Brasileiro de 2019', '</sup>')), 
margin=0) %>% style(textposition = "top")

Gostaria de saber se há como adicionar estas figuras usando códigos no R:

inserir a descrição da imagem aqui

Para gerar a figura abaixo no próprio R usando a função ggplotly:

inserir a descrição da imagem aqui

Consegui fazer a figura no ggplot, com o código:

library(png)
ouro <- readPNG('~/Caminho da Figura/Ouro.png')
prata <- readPNG('~/Caminho da Figura/Prata.png')
bronze <- readPNG('~/Caminho da Figura/Bronze.png')

tres %>% ggplot(aes(Time, Pontos, 
                            fill=Time, 
                            text=paste("Time:", Time, "<br>", 
                                       "Pontuação: ", Pontos)))+
  geom_col(show.legend = FALSE)+
  theme_bw()+
  geom_text(aes(label=Pontos),nudge_y = 2)+
  annotation_raster(ouro, ymin = 75,ymax= 90.1,xmin = 0.75,xmax = 1.25)+
  annotation_raster(prata, ymin = 59,ymax= 74.1,xmin = 1.75,xmax = 2.25)+
  annotation_raster(bronze, ymin = 59,ymax= 74.1,xmin = 2.75,xmax = 3.25)

Link para as figuras: Clique aqui!

6
  • Olá fsbmat, uma duvida, qual a relação do plotfy com a pergunta? Plotfy é uma lib JS, tem algo especifico ou correlacionado?
    – Syzoth
    4/11/2020 às 3:44
  • Oi Guilherme, vc leu errado, o nome correto da função é plotly!
    – fsbmat
    4/11/2020 às 3:49
  • Não fsbmat, eu não li errado, é a tag que você adicionou plotly, essa tag é sobre uma LIB javascript
    – Syzoth
    4/11/2020 às 4:02
  • Vc escreveu Plotfy na sua primeira pergunta, eu escrevi plotly. Essa última tag é referente a um pacote do software R que possui esse nome!
    – fsbmat
    4/11/2020 às 4:07
  • 1
    Caro Guilherme, me desculpe a ignorância em não observar a obviedade da tag. Mas acho sua solução ótima, parabéns pela iniciativa e obrigado por ajudar a melhorar o stackoverflow em português!
    – fsbmat
    4/11/2020 às 16:47

1 Resposta 1

3

Consegui fazer a figura também no ggplotly, usando o código abaixo:

plot <- tres %>% ggplot(aes(Time, Pontos, 
                            fill=Time, 
                            text=paste("Time:", Time, "<br>", 
                                       "Pontuação: ", Pontos)))+
geom_col(show.legend = FALSE)+
theme_bw()+
geom_text(aes(label=Pontos),nudge_y = 2)

ggplotly(plot, tooltip = "text", width = 600, height = 600)%>% 
  layout(images = list(list(
    source = raster2uri(as.raster(ouro)),
    x = 0.75, y = 75, 
    sizex = 0.5, sizey = 15.1,
    xref = "x", yref = "y",
    xanchor = "left", yanchor = "bottom",
    sizing = "stretch"
  ), list(
    source = raster2uri(as.raster(prata)),
    x = 1.75, y = 59, 
    sizex = 0.5, sizey = 15.1,
    xref = "x", yref = "y",
    xanchor = "left", yanchor = "bottom",
    sizing = "stretch"
  ), list(
    source = raster2uri(as.raster(bronze)),
    x = 2.75, y = 59, 
    sizex = 0.5, sizey = 15.1,
    xref = "x", yref = "y",
    xanchor = "left", yanchor = "bottom",
    sizing = "stretch"
  )),
  showlegend = FALSE, 
  title = list(text = paste0('Os três primeiros colocados',
                             '<br>',
                             '<sup>',
                             'Campeonato Brasileiro de 2019',
                             '</sup>')), 
  margin=0) %>%
  style(textposition = "top")

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .