Reputation: 305
I have a list of dataframes. The dataframes have the columns: Dist
, GPSSpeed_kmh
. I want to plot a png of the left-bend sign in a specific point of the plot. I used the following code for the dataframe df
and the result is shown below:
ggplot(df, aes(Dist, GPSSpeed_kmh)) +
geom_point(size = 1) +
geom_smooth(se = TRUE, span = 0.7) +
scale_x_continuous(limits = c(-20,11),breaks = seq(-30, 10, by = 10), labels = abs(breaks)) +
geom_vline(xintercept = -16) +
annotation_custom(leftbend, xmin = -18, xmax = -14, ymin = 30, ymax = 40)
But when I use another dataframe the leftbend icon is plotted in another point because the arguments ymin
,ymax
depend on the input dataframe in order to plot the leftbend icon above the geom_vline()
(xmin
, xmax
are fixed).
Is there a way for the arguments ymin
,ymax
to be adjusted in the input dataframe? I want to have a common output result for all my dataframes.
Thank you in advance!
*if I use another dataframe the result is the following:
The icon is even not plotted because this dataframe don't have GPSSpeed_kmh
values from 30 (ymin) to 40 (ymax) as the other have!
Upvotes: 0
Views: 445
Reputation: 4514
What I have meant with my comment is the following:
library(ggplot2)
library(png) ## for loading the png
library(grid) ## to render the image
y=c(1,2,3)
x=c(0,0,0)
d=data.frame(x=x, y=y) ## make sample dataframe
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)
mi <- 1; ma <- 2.2 ## defining limits for y-axis
ggplot(d, aes(x, y)) + geom_point() +
ylim(mi, ma)+ ## set limits for y-axis
annotation_custom(g, xmin=.020, xmax=.030, ymin=.5*ma)
By relating ymin of annotation_custom to half the max (or whatever fraction you like) of the max of the y-axis, you make sure the image is always at the same position.
Further help can be found here and here.
I hope this helps.
Upvotes: 1