Skip to content

Instantly share code, notes, and snippets.

@mattjbayly
Last active January 30, 2018 06:33
Show Gist options
  • Save mattjbayly/8234b4c2ebaad6e41ed60f6b2d4bb0ae to your computer and use it in GitHub Desktop.
Save mattjbayly/8234b4c2ebaad6e41ed60f6b2d4bb0ae to your computer and use it in GitHub Desktop.
Aligns new img with x guidelines for plotting a new png over an older one
RastObjAlign <- function(
h_aling=TRUE,
bg_rast=NA,
orig_width_in=11,
orig_height_in=8.5,
insert_img=NA,
x_pos_cent_set=NA,
y_pos_bottom_set=NA
# Get Dimensions in Inkscape with Ctrl+Shift+E export png image
){
dim.img <- dim(bg_rast) # x y and bands
dim.insert <- dim(insert_img) # x y and bands
# Portion to the left for cetner point
x_percent <- x_pos_cent_set/orig_width_in
# Object centerpoint
x_pos_cent <- dim.img[2]*x_percent
# Object right origin
x_pos_left <- x_pos_cent - (dim.insert[2]/2)
x_pos_right <- x_pos_cent + (dim.insert[2]/2)
# VERTICAL
y_percent <- y_pos_bottom_set/orig_height_in
y_pos_bottom <- dim.img[1]*y_percent
y_pos_top <- dim.insert[1]+y_pos_bottom
# return as percent
x_pos_left <- x_pos_left/dim.img[2]
x_pos_right <- x_pos_right/dim.img[2]
y_pos_bottom <- y_pos_bottom/dim.img[1]
y_pos_top <- y_pos_top/dim.img[1]
# return dimensions
fdims <- c(x_pos_left,y_pos_bottom,x_pos_right, y_pos_top)#xleft, ybottom, xright, ytop
return(fdims)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment