Skip to content

Commit

Permalink
Merge pull request #16 from USCCANA/unnecesary-draw
Browse files Browse the repository at this point in the history
Unnecesary draw
  • Loading branch information
gvegayon committed Jun 27, 2024
2 parents f1ff04b + d1f84b1 commit 0ca3399
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 33 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: netplot
Title: Beautiful Graph Drawing
Version: 0.2-0
Version: 0.2-9999
Authors@R: c(
person("George", "Vega Yon", email = "g.vegayon@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-3171-0844")),
person("Porter", "Bischoff", email = "portergbischoff@gmail.com", role = c("aut"), comment = c(ORCID = "0009-0004-6742-6281"))
)
Description: A graph visualization engine that puts an emphasis on
aesthetics at the same time of providing default parameters that yield
Description: A graph visualization engine that emphasizes on
aesthetics at the same time providing default parameters that yield
out-of-the-box-nice visualizations. The package is built on top of
'The Grid Graphics Package' and seamlessly work with 'igraph' and
'network' objects.
Expand Down
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
VERSION:=$(shell Rscript -e 'x<-readLines("DESCRIPTION");cat(gsub(".+[:]\\s*", "", x[grepl("^Vers", x)]))')
PKGNAME:=$(shell Rscript -e 'x<-readLines("DESCRIPTION");cat(gsub(".+[:]\\s*", "", x[grepl("^Package", x)]))')

install:
$(MAKE) clean && R CMD build . && \
install: build
cd ../ && \
R CMD INSTALL $(PKGNAME)_$(VERSION).tar.gz

$(PKGNAME)_$(VERSION).tar.gz:
cd ../ && R CMD build $(PKGNAME)/

