Skip to content

Commit

Permalink
ready for CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasH2 committed Feb 8, 2024
1 parent 1354d55 commit b67f904
Show file tree
Hide file tree
Showing 21 changed files with 645 additions and 446 deletions.
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
^.*\.Rproj$
^\.Rproj\.user$
^.gitignore
^.github
^.Rhistory
^readme_files
^vignettes
^ggbrace_
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
.Ruserdata
inst/doc
26 changes: 15 additions & 11 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
Package: ggbrace
Type: Package
Title: Curly Braces in ggplot
Title: Curly Braces in Ggplot
Depends:
R (>= 3.6.0),
ggplot2 (>= 3.3.1)
Imports: ggplot2 (>= 3.3.1)
Version: 0.1.0
R (>= 4.3)
Imports:
ggplot2 (>= 3.4.2),
stats (>= 4.3.1)
Version: 0.1.1
Author: Nicolas Huber
Maintainer: Nicolas Huber <info.huber@aold.de>
Description: Provides three methods to create curly braces in ggplot2 via two functions: geom_brace and stat_brace.
geom_brace in default mode plots a brace based on x and y values manually provided.
geom_brace can plot braces based on the confines of data when the mapping or inherit.aes arguement are given.
Maintainer: Nicolas Huber <info.huber@aol.de>
Description: Provides curly braces in ggplot2 plus matching text.
stat_brace plots braces partially in the confines of data so that the brace is set apart from it.
License: MIT
stat_bracetext plots corresponding text, fitting to the braces from stat_brace.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.2.3
Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
23 changes: 2 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
MIT License

