Skip to content

Commit

Permalink
new function plot_2d_img
Browse files Browse the repository at this point in the history
* new function plot_2d_img ... hope it will please you master !

* finally I am back

---------

Co-authored-by: nmouquet <>
  • Loading branch information
nmouquet authored May 5, 2023
1 parent 0414d42 commit 4a3bb6b
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 2 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
graphics,
grDevices,
httr,
imager,
jsonlite,
png,
stats,
tidyr,
utils
Suggests:
ggplot2,
knitr,
rmarkdown,
rphylopic,
testthat (>= 3.0.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export(get_world_basemap)
export(leading_zero)
export(len)
export(multi_merge)
export(plot_2d_img)
export(rename_col)
export(signi)
export(to_binomial_name)
import(graphics)
83 changes: 83 additions & 0 deletions R/plot_2d_img.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' Plot two variables using images png as points
#'
#' @description
#' Plot two variables using images png as points.Works with any king of png, the best stratgegy
#' is to use png with no backgrounds.
#' Parameter scale and size are important to tune to make the plot readable and not to heavy
#' Must be plotted (or saved) on a 1:1 dimension
#'
#' @param df a `data.frame` with three colums x coord,y coord,img_names
#' @param scale a numeric between 0 and 1 (used to resize the images)
#' @param size the parameter size of the function imager::resize (between -0L and -100L)
#' used to reduce the resolution of the image (proportion between 0 and 100%)
#' @param pathimages path to the images
#' @param cexaxis cex.axis of the plot function
#' @param cexlab cex.lab of the plot function
#' @param labelx label of the x axis
#' @param labely label of the y axis
#' @param lm TRUE or FALSE (compute and draw the lm)
#' @param xR x coordinate of the position of the R2 on the plot
#' @param yR y coordinate of the position of the R2 on the plot
#' @param colR color of the R2 on the plot
#' @param cexR size of the R2 on the plot
#' @param colline col of the abline used to illustrate the lm
#' @param lwline lwd of the abline used to illustrate the lm
#' @param ltyline lty of the abline used to illustrate the lm
#' @param ... other plot options; see ?par (as mar or mpg)
#'
#' @export
#'
#' @import graphics
#'
#' @return a plot
#'
#' @examples
#' set.seed(3)
#' df <- cbind.data.frame(x=runif(10, 0, 10),y=runif(10, 0, 10),
#' img_names=paste0("img_",rutils::leading_zero(1:10)))
#' pathimages <- system.file("extdata", "plot2dimg", package = "rutils")
#' plot_2d_img(df,scale=0.2,size=-35L,pathimages,
#' lm=TRUE,xR=1.5,yR=10,colR="gray",cexR=1.5,colline="gray",lwline=2,ltyline=2,
#' labelx="Variable x",labely="Variable y",mar=c(8, 9, 4.1, 2.1),mgp=c(6,2,0),
#' cex.lab=2.5,cex.axis=1.5)
#'

plot_2d_img <- function(df,scale,size,pathimages,cexaxis,cexlab,labelx,labely,lm,xR,
yR,colR,cexR,colline,lwline,ltyline,...)
{

opar <- par(no.readonly = TRUE)
on.exit(par(opar))
par(...)

labx <- colnames(df)[1]
laby <- colnames(df)[2]
minx <- min(stats::na.omit(df[,1]))
maxx <- max(stats::na.omit(df[,1]))*1.2
miny <- min(stats::na.omit(df[,2]))
maxy <- max(stats::na.omit(df[,2]))*1.2

plot(c(0,0),type="n",xlim=c(minx,maxx),ylim=c(miny,maxy),xlab=labelx,ylab=labely)
box(lwd=3)

xp=(abs(maxx-minx))*scale
yp=(abs(maxy-miny))*scale

for (i in 1:nrow(df)) {
img <- imager::load.image(file.path(pathimages,paste0(df[i,3],".png")))
img <- imager::resize(img, size, size, size_z = -100L,size_c = -100L,interpolation_type =1L)
img <- grDevices::as.raster(img)
img[img=='#FFFFFF']=NA #remove the background (white)
rasterImage(img, df[i,1], df[i,2], df[i,1]+xp, df[i,2]+yp,interpolate=TRUE)
rm(img)
}

if (lm){
abline(lm(stats::reformulate("x","y"), data = df),lty=ltyline, lwd=lwline,col=colline)
mods <- summary(lm(stats::reformulate("x","y"), data = df))
text(x=xR, y=yR, paste0("r2= ",round(mods$r.squared,2)),cex=cexR,adj=0,col=colR)
}

invisible(NULL)

}
2 changes: 2 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ library("rutils")

- [`signi()`](https://frbcesab.github.io/rutils/reference/signi.html): Produce produces significance symbols for the values of p

- [`plot_2d_img()`](https://frbcesab.github.io/rutils/reference/plot_2d_img.html): Plot two variables using images png as points


## Contribution

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ library("rutils")
- [`signi()`](https://frbcesab.github.io/rutils/reference/signi.html):
Produce produces significance symbols for the values of p

- [`plot_2d_img()`](https://frbcesab.github.io/rutils/reference/plot_2d_img.html):
Plot two variables using images png as points

## Contribution

Visit the
Expand Down
Binary file added inst/extdata/plot2dimg/img_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added inst/extdata/plot2dimg/img_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions man/plot_2d_img.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4a3bb6b

Please sign in to comment.