How to Use an Image as a Point in Ggplot

Use images as points ggplot2

Using ggimage,

df3$image <- rep(c( "http://phylopic.org/assets/images/submissions/95981b99-36cc-413e-bf59-0454fde43c85.512.png"), 4)

ggplot(df3, aes(Models,Estimate,ymin=Estimate-se, ymax=Estimate+se, color = Estimate)) +
geom_pointrange(aes(color= Estimate), size=1) +
scale_color_gradient(low="red", high="blue") +
theme_bw() + labs(colour = "S-stadistic") + geom_image(image = df3$image[1])

Sample Image

Display custom image as geom_point

The point geom is used to create scatterplots, and doesn't quite seem to be designed to do what you need, ie, display custom images. However, a similar question was answered here, which indicates that the problem can be solved in the following steps:

(1) Read the custom images you want to display,

(2) Render raster objects at the given location, size, and orientation using the rasterGrob() function,

(3) Use a plotting function such as qplot(),

(4) Use a geom such as annotation_custom() for use as static annotations specifying the crude adjustments for x and y limits as mentioned by user20650.

Using the code below, I could get two custom images img1.png and img2.png positioned at the given xmin, xmax, ymin, and ymax.

library(png)
library(ggplot2)
library(gridGraphics)
setwd("c:/MyFolder/")

img1 <- readPNG("img1.png")
img2 <- readPNG("img2.png")
g1 <- rasterGrob(img1, interpolate=FALSE)
g2 <- rasterGrob(img2, interpolate=FALSE)
qplot(1:10, 1:10, geom="blank") +
annotation_custom(g1, xmin=1, xmax=3, ymin=1, ymax=3) +
annotation_custom(g2, xmin=7, xmax=9, ymin=7, ymax=9) +
geom_point()

How to use your own image for geom_point in gganimate?

Is this what your are looking for ?

I Just changed the color = factor(Name) position to geom_line statement.

If you use color = factor(Name) with ggplot in first row, it will affect to whole plot. So you should take care when using this statement.

a <- ggplot(Final, aes(x = Step, y = Value, group = Name)) + 
geom_line(size=1, aes(color = factor(Name))) +
geom_image(aes(image=Image)) +
transition_reveal(Step) +
coord_cartesian(clip = 'off') +
theme_minimal() +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
theme(legend.position = "none")

For convenience, i captured the picture .

Sample Image

Inserting an image to ggplot2

try ?annotation_custom in ggplot2

example,

library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

qplot(1:10, 1:10, geom="blank") +
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_point()

Labelling the plots with images on graph in ggplot2

You can use annotation_custom, but it will be a lot of work because each image has to be rendered as a raster object and its location specified. I saved the images as png files to create this example.

library(ggplot2)
library(png)
library(grid)

img1 <- readPNG("c:/test/img1.png")

g1<- rasterGrob(img1, interpolate=TRUE)


img2 <- readPNG("c:/test/img2.png")
g2<- rasterGrob(img2, interpolate=TRUE)


plotdata<-data.frame(seq(1:2),seq(11:12))
ggplot(data=plotdata) + scale_y_continuous(limits=c(0,4))+ scale_x_continuous(limits=c(0,4))+
geom_point(data=plotdata, aes(plotdata[,1],plotdata[,2])) +
annotation_custom(g1,xmin=1, xmax=1.5,ymin=1, ymax=1.5)+
annotation_custom(g2,xmin=2, xmax=2.5,ymin=2, ymax=2.5)

Sample Image

How to add image to a ggplot

This will plot the image instead of a point

library("ggplot2")
library("ggimage")


d <- data.frame(x = 1:2,
y = 1:2,
image = c("https://nba-players.herokuapp.com/players/james/lebron",
"https://nba-players.herokuapp.com/players/james/lebron"))

ggplot(d, aes(x, y)) +
geom_image(aes(image=image), size=.2) +
scale_x_continuous(limits = c(0, 3)) +
scale_y_continuous(limits = c(0,3))

Align points with background png in ggplot2

By manually adjusting the position of the image with xmin/xmax & ymin/ymax, fixing the pitch of rows and columns with coord_fixed(clip = "off) and expanding the plot.margin in theme I was able to get something that seems like it will work.

library(ggplot2)
library(png)
library(RCurl)
library(grid)

example.gg <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vRcX5aMZGCp9Bs3BRZSg8k4o-kbSjOO5z3LsRxgIv4qJHz1fG-Argruje32OuZ2Tt2qPaNGksGr4Jia/pub?output=csv",
row.names = 1)
example.gg$Row <- factor(example.gg$Row, levels = sort(unique(example.gg$Row)))

png.img <- readPNG(getURLContent("https://i.imgur.com/QeSO7d3.png"))
img.rg <- rasterGrob(png.img, interpolate=TRUE)

gp <- ggplot(example.gg,
aes(x = Col, y = Row, col = Lum)) +
annotation_custom(
img.rg,
xmin = -2,
xmax = 27,
ymin = -1,
ymax = 18
) +
geom_point(shape = 15) +
coord_fixed(clip = "off") +
theme_void() +
theme(plot.margin = unit(c(3, 2, 5, 2), "lines"))

gp

Sample Image

Created on 2022-02-14 by the reprex package (v2.0.1)



Related Topics



Leave a reply



Submit