$(PKGNAME)_$(VERSION).tar.gz: R/*.R inst/NEWS README.md
R CMD build .
build: $(PKGNAME)_$(VERSION).tar.gz

inst/NEWS: NEWS.md
Rscript -e "rmarkdown::pandoc_convert('NEWS.md', 'plain', output='inst/NEWS')"&& \
Expand All @@ -29,7 +30,8 @@ checkv: $(PKGNAME)_$(VERSION).tar.gz
R CMD check --as-cran --use-valgrind $(PKGNAME)_$(VERSION).tar.gz

clean:
rm -rf $(PKGNAME).Rcheck $(PKGNAME)_$(VERSION).tar.gz
cd ../ && \
rm -rf $(PKGNAME).Rcheck $(PKGNAME)_$(VERSION).tar.gz

.PHONY: man docker
man: R/*
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# netplot 0.2-9999

* Unvalid arguments passed to `nplot()` now raise an error.

* Figures with legends are not drawn twice.


# netplot 0.2-0

* The arguments `skip.vertex`, `skip.edges`, and `skip.arrows` now work as
Expand Down
2 changes: 1 addition & 1 deletion R/geometry.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ rescale_size <- function(size, rel=c(.01, .05, 1)) {
#' @param adj Numeric vector of length 2.
#' @noRd
# @export
fit_coords_to_dev <- function(coords, adj = grDevices::dev.size()) {
fit_coords_to_dev <- function(coords, adj = dev_size()) {

# Making it -1 to 1
yran <- range(coords[,2], na.rm = TRUE)
Expand Down
67 changes: 45 additions & 22 deletions R/netplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,19 @@ NULL

#' @export
#' @rdname nplot
nplot <- function(x, ..., edgelist) UseMethod("nplot")
nplot <- function(x, ..., edgelist) {

# Checking if the passed arguments match the default class
res <- sapply(
...names(),
match.arg,
choices = formalArgs(nplot.default),
several.ok = FALSE
)

UseMethod("nplot")

}

#' @export
#' @rdname nplot
Expand Down Expand Up @@ -137,13 +149,13 @@ nplot.network <- function(
) {

nplot.default(
x = x,
layout = layout,
vertex.size = vertex.size,
x = x,
layout = layout,
vertex.size = vertex.size,
vertex.label = vertex.label,
skip.arrows = skip.arrows,
skip.arrows = skip.arrows,
...,
edgelist = network::as.edgelist(x)
edgelist = network::as.edgelist(x)
)

}
Expand Down Expand Up @@ -205,6 +217,18 @@ netplot_theme <- (function() {

})()

# Function to retrieve the dev.size only if there's an active device
dev_size <- function(...) {

if (length(grDevices::dev.list())) {

grDevices::dev.size(...)

} else
c(7, 7)

}

#' @export
#' @rdname nplot
#' @param edgelist An edgelist.
Expand Down Expand Up @@ -261,12 +285,12 @@ nplot.default <- function(
edgelist
) {

# We turn off the device if not need
if (length(grDevices::dev.list()) == 0L) {
on.exit(
grDevices::dev.off(grDevices::dev.cur())
)
}
# # We turn off the device if not need
# if (length(grDevices::dev.list()) == 0L) {
# on.exit(
# grDevices::dev.off(grDevices::dev.cur())
# )
# }

# listing objects
netenv <- environment()
Expand Down Expand Up @@ -424,11 +448,9 @@ nplot.default <- function(
netenv$xlim <- range(netenv$layout[,1], na.rm=TRUE)
netenv$ylim <- range(netenv$layout[,2], na.rm=TRUE)


# Creating layout
# Solution from this answer https://stackoverflow.com/a/48084527

asp <- grDevices::dev.size()
asp <- dev_size()

lo <- grid::grid.layout(
widths = grid::unit(1, "null"),
Expand Down Expand Up @@ -593,20 +615,21 @@ nplot.default <- function(
#' @param y,... Ignored
print.netplot <- function(x, y = NULL, newpage = TRUE, legend = TRUE, ...) {

# If legend, then we avoid drawing twice
if (length(x$.legend_vertex_fill) && legend) {

color_nodes_legend(x)
return(invisible(x))

}

# Drawing
if (newpage) {
grid::grid.newpage()
}

grid::grid.draw(x)

# If legend
if (legend) {

color_nodes_legend(x)

}

# Storing the value
.Last.netplot$set(x)

Expand Down
2 changes: 1 addition & 1 deletion R/nplot_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ nplot_legend <- function(
gf <- grid::packGrob(grid::frameGrob(), g)

# Adding the legend
legend.args <- do.call(
legend.args <- do.call(
grid::legendGrob,
c(list(labels = labels, pch = pch, gp = gp), list(...))
)
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ ans <- nplot(
UKfaculty,
layout = l,
vertex.color = ~ Group,
vertex.nsides = ~ Group,
vertex.label = nam,
vertex.size.range = c(.01, .04, 4),
vertex.label.col = "black",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ ans <- nplot(
UKfaculty,
layout = l,
vertex.color = ~ Group,
vertex.nsides = ~ Group,
vertex.label = nam,
vertex.size.range = c(.01, .04, 4),
vertex.label.col = "black",
Expand Down
4 changes: 4 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
netplot 0.2-9999

- Unvalid arguments passed to nplot() now raise an error.

netplot 0.2-0

- The arguments skip.vertex, skip.edges, and skip.arrows now work as
Expand Down
Binary file modified man/figures/README-example-1.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 modified man/figures/README-example-2.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 modified man/figures/README-example-3.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 modified man/figures/README-uk-faculty-gradient-1.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 modified man/figures/README-unnamed-chunk-1-1.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 modified man/figures/README-unnamed-chunk-1-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion vignettes/examples.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ nw <- map_base_to_grid(function() gplot(as.matrix(as_adj(UKfaculty)), coord = l_
```

```{r nplot, fig.keep='none'}
nplot(UKfaculty, layout = l_ukf, main="netplot")
nplot(UKfaculty, layout = l_ukf)
grid::grid.text("netplot", x = .05, y=.9)
np <- grid::grid.grab()
```
Expand Down

0 comments on commit 0ca3399

Please sign in to comment.