1

I knew how to insert a .png image to a table using annotation_custom() function, but can I use the same function when the variable on the x-axis is categorical? If I can, how should I modify the syntaxes then? And are there any possible ways to convert categorical variables into qualitative variables? I have edited my command, but I still cannot see the image. Here is my command:

AmericanAirlines <- readPNG("C:\\Users\\Desktop\\AU 19\\Lab 5\\American Airlines.png")
dim(AmericanAirlines)
grobAA <- rasterGrob(AmericanAirlines, interpolate=TRUE)
plot3 + annotation_custom(grobAA, xmin=1, xmax=2, ymin=1, ymax=2)

p.s.: I think there is nothing wrong with the plot3 since I can get the plot successfully.

Thanks for your answer!

Tianrun
  • 13
  • 3
  • 1
    It's easier to help you if you 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 Dec 10 '19 at 22:10
  • Please share a reproducible example, with a little bit of sample data and a code attempt. Also please let us know what you mean by "qualitative variables" and how they are different from "categorical variables". – Gregor Thomas Dec 10 '19 at 22:10
  • Hi @Tianrun. Please try providing a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data and the code you have tried so far. This way is easier for the SO community to help you. – MalditoBarbudo Dec 10 '19 at 22:10
  • You might be able to convert your 'categorical' variable to a 'qualitative' with the function `as.character()`. But the best practice would be to follow MrFlick's suggestion. – xilliam Dec 10 '19 at 22:11

1 Answers1

1

ggplot internally puts categorical values at positions 1, 2, 3, etc., on the x-axis, so you can use those values as the scale when determining where to place an image. For example, using the built-in iris data frame and the Rlogo image:

img = png::readPNG(system.file("img", "Rlogo.png", package="png"))
img = rasterGrob(img, interpolate=TRUE)

ggplot(iris, aes(Species, Petal.Width)) + 
  geom_point() + 
  annotation_custom(img, xmin=0.8, xmax=1.2, ymin=0.7, ymax=1.1) +
  annotation_custom(img, xmin=1.2, xmax=1.8, ymin=1.4, ymax=2) +
  annotation_custom(img, xmin=2.1, xmax=2.3, ymin=0.5, ymax=0.7) 

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thanks for your help! But can you go back and check if there are any problems with the syntaxes I wrote? – Tianrun Dec 10 '19 at 22:37
  • The code you posted looks okay, but other things could be going wrong. For example, is the coordinate range you're using in `annotation_custom` within the range of the data in your plot. If not, the image won't be visible. Also, are you sure there's not a problem with the image file? For example, if you run `plot(as.raster(AmericanAirlines))`, does the image appear in the plot window? – eipi10 Dec 10 '19 at 22:50
  • plot(as.raster(AmericanAirlines) works and I have tried some different range settings, but I still cannot get the image. Are there any other possible explanations? – Tianrun Dec 10 '19 at 22:56
  • Hard to know without a reproducible example. If you try to plot the R logo, does it work? – eipi10 Dec 10 '19 at 22:58
  • I have also tried to use as.character() to convert the format of the variables, but it says'Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "character"' – Tianrun Dec 10 '19 at 22:58
  • I think it is the range of data, I put -Inf and Inf in the corresponding place and the picture popped up. But how can I know the range of the x-axis when the variable on the axis is categorical – Tianrun Dec 10 '19 at 23:09
  • The range of the x-axis is *1* to *n*, where *n* is the number of unique categories. – eipi10 Dec 11 '19 at 00:18