## There is a bug in likert:::plot.likert.
## The centered argument is not handled correctly.
## In addition to centering, it also flips the order.
## Here it is:
likert:::plot.likert(results, type = "bar", centered = TRUE, ##
correct order
group.order = c("Band 3", "Band 4"))
likert:::plot.likert(results, type = "bar", centered = FALSE, ## wrong
order
group.order = c("Band 3", "Band 4"))
## I am including the maintainer of the likert package in the cc for
this response.
## In the meantime, you can get the picture you want from the likert
## plot in the HH package.
##
## install.packages("HH") ## if you don't have HH yet.
##
## this uses the same colors as the likert:::plot.likert function.
## See ?likert.options for details
lpl.col <- c(low.color = "#D8B365", neutral.color =
"grey90",
high.color = "#5AB4AC")
HH::plot.likert(results, col=lpl.col,
main="equivalent to 'centered=TRUE' with different
layout")
HH::plot.likert(results, col=lpl.col, ReferenceZero=0,
main="equivalent to 'centered=FALSE' with different
layout")
## HH::plot.likert defaults to a different layout than likert::plot.likert
## this makes the necessary changes to get the intended plot
results.renamed <- results
names(results.renamed[[1]])[1:2] <- names(results[[1]])[2:1]
HH::plot.likert(results.renamed, col=lpl.col, layout=c(1, 5),
main="equivalent to 'centered=TRUE'")
HH::plot.likert(results.renamed, col=lpl.col, ReferenceZero=0, layout=c(1, 5),
main="equivalent to 'centered=FALSE'")
## for completeness, this is how to get the intended plot directly
from the HH package
df2 <- reshape2::melt(df, id.vars="band")
df3 <- aggregate(factor(value) ~ band + variable, data=df2, FUN=table)
HH::likert(band ~ . | variable , data=df3, as.percent=TRUE, layout=c(1,5),
col=lpl.col,
main="equivalent to 'centered=TRUE' with different
layout")
HH::likert(band ~ . | variable , data=df3, as.percent=TRUE, layout=c(1,5),
col=lpl.col, ReferenceZero=0,
main="equivalent to 'centered=FALSE' with different
layout")
On Wed, Aug 23, 2017 at 9:38 PM, Jeff Reichman <reichmanj at
sbcglobal.net> wrote:> R- Help Forum
>
>
>
> Working with the "likert" package and I can't figure out why
my "bar" graphs
> are backwards (see attached). The percentages are place correctly but the
> bars are backwards.
>
>
>
> #Sample code
>
> # libraries
>
> library(likert)
>
>
>
> # create data
>
> band <- c("Band 3","Band 3","Band
3","Band 3","Band 3","Band 3","Band
> 3","Band 3","Band 3","Band 3",
>
> "Band 4","Band 4","Band
4","Band 4","Band 4","Band 4","Band
> 4","Band 4","Band 4","Band 4")
>
> q1 <- factor(c(1,1,2,2,2,2,1,1,1,2,1,1,1,1,1,3,2,1,1,1),levels >
c("1","2","3"))
>
> q2 <- factor(c(1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1),levels >
c("1","2","3"))
>
> q3 <- factor(c(2,2,2,2,2,2,2,2,2,2,1,3,2,2,2,2,2,2,2,2),levels >
c("1","2","3"))
>
> q4 <- factor(c(3,3,3,3,3,3,3,3,2,1,2,2,3,3,3,3,3,3,3,3),levels >
c("1","2","3"))
>
> q5 <- factor(c(2,2,2,2,3,1,1,2,2,2,2,3,3,3,3,3,3,1,1,3),levels >
c("1","2","3"))
>
>
>
> df <- data.frame(band,q1,q2,q3,q4,q5)
>
>
>
> summary(df)
>
> results <- likert(df[,2:6])
>
> summary(results)
>
>
>
> # bar plot, this plots correctly
>
> plot(results, type="bar")
>
>
>
> # group by 'Band Level', this plots the bars backwards
>
> results <- likert(df[,2:6], grouping = df$band)
>
> plot(results, type = "bar", centered = FALSE, group.order =
c("Band 3",
> "Band 4"))
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.