diff --git a/DESCRIPTION b/DESCRIPTION index c154cbc..1310fdf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,4 +27,5 @@ Imports: data.table, tibble, stringr, - tidyselect + tidyselect, + ggplot2 (>= 3.2.0) diff --git a/NAMESPACE b/NAMESPACE index 58c5e1e..add7b29 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,12 +15,14 @@ export(quick_forestfire) export(quick_graph) export(recursive_graph_join) export(rm_giant_component) +export(show_palette) export(str_replace_sp) export(str_replace_us) export(tidy_colnames) export(u_pull) export(unique_na) export(vsample) +importFrom(ggplot2,aes) importFrom(magrittr,"%<>%") importFrom(magrittr,"%>%") importFrom(rlang,"!!") diff --git a/R/show_palette.R b/R/show_palette.R new file mode 100644 index 0000000..9467942 --- /dev/null +++ b/R/show_palette.R @@ -0,0 +1,86 @@ +#' Show a color palette in a matrix +#' +#' This function is very simillar to the \code{show_cols()} in 'scales'. The +#' main difference is that this function returns a true \code{ggplot2} object. +#' This allows for further customization. +#' +#' @param cols The colors to present in a matrix. If the values are named, these +#' will be used for the cell label. If not, then the color value itself is used +#' for the label. +#' @param num_rows The number of rows to use or if the matrix should be as +#' square as possible. +#' @param label_size The size passed to \code{ggplot2::geom_text()} for the size +#' of the label. Default is 5. +#' @param base_size The base font size passed to \code{theme_bw()}. The default +#' is to use \code{label_size}. +#' @param font_family The font to use for thee plot. Default is "Times". +#' @param ... Any other information to pass to \code{ggplot2::theme()}. +#' +#' @return A \code{ggplot2} object. +#' +#' @examples +#' pal <- c("#3fc5f0", "#42dee1", "#6decb9", "#eef5b2") +#' show_palette(pal) +#' +#' show_palette(pal, num_rows = 1) +#' +#' names(pal) <- paste("color", 1:4) +#' show_palette(pal) +#' +#' @importFrom magrittr %>% +#' @importFrom ggplot2 aes +#' +#' @export show_palette +show_palette <- function(cols, + num_rows = "square", + label_size = 5, + font_family = "Times", + base_size = label_size, + ...) { + + if (is.null(names(cols))) { + col_names <- cols + } else { + col_names <- names(cols) + } + + if (num_rows == "square") { + num_rows <- ceiling(sqrt(length(cols))) + } else if (!is.numeric(num_rows)) { + stop("The `num_rows` argument must be either 'smart' or a numeric value.") + } + num_cols <- ceiling(length(cols) / num_rows) + + col_assignments <- c() + for (j in seq(num_rows, 1)) { + col_assignments <- c(col_assignments, rep(j, num_cols)) + } + row_assignments <- rep(seq(1, num_cols), num_rows) + + tibble::tibble(col_names = col_names, + color_vals = cols, + x = row_assignments[1:length(cols)], + y = col_assignments[1:length(cols)]) %>% + ggplot2::ggplot(aes(x = x, y = y)) + + ggplot2::geom_tile(aes(fill = color_vals), + color = NA) + + ggplot2::geom_text(aes(label = col_names), + size = label_size, + family = font_family) + + ggplot2::scale_fill_identity() + + ggplot2::scale_x_discrete(expand = c(0, 0)) + + ggplot2::scale_y_discrete(expand = c(0, 0)) + + ggplot2::theme_bw( + base_family = font_family, + base_size = base_size + ) + + ggplot2::theme( + axis.text = ggplot2::element_blank(), + axis.title = ggplot2::element_blank(), + axis.ticks = ggplot2::element_blank(), + ... + ) +} + +# for "show_palette" +utils::globalVariables(c("x", "y", "color_vals"), add = TRUE) diff --git a/_pkgdown.yml b/_pkgdown.yml index 74d9bfb..9b4325f 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -41,6 +41,10 @@ reference: - '`str_replace_us`' - '`str_replace_sp`' +- title: styling + contents: + - '`show_palette`' + - title: for 'tidygraph' contents: - '`filter_component_size`' diff --git a/docs/LICENSE.html b/docs/LICENSE.html index 86d74d3..90c1640 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -123,7 +123,7 @@
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
“This License” refers to version 3 of the GNU General Public License.
“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations.
@@ -154,9 +154,9 @@To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.
A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
@@ -164,28 +164,28 @@The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same work.
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work’s users, your or third parties’ legal rights to forbid circumvention of technological measures.
You may convey verbatim copies of the Program’s source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation’s users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
@@ -246,29 +246,29 @@If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party’s predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor’s “contributor version”.
A contributor’s “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor’s essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
@@ -278,37 +278,37 @@A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.
The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.
If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy’s public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
-<one line to give the program's name and a brief idea of what it does.>
-Copyright (C) 2019 Joshua H. Cook
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
<one line to give the program's name and a brief idea of what it does.>
+Copyright (C) 2019 Joshua H. Cook
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
-jhcutils Copyright (C) 2019 Joshua H. Cook
-This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'.
-This is free software, and you are welcome to redistribute it
-under certain conditions; type 'show c' for details.
jhcutils Copyright (C) 2019 Joshua H. Cook
+This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'.
+This is free software, and you are welcome to redistribute it
+under certain conditions; type 'show c' for details.
The hypothetical commands show w
and show c
should show the appropriate parts of the General Public License. Of course, your program’s commands might be different; for a GUI interface, you would use an “about box”.
You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see <http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read <http://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/docs/index.html b/docs/index.html index 550af37..b44bce4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -80,24 +80,24 @@These are a bunch of functions that I find myself declaring and rewriting in a many scripts and analyses.
Full documentation at the ‘pkgdown site’.
You can install ‘jhcutils’ with:
- - +devtools::install_github("jhrcook/jhcutils")
n_unique
- return the number of unique values in a vector.
unique_na
- return the unique values in a vector, omitting NA
.
a <- c(1, 2, 3, NA, 3)
-unique_na(a)
-#> [1] 1 2 3
-b <- list(c(1, 2, 3, NA), c(1, 2, NA, 5))
-unique_na(b)
-#> [[1]]
-#> [1] 1 2 3 NA
-#>
-#> [[2]]
-#> [1] 1 2 NA 5
-unique_na(b, to_unlist = TRUE)
-#> [1] 1 2 3 5
a <- c(1, 2, 3, NA, 3)
+unique_na(a)
+#> [1] 1 2 3
+b <- list(c(1, 2, 3, NA), c(1, 2, NA, 5))
+unique_na(b)
+#> [[1]]
+#> [1] 1 2 3 NA
+#>
+#> [[2]]
+#> [1] 1 2 NA 5
+unique_na(b, to_unlist = TRUE)
+#> [1] 1 2 3 5
minmax
- set limits on a vector of numeric values.
c <- sample(-100:100, 20)
-c
-#> [1] 41 -33 66 28 61 -58 -87 86 -50 -16 -80 5 81 -27 -94 -28 -22
-#> [18] -64 4 9
-minmax(c, -10, 10)
-#> [1] 10 -10 10 10 10 -10 -10 10 -10 -10 -10 5 10 -10 -10 -10 -10
-#> [18] -10 4 9
c <- sample(-100:100, 20)
+c
+#> [1] 41 -33 66 28 61 -58 -87 86 -50 -16 -80 5 81 -27 -94 -28 -22
+#> [18] -64 4 9
+minmax(c, -10, 10)
+#> [1] 10 -10 10 10 10 -10 -10 10 -10 -10 -10 5 10 -10 -10 -10 -10
+#> [18] -10 4 9
u_pull
- works just like dplyr::pull()
except only returns unique values. There are also options to return the values sorted and without NA
using the paramters sorted
and na.rm
, respectively.
vsample
- a safe wrapper for base::sample()
that always assumes you are passing a vector.
# samples from 1:10
-sample(10)
-#> [1] 5 10 2 8 6 1 4 3 9 7
-# just returns 10
-vsample(10)
-#> [1] 10
-
-# samples from 1:5 with replacement
-sample(5, 10, replace = TRUE)
-#> [1] 3 2 2 4 4 4 2 4 1 1
-# samples from `c(5)` with replacement
-vsample(5, 10, replace = TRUE)
-#> [1] 5 5 5 5 5 5 5 5 5 5
# samples from 1:10
+sample(10)
+#> [1] 5 10 2 8 6 1 4 3 9 7
+# just returns 10
+vsample(10)
+#> [1] 10
+
+# samples from 1:5 with replacement
+sample(5, 10, replace = TRUE)
+#> [1] 3 2 2 4 4 4 2 4 1 1
+# samples from `c(5)` with replacement
+vsample(5, 10, replace = TRUE)
+#> [1] 5 5 5 5 5 5 5 5 5 5
str_replace_us
and str_replace_sp
- replace underscores with spaces, or vice vera.
quick_forestfire
and quick_barabasi
- wrapper around tidygraph::play_forestfire
and tidygraph::play_barabasi_albert
except that it will return a tidygraph object with the node attribute "name"
.
forest_gr <- quick_forestfire(10)
-forest_gr
-#> # A tbl_graph: 10 nodes and 31 edges
-#> #
-#> # An undirected simple graph with 1 component
-#> #
-#> # Node Data: 10 x 1 (active)
-#> name
-#> <chr>
-#> 1 A
-#> 2 B
-#> 3 C
-#> 4 D
-#> 5 E
-#> 6 F
-#> # … with 4 more rows
-#> #
-#> # Edge Data: 31 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 2 3
-#> 3 1 3
-#> # … with 28 more rows
-my_plot_fxn(forest_gr) +
- labs(title = "Example of a quick Forest Fire graph model")
forest_gr <- quick_forestfire(10)
+forest_gr
+#> # A tbl_graph: 10 nodes and 31 edges
+#> #
+#> # An undirected simple graph with 1 component
+#> #
+#> # Node Data: 10 x 1 (active)
+#> name
+#> <chr>
+#> 1 A
+#> 2 B
+#> 3 C
+#> 4 D
+#> 5 E
+#> 6 F
+#> # … with 4 more rows
+#> #
+#> # Edge Data: 31 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 2 3
+#> 3 1 3
+#> # … with 28 more rows
+my_plot_fxn(forest_gr) +
+ labs(title = "Example of a quick Forest Fire graph model")
-barabasi_gr <- quick_barabasi(10)
-barabasi_gr
-#> # A tbl_graph: 10 nodes and 9 edges
-#> #
-#> # An undirected simple graph with 1 component
-#> #
-#> # Node Data: 10 x 1 (active)
-#> name
-#> <chr>
-#> 1 A
-#> 2 B
-#> 3 C
-#> 4 D
-#> 5 E
-#> 6 F
-#> # … with 4 more rows
-#> #
-#> # Edge Data: 9 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 1 3
-#> 3 2 4
-#> # … with 6 more rows
-my_plot_fxn(barabasi_gr) +
- labs(title = "Example of a quick Barabasi-Albert graph")
+barabasi_gr <- quick_barabasi(10)
+barabasi_gr
+#> # A tbl_graph: 10 nodes and 9 edges
+#> #
+#> # An undirected simple graph with 1 component
+#> #
+#> # Node Data: 10 x 1 (active)
+#> name
+#> <chr>
+#> 1 A
+#> 2 B
+#> 3 C
+#> 4 D
+#> 5 E
+#> 6 F
+#> # … with 4 more rows
+#> #
+#> # Edge Data: 9 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 1 3
+#> 3 2 4
+#> # … with 6 more rows
+my_plot_fxn(barabasi_gr) +
+ labs(title = "Example of a quick Barabasi-Albert graph")
quick_graph
- randomly selects one of the above random graphs.
recursive_graph_join
- recursively join a list of tidygraph objects.
gr_list <- purrr::map(c(5, 10, 15), quick_forestfire)
-gr <- recursive_graph_join(gr_list)
-gr
-#> # A tbl_graph: 15 nodes and 93 edges
-#> #
-#> # A directed acyclic multigraph with 1 component
-#> #
-#> # Node Data: 15 x 1 (active)
-#> name
-#> <chr>
-#> 1 A
-#> 2 B
-#> 3 C
-#> 4 D
-#> 5 E
-#> 6 F
-#> # … with 9 more rows
-#> #
-#> # Edge Data: 93 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 1 3
-#> 3 2 3
-#> # … with 90 more rows
-my_plot_fxn(gr) +
- labs(title = "Example of joining 3 forest fire graphs")
gr_list <- purrr::map(c(5, 10, 15), quick_forestfire)
+gr <- recursive_graph_join(gr_list)
+gr
+#> # A tbl_graph: 15 nodes and 93 edges
+#> #
+#> # A directed acyclic multigraph with 1 component
+#> #
+#> # Node Data: 15 x 1 (active)
+#> name
+#> <chr>
+#> 1 A
+#> 2 B
+#> 3 C
+#> 4 D
+#> 5 E
+#> 6 F
+#> # … with 9 more rows
+#> #
+#> # Edge Data: 93 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 1 3
+#> 3 2 3
+#> # … with 90 more rows
+my_plot_fxn(gr) +
+ labs(title = "Example of joining 3 forest fire graphs")
filter_component_size
- filter the components of a tidygraph object by their individual number of nodes (order).
gr <- tidygraph::bind_graphs(quick_forestfire(4, name = LETTERS),
- quick_forestfire(6, name = letters))
-igraph::count_components(gr)
-#> [1] 2
-igraph::count_components(filter_component_size(gr, min_size = 5))
-#> Warning: `as_quosure()` requires an explicit environment as of rlang 0.3.0.
-#> Please supply `env`.
-#> This warning is displayed once per session.
-#> [1] 1
-igraph::count_components(filter_component_size(gr, max_size = 5))
-#> [1] 1
gr <- tidygraph::bind_graphs(quick_forestfire(4, name = LETTERS),
+ quick_forestfire(6, name = letters))
+igraph::count_components(gr)
+#> [1] 2
+igraph::count_components(filter_component_size(gr, min_size = 5))
+#> Warning: `as_quosure()` requires an explicit environment as of rlang 0.3.0.
+#> Please supply `env`.
+#> This warning is displayed once per session.
+#> [1] 1
+igraph::count_components(filter_component_size(gr, max_size = 5))
+#> [1] 1
get/rm_giant_component
- either return only or everything except the giant component of a graph (i.e. the component with the most number of nodes).
gr_large <- quick_forestfire(10, name = LETTERS)
-gr_small <- quick_forestfire(5, name = letters)
-gr <- tidygraph::bind_graphs(gr_large, gr_small)
-gr
-#> # A tbl_graph: 15 nodes and 28 edges
-#> #
-#> # A directed acyclic simple graph with 2 components
-#> #
-#> # Node Data: 15 x 1 (active)
-#> name
-#> <chr>
-#> 1 A
-#> 2 B
-#> 3 C
-#> 4 D
-#> 5 E
-#> 6 F
-#> # … with 9 more rows
-#> #
-#> # Edge Data: 28 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 2 3
-#> 3 1 3
-#> # … with 25 more rows
-get_giant_component(gr)
-#> # A tbl_graph: 10 nodes and 19 edges
-#> #
-#> # A directed acyclic simple graph with 1 component
-#> #
-#> # Node Data: 10 x 1 (active)
-#> name
-#> <chr>
-#> 1 A
-#> 2 B
-#> 3 C
-#> 4 D
-#> 5 E
-#> 6 F
-#> # … with 4 more rows
-#> #
-#> # Edge Data: 19 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 2 3
-#> 3 1 3
-#> # … with 16 more rows
-rm_giant_component(gr)
-#> # A tbl_graph: 5 nodes and 9 edges
-#> #
-#> # A directed acyclic simple graph with 1 component
-#> #
-#> # Node Data: 5 x 1 (active)
-#> name
-#> <chr>
-#> 1 a
-#> 2 b
-#> 3 c
-#> 4 d
-#> 5 e
-#> #
-#> # Edge Data: 9 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 1 3
-#> 3 2 3
-#> # … with 6 more rows
gr_large <- quick_forestfire(10, name = LETTERS)
+gr_small <- quick_forestfire(5, name = letters)
+gr <- tidygraph::bind_graphs(gr_large, gr_small)
+gr
+#> # A tbl_graph: 15 nodes and 28 edges
+#> #
+#> # A directed acyclic simple graph with 2 components
+#> #
+#> # Node Data: 15 x 1 (active)
+#> name
+#> <chr>
+#> 1 A
+#> 2 B
+#> 3 C
+#> 4 D
+#> 5 E
+#> 6 F
+#> # … with 9 more rows
+#> #
+#> # Edge Data: 28 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 2 3
+#> 3 1 3
+#> # … with 25 more rows
+get_giant_component(gr)
+#> # A tbl_graph: 10 nodes and 19 edges
+#> #
+#> # A directed acyclic simple graph with 1 component
+#> #
+#> # Node Data: 10 x 1 (active)
+#> name
+#> <chr>
+#> 1 A
+#> 2 B
+#> 3 C
+#> 4 D
+#> 5 E
+#> 6 F
+#> # … with 4 more rows
+#> #
+#> # Edge Data: 19 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 2 3
+#> 3 1 3
+#> # … with 16 more rows
+rm_giant_component(gr)
+#> # A tbl_graph: 5 nodes and 9 edges
+#> #
+#> # A directed acyclic simple graph with 1 component
+#> #
+#> # Node Data: 5 x 1 (active)
+#> name
+#> <chr>
+#> 1 a
+#> 2 b
+#> 3 c
+#> 4 d
+#> 5 e
+#> #
+#> # Edge Data: 9 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 1 3
+#> 3 2 3
+#> # … with 6 more rows
num_qual_neighbors
- to be used with tidygraph::map_local_int()
to count the number of neighbors that satisfy a logical expression that is applied to the node attributes of the neighborhood.
gr <- quick_barabasi(30)
-gr
-#> # A tbl_graph: 30 nodes and 29 edges
-#> #
-#> # An undirected simple graph with 1 component
-#> #
-#> # Node Data: 30 x 1 (active)
-#> name
-#> <chr>
-#> 1 AB
-#> 2 AC
-#> 3 AD
-#> 4 AE
-#> 5 AF
-#> 6 AG
-#> # … with 24 more rows
-#> #
-#> # Edge Data: 29 x 2
-#> from to
-#> <int> <int>
-#> 1 1 2
-#> 2 1 3
-#> 3 2 4
-#> # … with 26 more rows
-my_plot_fxn(gr)
gr <- quick_barabasi(30)
+gr
+#> # A tbl_graph: 30 nodes and 29 edges
+#> #
+#> # An undirected simple graph with 1 component
+#> #
+#> # Node Data: 30 x 1 (active)
+#> name
+#> <chr>
+#> 1 AB
+#> 2 AC
+#> 3 AD
+#> 4 AE
+#> 5 AF
+#> 6 AG
+#> # … with 24 more rows
+#> #
+#> # Edge Data: 29 x 2
+#> from to
+#> <int> <int>
+#> 1 1 2
+#> 2 1 3
+#> 3 2 4
+#> # … with 26 more rows
+my_plot_fxn(gr)
-# number of neighbors with a "B" in their name
-B_gr <- gr %>%
- mutate(name_with_B = map_local_int(
- .f = num_qual_neighbors,
- lgl_filter = rlang::expr(stringr::str_detect(name, "B"))
- ))
-
-B_gr %N>%
- filter(name_with_B > 0) %>%
- my_plot_fxn()
+# number of neighbors with a "B" in their name
+B_gr <- gr %>%
+ mutate(name_with_B = map_local_int(
+ .f = num_qual_neighbors,
+ lgl_filter = rlang::expr(stringr::str_detect(name, "B"))
+ ))
+
+B_gr %N>%
+ filter(name_with_B > 0) %>%
+ my_plot_fxn()
get_node_index
- returns the indices of the nodes that pass the expression evaluted in ’dplyr::filter()`.
# simple equalities
-get_node_index(quick_barabasi(10), name == "B")
-#> [1] 2
-get_node_index(quick_barabasi(10), name %in% c("B", "C", "D"))
-#> [1] 2 3 4
-# can also evaluate functions
-get_node_index(quick_barabasi(10), stringr::str_detect(name, "A|B|C"))
-#> [1] 1 2 3
# simple equalities
+get_node_index(quick_barabasi(10), name == "B")
+#> [1] 2
+get_node_index(quick_barabasi(10), name %in% c("B", "C", "D"))
+#> [1] 2 3 4
+# can also evaluate functions
+get_node_index(quick_barabasi(10), stringr::str_detect(name, "A|B|C"))
+#> [1] 1 2 3
document_df
- prints the framework for documenting a data frame object.
dat <- tibble::tibble(x = c(LETTERS[1:5]),
- y = c(1:5),
- z = list(rep(list(1:3), 5)))
-dat
-#> # A tibble: 5 x 3
-#> x y z
-#> <chr> <int> <list>
-#> 1 A 1 <list [5]>
-#> 2 B 2 <list [5]>
-#> 3 C 3 <list [5]>
-#> 4 D 4 <list [5]>
-#> 5 E 5 <list [5]>
-document_df(dat)
-#> #' @format a tibble (5 x 3)
-#> #' \describe{
-#> #' \item{x}{}
-#> #' \item{y}{}
-#> #' \item{z}{}
-#> #' }
dat <- tibble::tibble(x = c(LETTERS[1:5]),
+ y = c(1:5),
+ z = list(rep(list(1:3), 5)))
+dat
+#> # A tibble: 5 x 3
+#> x y z
+#> <chr> <int> <list>
+#> 1 A 1 <list [5]>
+#> 2 B 2 <list [5]>
+#> 3 C 3 <list [5]>
+#> 4 D 4 <list [5]>
+#> 5 E 5 <list [5]>
+document_df(dat)
+#> #' @format a tibble (5 x 3)
+#> #' \describe{
+#> #' \item{x}{}
+#> #' \item{y}{}
+#> #' \item{z}{}
+#> #' }
Show a color palette in a matrix
This function is very simillar to the show_cols()
in 'scales'. The
+main difference is that this function returns a true ggplot2
object.
+This allows for further customization.
show_palette(cols, num_rows = "square", label_size = 5, + font_family = "Times", base_size = label_size, ...)+ +
cols | +The colors to present in a matrix. If the values are named, these +will be used for the cell label. If not, then the color value itself is used +for the label. |
+
---|---|
num_rows | +The number of rows to use or if the matrix should be as +square as possible. |
+
label_size | +The size passed to |
+
font_family | +The font to use for thee plot. Default is "Times". |
+
base_size | +The base font size passed to |
+
... | +Any other information to pass to |
+
A ggplot2
object.
++show_palette(pal, num_rows = 1)+