Copyright (c) 2021 Nicolas Huber

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
YEAR: 2024
COPYRIGHT HOLDER: Nicolas Huber
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(geom_brace)
export(stat_brace)
export(stat_bracetext)
import(ggplot2)
import(stats)
87 changes: 39 additions & 48 deletions R/StatBrace.R
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
StatBrace <- ggproto(`_class` = "StatBrace",
`_inherit` = Stat,
required_aes = c("x", "y"),
compute_group = function(
data, scales,
rotate, mid, bending, npoints,
distance=NULL, outerstart=NULL, width=NULL,
direction, outside
) {
x=data$x
y=data$y
StatBrace <- ggplot2::ggproto(
`_class` = "StatBrace",
`_inherit` = Stat,
required_aes = c("x", "y"),
compute_group = function(
data,
scales,
rotate = 0,
mid = NULL,
bending = NULL,
npoints = 100,
distance = NULL,
outerstart = NULL,
width = NULL,
outside = TRUE
){
x = data$x
y = data$y

#before we feed the .seekBrace function we need to change the x/y values
coords <- .coordCorrection(
x=x, y=y,
rotate=rotate, mid=mid,
distance=distance, outerstart=outerstart, width=width,
direction=direction, outside=outside
)[["dataCoords"]]
#before we feed the .seekBrace function we need to change the x/y values
coords <- .coordCorrection(
x = x,
y = y,
rotate = rotate,
mid = mid,
distance = distance,
outerstart = outerstart,
width = width,
outside = outside
)[["dataCoords"]]

#provides data.frame with x and y values to draw the brace
brace <- .seekBrace(x=coords$x, y=coords$y,
rotate=rotate, bending=bending, npoints=npoints)
#provides data.frame with x and y values to draw the brace
brace <- .seekBrace(
x = coords$x,
y = coords$y,
rotate = rotate,
bending = bending,
npoints = npoints
)

return(brace)
}
)

StatBraceLabel <- ggproto("StatBraceLabel", Stat,
required_aes = c("x", "y"),
compute_group = function(
data, scales,
rotate, mid, bending, npoints,
labeldistance,
distance=NULL, outerstart=NULL, width=NULL,
direction, outside
) {
x=data$x
y=data$y

#provides data.frame with an x and y value for the label
coords <- .coordCorrection(
x=x, y=y,
rotate=rotate, mid=mid,
labeldistance=labeldistance,
distance=distance, outerstart=outerstart, width=width,
direction=direction, outside=outside
)[["labelCoords"]]

return(coords)
}
return(brace)
}
)
33 changes: 33 additions & 0 deletions R/StatBraceLabel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
StatBraceLabel <- ggplot2::ggproto(
`_class` = "StatBraceLabel",
`_inherit` = Stat,
compute_group = function(
data,
scales,
rotate = 0,
mid = NULL,
textdistance = NULL,
distance = NULL,
outerstart = NULL,
width = NULL,
outside = TRUE
){
x = data$x
y = data$y

#provides data.frame with an x and y value for the label
coords <- .coordCorrection(
x = x,
y = y,
rotate = rotate,
mid = mid,
textdistance = textdistance,
distance = distance,
outerstart = outerstart,
width = width,
outside = outside
)[["labelCoords"]]

return(coords)
}
)
57 changes: 36 additions & 21 deletions R/coordCorrection.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
#before calculating the brace, this function is used to change the data (mid value, position, etc.)

#' Imports:
#' stats
#'
#' @inheritParams stat_brace
#' @inheritParams stat_bracetext
#' @import stats
#'
#' @param x vector, x values of all data points
#' @param y vector, y value of all data points
.coordCorrection <- function(
x, y,
rotate, mid,
labeldistance=NULL,
distance, outerstart, width,
direction, outside
x,
y,
rotate,
mid,
textdistance=NULL,
distance,
outerstart,
width,
outside
){
#to save if-statements, define 2 axes: a and b
# a is the axis that is enclosed by the brace
# b is the axis along which the brace points
# so for } or {, a would stand for y and b for x
direction <- -sign(rotate/90-1.5)
a <- x
b <- range(y)
if(any(rotate==c(90,270))){
a <- y
b <- range(x)
}else{
a <- x
b <- range(y)
}

#==========================================================#
Expand All @@ -25,37 +39,38 @@
if(!is.null(mid)) a <- c( base[1] , base[1]+diff(base)*mid, base[2] )

#============================================#
#==change coordinates (poisition and width)==#
#==change coordinates (position and width)==#
#============================================#
#outside is TRUE for stat_brace and FALSE for geom_brace
if(outside){
# change position
if(is.null(distance)) distance <- diff(base)/50 #set distance to data points
if(is.null(outerstart)) outerstart <- distance * direction + ifelse(direction>0, max(b), min(b)) #set position
b <- b + outerstart - ifelse(direction>0, min(b), max(b)) #change coordinates
# change width based on the original width
if(is.null(width)) width <- diff(range(b)) / 2 #original width is diff(range(b))
b[b == ifelse(direction>0, max(b), min(b))] <- width + ifelse(direction>0, min(b), max(b))
if(is.null(width)) width <- diff(range(b)) / 2
outerend <- outerstart + width * direction
b <- c(outerstart, outerend)
# b <- b + outerstart - ifelse(direction>0, min(b), max(b)) #change coordinates
# # change width based on the original width
# b[b == ifelse(direction>0, max(b), min(b))] <- width + ifelse(direction>0, min(b), max(b))
}

#=============================#
#==coordinates for the label==#
#=============================#
if(is.null(labeldistance)) labeldistance <- diff(range(b)) / 5
alabel <- median(a)
blabel <- ifelse(direction>0, max(b)+labeldistance, min(b)-labeldistance)
if(is.null(textdistance)) textdistance <- ( diff(range(b)) )/5
alabel <- stats::median(a)
blabel <- ifelse(direction>0, max(b)+textdistance, min(b)-textdistance)

#convert a and b back to x and y
x <- a
y <- b
xlabel <- alabel
ylabel <- blabel
if(any(rotate==c(90,270))){
y <- a
x <- b
ylabel <- alabel
xlabel <- blabel
}else{
x <- a
y <- b
xlabel <- alabel
ylabel <- blabel
}

output <- list(
Expand Down
Loading

0 comments on commit b67f904

Please sign in to comment.