/
barchart.R
131 lines (125 loc) · 4.33 KB
/
barchart.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#' Create a bar chart.
#'
#' @param data The data frame containing the variables to consider.
#' @param x The x-variable to consider.
#' @param y The y-variable to consider.
#' @param fill The color of the bars. Defaults to 'crimson'.
#' @param sort Whether to sort or not the bars. Takes three values
#' 'none' which is the default, 'ascending' or 'descending'.
#' @param paddingWidth The distance between each bar.
#' The value goes from 0 to 0.99 included. Defaults to 0.1.
#' @param xticks Optional. the number of x-axis ticks to consider.
#' @param xFontSize the font size of the x-axis labels. Defaults to 10.
#' @param yFontSize the font size of the y-axis labels. Defaults to 10.
#' @param yticks Optional. The number of y-axis ticks to consider.
#' @param xtitle Optional. The title of the x-axis.
#' @param xtitleFontSize The font size of the x-axis title. Defaults to 16.
#' @param ytitle Optional. The title of the y-axis.
#' @param ytitleFontSize The font size of the y-axis title. Defaults to 16.
#' @param title Optional. The title of the plot.
#' @param titleFontSize The font size of the plot title. Defaults to 22.
#' @param stroke The stroke color of the bars. Defaults to 'crimson'.
#' @param strokeWidth Optional. the stroke width of the bars.
#' @param font The font family to consider for the titles. Defaults to
#' "Verdana, Geneva, Tahoma, sans-serif".
#' @param bgcol The background color of the SVG. Defaults to "#CAD0D3" HEX color.
#' @param opacity The color opacity of the bars (from 0 to 1). Defaults to 1.
#' @param axisCol the color of the x and y axis. It includes the ticks, the labels and titles.
#' Defaults to 'black'.
#' @param width Optional. The width of the SVG output.
#' @param height Optional. The height of the SVG output.
#'
#' @return A SVG bar chart.
#' @export
#'
#' @examples
#'library(ggplot2) #needed for the mpg data frame
#'library(dplyr) #needed for data wrangling
#'
#'mpg %>% group_by(manufacturer) %>%
#' summarise(mean_cty = mean(cty)) %>%
#' barChart(
#' x = "manufacturer",
#' y = "mean_cty",
#' sort = "ascending",
#' xFontSize = 10,
#' yFontSize = 10,
#' fill = "orange",
#' strokeWidth = 1,
#' ytitle = "average cty value",
#' title = "Average City Miles per Gallon by manufacturer",
#' titleFontSize = 16
#' )
barChart <- function(
data,
x,
y,
fill = "crimson",
sort = "none",
paddingWidth = 0.1,
xticks = NULL,
xFontSize = 10,
yFontSize = 10,
yticks = NULL,
xtitle = NULL,
xtitleFontSize = 16,
ytitle = NULL,
ytitleFontSize = 16,
title = NULL,
titleFontSize = 22,
stroke = "crimson",
strokeWidth = NULL,
font = "Verdana, Geneva, Tahoma, sans-serif",
bgcol = "#CAD0D3",
opacity = 1,
axisCol = "black",
width = NULL,
height = NULL
) {
if (is.null(data[[x]]) || is.null(data[[y]])) {
stop("Please check that x and y belong to the specified data frame")
}
if (!(any(c('none',
'ascending',
'descending') %in% sort))) {
stop("
the sort parameter can only take the following values:
'none', 'ascending' or 'descending'
")
}
if (grepl(";", font)) {
stop("please remove the ';' character from your font argument")
}
r2d3::r2d3(
data = data,
script = system.file(
"d3/scatterplot/barchart.js",
package = "ddplot"
),
options = list(
x = x,
y = y,
fill = fill,
sort = sort,
paddingWidth = paddingWidth,
xticks = xticks,
yticks = yticks,
xtitle = xtitle,
ytitle = ytitle,
title = title,
font = font,
stroke = stroke,
strokeWidth = strokeWidth,
width = width,
height = height,
xFontSize = xFontSize,
yFontSize = yFontSize,
xtitleFontSize = xtitleFontSize,
ytitleFontSize = ytitleFontSize,
titleFontSize = titleFontSize,
bgcol = bgcol,
opacity = opacity,
axisCol = axisCol
)
)
}