Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual text colouring #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions R/likert.options.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#' @param plot.percents whether to label each category/bar.
#' @param text.size size of text attributes.
#' @param text.color color of text attributes.
#' @param text.color.manual.pos individual color specification of positive text attributes.
#' @param text.color.manual.neg individual color specification of negative text attributes.
#' @param text.color.manual.neg individual color specification of neutral text attributes.
#' @param centered if true, the bar plot will be centered around zero such that
#' the lower half of levels will be negative.
#' @param include.center if TRUE, include the center level in the plot otherwise
Expand Down Expand Up @@ -48,6 +51,9 @@ likert.options <- function(
plot.percents = FALSE,
text.size = 3,
text.color = 'black',
text.color.manual.pos = NULL,
text.color.manual.neg = NULL,
text.color.manual.neutral = NULL,
centered = TRUE,
include.center = TRUE,
ordered = TRUE,
Expand Down Expand Up @@ -75,6 +81,9 @@ likert.options <- function(
plot.percents = plot.percents,
text.size = text.size,
text.color = text.color,
text.color.manual.pos = text.color.manual.pos,
text.color.manual.neg = text.color.manual.neg,
text.color.manual.neutral = text.color.manual.neutral,
centered = centered,
include.center = include.center,
ordered = ordered,
Expand Down
42 changes: 29 additions & 13 deletions R/plot.likert.bar.r
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
utils::globalVariables(c('value','Group','variable','low','Item','high',
'neutral','x','y','pos','ddply','.','colors','low.color',
'neutral.color.ramp','high.color','neutral.color','wrap',
'wrap.grouping','centered','include.center','legend',
'plot.percent.low','text.size','text.color','plot.percent.high',
'plot.percent.neutral','plot.percents','panel.strip.color',
'panel.arrange','legend.position'))
'neutral','x','y','pos','ddply','.','colors',
'low.color', 'neutral.color.ramp','high.color',
'neutral.color','wrap', 'wrap.grouping','centered',
'include.center','legend', 'plot.percent.low',
'text.size','text.color', 'text.color.manual.pos',
'text.color.manual.neutral', 'text.color.manual.neg',
'plot.percent.high', 'plot.percent.neutral',
'plot.percents','panel.strip.color', 'panel.arrange',
'legend.position'))

#' Bar Plot for Likert Items.
#'
Expand Down Expand Up @@ -67,6 +70,14 @@ likert.bar.plot <- function(l,
}
}

if (!any(nzchar(text.color.manual.pos))) {
text.color.manual.pos <- text.color
}

if(!any(nzchar(text.color.manual.neg))) {
text.color.manual.neg <- text.color
}

lsum <- summary(l, center=center)

p <- NULL
Expand Down Expand Up @@ -167,6 +178,7 @@ likert.bar.plot <- function(l,
#warning('plot.percents is not currently supported for grouped analysis.')
lpercentpos <- ddply(results[results$value > 0,], .(Group, Item), transform,
pos = cumsum(value) - 0.5 * value)
lpercentpos$text.color.manual.pos <- text.color.manual.pos
p <- p + geom_text(data = lpercentpos,
aes(x = Group, y = pos,
label = paste0(prettyNum(value, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%'),
Expand All @@ -180,6 +192,7 @@ likert.bar.plot <- function(l,
.(Group, Item),
transform,
pos = cumsum(value) - 0.5 * value)
lpercentneg$text.color.manual.neg <- text.color.manual.neg
lpercentneg$pos <- lpercentneg$pos * -1
p <- p + geom_text(data = lpercentneg,
aes(x = Group, y = pos,
Expand Down Expand Up @@ -308,8 +321,9 @@ 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,
color=text.color.manual.pos,
label=paste0(prettyNum(value, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
size=text.size)
lpercentneg <- results[results$value < 0,]
if(nrow(lpercentneg) > 0) {
lpercentneg <- lpercentneg[nrow(lpercentneg):1,]
Expand All @@ -318,22 +332,23 @@ likert.bar.plot <- function(l,
pos = cumsum(value) - 0.5*value)
lpercentneg$pos <- lpercentneg$pos * -1
p <- p + geom_text(data=lpercentneg[lpercentneg$variable != center.label,],
aes(x=Item, y=pos,
aes(x=Item, y=pos, color=text.color.manual.neg,
label=paste0(prettyNum(abs(value), digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
size=text.size)
}
lpercentneutral <- results[results$variable == center.label,]
lpercentneutral$text.color.manual.neutral <- text.color.manual.neutral
if(nrow(lpercentneutral) > 0 & centered) {
p <- p + geom_text(data=lpercentneutral,
aes(x=Item, y=0,
aes(x=Item, y=0, color=text.color.manual.neutral,
label=paste0(prettyNum(abs(value * 2), digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
size=text.size)
}
if(nrow(lpercentneutral) > 0 & !centered) {
p <- p + geom_text(data=lpercentpos[lpercentpos$variable == center.label,],
aes(x=Item, y=pos,
aes(x=Item, y=pos, color=text.color.manual.neutral,
label=paste0(prettyNum(value, digits=digits, drop0trailing=drop0trailing, zero.print=zero.print), '%')),
size=text.size, color=text.color)
size=text.size)
}
}
p <- p +
Expand All @@ -354,6 +369,7 @@ likert.bar.plot <- function(l,
p <- p + scale_y_continuous(labels=abs_formatter,
limits=c(ymin - ybuffer, ymax + ybuffer))
p <- p + theme(legend.position=legend.position)
p <- p + scale_color_identity()

attr(p, 'item.order') <- levels(results$Item)
class(p) <- c('likert.bar.plot', class(p))
Expand Down