Skip to content

Commit

Permalink
Merge pull request #13 from giovannellilab/dev
Browse files Browse the repository at this point in the history
Merge dev branch changes for perimeter/area calculation and mean polygon
  • Loading branch information
davidecrs committed May 14, 2024
2 parents a480cda + 079a7c3 commit e2e502b
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 7 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Authors@R: c(
person("Donato", "Giovannelli", role = "aut",
email = "donato.giovannelli@unina.it"))
Description: Polygon plot is designed to visualize multivariate ranges as opposite to single data points and it is able to show 3 to 6 variables simultaneously showcase the min-max and average for each.
License: CC BY-SA 4.0
License: CC BY-SA 4.0 + file LICENSE
URL: https://github.com/giovannellilab/polygonPlot
BugReports: https://github.com/giovannellilab/polygonPlot/issues
Encoding: UTF-8
Expand All @@ -23,7 +23,9 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
checkmate,
ggplot2
ggplot2,
dplyr,
pracma
Suggests:
knitr,
RefManageR,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export(overlay_polygons)
export(polygonplot)
export(prepare_dataframe)
import(checkmate)
import(dplyr)
import(ggplot2)
importFrom(pracma,polyarea)
importFrom(stats,dist)
8 changes: 8 additions & 0 deletions R/hexagon.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,13 @@
}
}

# Add mean polygon
p = p + .draw_mean_polygon(df_coord)

# Print perimeter and area
message(sprintf("Perimeter: %5.2f", .get_perimeter(df_coord)))
message(sprintf("Area: %5.2f", .get_area(df_coord)))


return(p)
}
58 changes: 58 additions & 0 deletions R/mean-polygon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' Calculates the mean coordinates for further plotting the mean polygon
#'
#' @param df Data frame containing the coordinates in two columns: x and y
#'
#' @return A data.frame with the mean coordinates.
#'
#' @examples
#' .get_mean_coords(data.frame(x=c(8, 8, 22, 24), y=c(12, 15, 8, 8)))
#'
#' @seealso [polygonPlot::.get_perimeter()]
#'
#' @import dplyr
.get_mean_coords = function(df) {

mean_df = data.frame()

# WARNING: this code assumes the order of the points is correct!
for (i in seq(from=1, to=nrow(df), by=2)) {
mean_df = mean_df %>%
bind_rows(
df %>% slice(i, i+1) %>% summarise(x=mean(x), y=mean(y))
)
}

return(mean_df)
}

#' Plots the mean polygon
#'
#' @param df Data frame containing the coordinates in two columns: x and y
#'
#' @return The mean polygon as a `ggplot2` object.
#'
#' @examples
#' .draw_mean_polygon(data.frame(x=c(8, 8, 22, 24), y=c(12, 15, 8, 8)))
#'
#' @seealso [polygonPlot::.get_perimeter()]
#'
#' @import checkmate
#' @import ggplot2
.draw_mean_polygon = function(df) {
checkmate::assertDataFrame(x=df, col.names="named", ncols=2)

# Get mean coordinates from original ones
mean_df = .get_mean_coords(df)

p = ggplot2::geom_polygon(
data=mean_df,
aes(x=x, y=y),
fill="darkgrey",
colour="darkgrey",
alpha=0.0,
linetype=1,
linewidth=0.5
)

return(p)
}
1 change: 1 addition & 0 deletions R/overlay_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#'
#' @import checkmate
#' @import ggplot2

overlay_polygons <- function(plot_list, label_list, matching_color_points=FALSE) {
checkmate::assertList(plot_list, types = "ggplot")
checkmate::assertCharacter(label_list, len = length(plot_list),
Expand Down
7 changes: 7 additions & 0 deletions R/pentagon.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@
angle=288)
}
}

# Add mean polygon
p = p + .draw_mean_polygon(df_coord)

# Print perimeter and area
message(sprintf("Perimeter: %5.2f", .get_perimeter(df_coord)))
message(sprintf("Area: %5.2f", .get_area(df_coord)))

