0

I work with sports data and would like to visualize different shooting percentages at different places. I would like to make several text boxes, similar to in Microsoft word or powerpoint, and put them over each corresponding part of a picture of a goal to show the percentage at that place. I figured out how to get a picture into and R markdown but can't figure out how to make these text boxes and place them where I want.

  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 16 '18 at 18:51

1 Answers1

0

You could add a chunk of R code to create a plot with your image as the background. Then you can add text to the plot, and it will appear over your image.

In the example below, I use an image in my working directory and the jpeg library:

```{r image_with_text, echo=FALSE}
library(jpeg)

#This image is in my working directory, but you may need to use a full path to your image:
my_image=readJPEG("beach-sunset-1483166052.jpg")

# Set up a blank plot
plot.new()

# get all the graphical parameters (as a named list).
#In this list, usr is a vector of the form c(x1, x2, y1, y2),
#giving the extremes of the user coordinates of the plotting region
gp <- par() 

rasterImage(image=my_image,
        xleft= gp$usr[1], ybottom=gp$usr[3], xright=gp$usr[2], ytop=gp$usr[4])

text(x=0.75, y=0.15, labels = "Wish You Were Here!", col="White")

```

The resulting image has the text in the bottom right corner (75% of the way along the x-axis, 15% of the way along the y-axis.)

Of course there are many ways to change the text, you can find out more about it here: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/text.html

cfelix
  • 194
  • 2
  • 7