Skip to content

Commit

Permalink
Changed precent formatting to use prettyNum. Added digits, drop0trail…
Browse files Browse the repository at this point in the history
…ing, and zero.print parameters (pass through).
  • Loading branch information
jbryer committed Mar 6, 2019
1 parent b6dab4a commit 5bf9b1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
11 changes: 10 additions & 1 deletion R/likert.options.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#' Possible values are \code{v} (vertical, the default), \code{h}
#' (horizontal), and \code{NULL} (auto fill horizontal and vertical)
#' @param panel.strip.color the background color for panel labels.
#' @param digits the number of significant digits to print.
#' @param drop0trailing logical, indicating if trailing zeros, i.e., "0" after the decimal mark, should be removed
#' @param zero.print logical, character string or NULL specifying if and how zeros should be formatted specially.
#' @param ... included for future expansion.
#'
#' @export
Expand All @@ -52,6 +55,9 @@ likert.options <- function(
legend.position='bottom',
panel.arrange='v',
panel.strip.color='#F0F0F0',
digits = 0,
drop0trailing = FALSE,
zero.print = TRUE,
...
) {
opts <- list(
Expand All @@ -74,7 +80,10 @@ likert.options <- function(
legend = legend,
legend.position = legend.position,
panel.arrange = panel.arrange,
panel.strip.color = panel.strip.color
panel.strip.color = panel.strip.color,
digits = digits,
drop0trailing = drop0trailing,
zero.print = zero.print
)

return(opts)
Expand Down
31 changes: 18 additions & 13 deletions R/plot.likert.bar.r
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,24 @@ likert.bar.plot <- function(l,

if(plot.percent.low) {
p <- p + geom_text(data=lsum, y=ymin, aes(x=Group,
label=paste0(round(low), '%'), group=Item),
label=paste0(prettyNum(low, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%'),
group=Item),
size=text.size, hjust=1, color=text.color)
}
if(plot.percent.high) {
p <- p + geom_text(data=lsum, aes(x=Group, y=100,
label=paste0(round(high), '%'),
label=paste0(prettyNum(high, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%'),
group=Item), size=text.size, hjust=-.2, color=text.color)
}
if(plot.percent.neutral & l$nlevels %% 2 == 1 & include.center) {
if(centered) {
p <- p + geom_text(data=lsum, y=0, aes(x=Group, group=Item,
label=paste0(round(neutral), '%')),
label=paste0(prettyNum(neutral, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, hjust=.5, color=text.color)
} else {
lsum$y <- lsum$low + (lsum$neutral/2)
p <- p + geom_text(data=lsum, aes(x=Group, y=y, group=Item,
label=paste0(round(neutral), '%')),
label=paste0(prettyNum(neutral, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, hjust=.5, color=text.color)
}
}
Expand All @@ -162,7 +163,8 @@ likert.bar.plot <- function(l,
lpercentpos <- ddply(results[results$value > 0,], .(Group, Item), transform,
pos = cumsum(value) - 0.5 * value)
p <- p + geom_text(data = lpercentpos,
aes(x = Group, y = pos, label = paste0(round(value), '%'),
aes(x = Group, y = pos,
label = paste0(prettyNum(value, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%'),
group = Item), size = text.size)
lpercentneg <- results[results$value < 0, ]
if(nrow(lpercentneg) > 0) {
Expand All @@ -175,7 +177,8 @@ likert.bar.plot <- function(l,
pos = cumsum(value) - 0.5 * value)
lpercentneg$pos <- lpercentneg$pos * -1
p <- p + geom_text(data = lpercentneg,
aes(x = Group, y = pos, label = paste0(round(abs(value)), '%')),
aes(x = Group, y = pos,
label = paste0(prettyNum(abs(value), digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size = text.size)
}
}
Expand Down Expand Up @@ -264,25 +267,27 @@ likert.bar.plot <- function(l,
}
if(plot.percent.low) {
p <- p + geom_text(data=lsum, y=ymin, aes(x=Item,
label=paste0(round(low), '%')),
label=paste0(prettyNum(low, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, hjust=1, color=text.color)
}
if(plot.percent.high) {
p <- p + geom_text(data=lsum, y=100, aes(x=Item,
label=paste0(round(high), '%')),
label=paste0(prettyNum(high, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, hjust=-.2, color=text.color)
}
if(plot.percent.neutral & l$nlevels %% 2 == 1 & include.center & !plot.percents) {
if(centered) {
p <- p +
geom_text(data=lsum, y=0,
aes(x=Item, label=paste0(round(neutral), '%')),
aes(x=Item,
label=paste0(prettyNum(neutral, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, hjust=.5, color=text.color)
} else {
lsum$y <- lsum$low + (lsum$neutral/2)
p <- p +
geom_text(data=lsum,
aes(x=Item, y=y, label=paste0(round(neutral), '%')),
aes(x=Item, y=y,
label=paste0(prettyNum(neutral, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, hjust=.5, color=text.color)
}
}
Expand All @@ -295,7 +300,7 @@ likert.bar.plot <- function(l,
pos = cumsum(value) - 0.5*value)
p <- p + geom_text(data=lpercentpos[lpercentpos$variable != center.label,],
aes(x=Item, y=pos,
label=paste0(round(value), '%')),
label=paste0(prettyNum(value, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
lpercentneg <- results[results$value < 0,]
if(nrow(lpercentneg) > 0) {
Expand All @@ -306,14 +311,14 @@ likert.bar.plot <- function(l,
lpercentneg$pos <- lpercentneg$pos * -1
p <- p + geom_text(data=lpercentneg[lpercentneg$variable != center.label,],
aes(x=Item, y=pos,
label=paste0(round(abs(value)), '%')),
label=paste0(prettyNum(abs(value), digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
}
lpercentneutral <- results[results$variable == center.label,]
if(nrow(lpercentneutral) > 0) {
p <- p + geom_text(data=lpercentneutral,
aes(x=Item, y=0,
label=paste0(round(abs(value * 2)), '%')),
label=paste0(prettyNum(abs(value * 2), digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
}
}
Expand Down
2 changes: 1 addition & 1 deletion man/likert.Rd

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

9 changes: 8 additions & 1 deletion man/likert.options.Rd

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

0 comments on commit 5bf9b1e

Please sign in to comment.