return(p)
}
7 changes: 7 additions & 0 deletions R/square.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,12 @@
}
}

# Add mean polygon
p = p + .draw_mean_polygon(df_coord)

# Print perimeter and area
message(sprintf("Perimeter: %5.2f", .get_perimeter(df_coord)))
message(sprintf("Area: %5.2f", .get_area(df_coord)))

return(p)
}
7 changes: 7 additions & 0 deletions R/triangle.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@
label=tick, size=3.5, angle=60)
}
}

# Add mean polygon
p = p + .draw_mean_polygon(df_coord)

# Print perimeter and area
message(sprintf("Perimeter: %5.2f", .get_perimeter(df_coord)))
message(sprintf("Area: %5.2f", .get_area(df_coord)))

return(p)
}
85 changes: 85 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,88 @@ prepare_dataframe <- function(dataframe){

return(m)
}

#' Calculates the length of each side of the polygon
#'
#' @param df Data frame containing the coordinates in two columns: x and y
#'
#' @return The list of lengths as a numeric vector.
#'
#' @seealso [polygonPlot::.get_perimeter()]
#'
#' @importFrom stats dist
#' @import checkmate
.get_lengths = function(df) {
checkmate::assertDataFrame(x=df, col.names="named", ncols=2)

len_list = c()

# WARNING: this code assumes the order of the points is correct!
# Get the points for each axis 2 by 2 (corresponding to each side)
for (i in seq(from=1, to=nrow(df), by=2)) {
len_list = c(len_list, stats::dist(df[i:(i+1),]))
}

return(len_list)
}

#' Calculates the perimeter of the polygon
#'
#' @param df Data frame containing the coordinates in two columns: x and y
#'
#' @return The perimeter as numeric.
#'
#' @seealso [polygonPlot::.get_area()]
#'
#' @import checkmate
.get_perimeter = function(df) {
checkmate::assertDataFrame(x=df, col.names="named", ncols=2)

return(sum(.get_lengths(df)))
}

#' Collapses the original polygon by linking each segment to the last vertex
#' of the previous segment, basically connecting all sides to the base.
#'
#' @param df Data frame containing the coordinates in two columns: x and y
#'
#' @return The coordinates of the collapsed polygon as a data frame.
#'
#' @seealso [polygonPlot::.get_area()]
#'
.collapse_polygon = function(df) {

# WARNING: this code assumes the order of the points is correct!
# Connect each segment to the base (first vertex after base is number 3)
for (i in seq(from=3, to=nrow(df), by=2)) {
# Get translation in the Cartesian plane
diff_vector = df[(i),] - df[i-1,]

# Connect segment to the last vertex of the previous segment
df[(i),] = df[(i),] - diff_vector # Same as setting as df[i-1,]
df[(i+1),] = df[(i+1),] - diff_vector
}

# Remove duplicated vertices caused by connecting segments
df = unique(df)

return(df)
}

#' Calculates the area of the polygon.
#'
#' @param df Data frame containing the coordinates in two columns: x and y
#'
#' @return The area as numeric.
#'
#' @seealso [polygonPlot::.collapse_polygon()]
#'
#' @import checkmate
#' @importFrom pracma polyarea
.get_area = function(df) {
checkmate::assertDataFrame(x=df, col.names="named", ncols=2)

df = .collapse_polygon(df)

return(pracma::polyarea(x=df$x, y=df$y))
}
26 changes: 26 additions & 0 deletions man/dot-collapse_polygon.Rd

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

24 changes: 24 additions & 0 deletions man/dot-draw_mean_polygon.Rd

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

24 changes: 24 additions & 0 deletions man/dot-get_area.Rd

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

24 changes: 24 additions & 0 deletions man/dot-get_lengths.Rd

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

24 changes: 24 additions & 0 deletions man/dot-get_mean_coords.Rd

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

24 changes: 24 additions & 0 deletions man/dot-get_perimeter.Rd

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

Loading

0 comments on commit e2e502b

Please sign in to comment.