Skip to content
Prev 377216 / 398502 Next

Different stack barplots - same color legends

## reminder on how the levels= argument to factor works

mydata <- matrix(1:8,  nrow=4, ncol=2, dimnames=list(letters[1:4],
LETTERS[1:2]))
dput(mydata)

Factor.wrong <- factor(c("mm", "cm", "m", "km"))
levels(Factor.wrong) ## alphabetical order, not meaning order

Factor.right <- factor(c("mm", "cm", "m", "km"),
                       levels=c("mm", "cm", "m", "km"))
levels(Factor.right) ## meaning order



library(HH) ## for the likert function

## dput(teamq[1:10,7:8])
teamq10x78 <-
structure(list(`Ik volg bijscholing om mijn opleiders-kwaliteiten op
peil te houden` = c("de situatie in hoge mate van toepassing is voor u
of uw supervisorengroep",
"de situatie in hoge mate van toepassing is voor u of uw supervisorengroep",
"de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep",
"de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep",
"de situatie in geringe mate van toepassing is voor u of uw supervisorengroep",
"de situatie enigszins van toepassing is voor u of uw supervisorengroep",
"de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep",
"de situatie in hoge mate van toepassing is voor u of uw supervisorengroep",
"de situatie in hoge mate van toepassing is voor u of uw supervisorengroep",
"de situatie in zeer hoge mate van toepassing is voor u of uw supervisorengroep"
), `Ik weet precies wat de ?modernisering van de opleiding? inhoudt` =
c("de situatie in hoge mate van toepassing is voor u of uw
supervisorengroep",
"de situatie in hoge mate van toepassing is voor u of uw supervisorengroep",
"de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep",
"de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep",
"de situatie in geringe mate van toepassing is voor u of uw supervisorengroep",
"de situatie enigszins van toepassing is voor u of uw supervisorengroep",
"de situatie in geringe mate van toepassing is voor u of uw supervisorengroep",
"de situatie in geringe mate van toepassing is voor u of uw supervisorengroep",
"de situatie enigszins van toepassing is voor u of uw supervisorengroep",
"de situatie in hoge mate van toepassing is voor u of uw supervisorengroep"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))


## This is from Google translate

## Ik weet precies wat de ?modernisering van de opleiding? inhoudt
## I know exactly what the "modernization of the training"

## Ik volg bijscholing om mijn opleiders-kwaliteiten op peil te houden
## I follow training to keep my grades at level trainers


## ## This is your order of levels from Mon, Oct 22, 2018 at 1:30 PM
## "de situatie in zeer geringe mate van toepassing is voor u of uw
supervisorengroep"
## "de situatie in geringe mate van toepassing is voor u of uw
supervisorengroep"
## "de situatie enigszins van toepassing is voor u of uw supervisorengroep"
## "de situatie in hoge mate van toepassing is voor u of uw supervisorengroep"
## "de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep"

## ## This is from Google translate
## the situation very little applies to you or your group supervisor
## the situation slightly applies to you or your group supervisor
## the situation somewhat applies to you or your group supervisor
## the situation is highly applicable to you or your group supervisor
## the situation very largely applies to you or your group supervisor



sapply(teamq10x78, table)
likert(t(sapply(teamq10x78, table)))
likert(t(sapply(teamq10x78, table)),
       auto.key=list(columns=1, border=TRUE),
       main="character values are sorted alphabetically, we will use
factors in the next figure")

object.size(teamq10x78)
## more rows
object.size(rbind(teamq10x78,teamq10x78,teamq10x78,teamq10x78,teamq10x78,
                  teamq10x78,teamq10x78,teamq10x78,teamq10x78,teamq10x78,
                  teamq10x78,teamq10x78,teamq10x78,teamq10x78,teamq10x78,
                  teamq10x78,teamq10x78,teamq10x78,teamq10x78,teamq10x78))

situatie.levels <- c(
  "de situatie in zeer geringe mate van toepassing is voor u of uw
supervisorengroep",
  "de situatie in geringe mate van toepassing is voor u of uw
supervisorengroep",
  "de situatie enigszins van toepassing is voor u of uw supervisorengroep",
  "de situatie in hoge mate van toepassing is voor u of uw supervisorengroep",
  "de situatie in zeer hoge mate van toepassing is voor u of uw
supervisorengroep")

teamf <- tibble::as.tibble(
  lapply(teamq10x78,
         function(x, levels) factor(x, levels=levels),
         levels=situatie.levels)
)
names(teamf) <- names(teamq10x78) ## lapply replaced space and quote
characters with "."
## and each column is a factor with properly ordered labels.
sapply(teamf, class)
sapply(teamf, levels) ## all five levels appear even though this
example observed only four

object.size(teamf) ## bigger here
## significantly smaller for more rows
object.size(rbind(teamf,teamf,teamf,teamf,teamf,
                  teamf,teamf,teamf,teamf,teamf,
                  teamf,teamf,teamf,teamf,teamf,
                  teamf,teamf,teamf,teamf,teamf))

sapply(teamf, table) ## these are the counts of responses by question

likert(t(sapply(teamf, table)),
       auto.key=list(columns=1, border=TRUE),
       main="the middle group enigszins is by default split equally
between negative and positive")

likert(t(sapply(teamf, table)),
       auto.key=list(columns=1, border=TRUE),
       main="based on your color scheme, I am putting enigszins on the
negative side",
       ReferenceZero=3.5,
       col=c("yellow","sandybrown","orange", "darkolivegreen","green"))


## I am adding a third question with some "zeer geringe" values

teamf[,"Extra Question"] <- teamf[,2]
teamf[1:2, 3] <- situatie.levels[1]

likert(t(sapply(teamf, table)),
       auto.key=list(columns=1, border=TRUE),
       main="based on your color scheme, I am putting enigszins on the
negative side",
       ReferenceZero=3.5,
       col=c("yellow","sandybrown","orange", "darkolivegreen","green"))


## I find the color scheme unsatisfactory.
## The break point between sandybrown and orange is not distinct.
## I would prefer a darker green on the right side.
## try
RColorBrewer::display.brewer.all()
## and see if sny of them work for you.

display.brewer.pal(6, "RdYlGn")

RYG5 <- brewer.pal(6, "RdYlGn")[-4]

likert(t(sapply(teamf, table)),
       auto.key=list(columns=1, border=TRUE),
       main="based on your color scheme, I am putting enigszins on the
negative side",
       ReferenceZero=3.5,
       col=RYG5)



On Wed, Oct 31, 2018 at 5:56 PM, P. Roberto Bakker
<robertobakker at gmail.com> wrote: