[R] Different stack barplots - same color legends

P. Roberto Bakker
2018-10-31T21:56:05Z

Hi Rich,

Thank you for your answer.
The sentences are strings (likert scale: 'the situation is highly
applicable to me' etc - in Dutch), or column labels; it may be confusing as
it is in Dutch. Below I show you part of the dataframe with my annotation
added (string/column lable) to give you an idea.
I need to change the likert strings into numeric (1:5). And this is a
challenge somehow.
With dplyr, plyr it did not work.
After I have the numeric version then I can stack them as suggested by
David.

PART OF THE DATAFRAME
>> >>>> >   `Ik volg bijscholing om mijn
opleiders-kwaliteiten op peil te
>> >>>> > houden` COLUMN LABEL
>> >>>> >
>> >>>> >   <chr>
>> >>>> >
>> >>>> > 1 de situatie in hoge mate van toepassing is voor
u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> > 2 de situatie in zeer hoge mate van toepassing is
voor u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> > 3 de situatie in zeer hoge mate van toepassing is
voor u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> > 4 de situatie in geringe mate van toepassing is
voor u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> >   `Ik weet precies wat de ?modernisering van de
opleiding?
inhoudt` COLUMN LABEL
>> >>>> >
>> >>>> >   <chr>
>> >>>> >
>> >>>> > 1 de situatie in hoge mate van toepassing is voor
u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> > 2 de situatie in zeer hoge mate van toepassing is
voor u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> > 3 de situatie in zeer hoge mate van toepassing is
voor u of uw
>> >>>> > supervisorengroep LIKERT STRING
>> >>>> > 4 de situatie in geringe mate van toepassing is
voor u of uw
>> >>>> > supervisorengroep LIKERT STRING



Op wo 31 okt. 2018 om 20:28 schreef Richard M. Heiberger <rmh at
temple.edu>:

> What you sent looks like a set of column labels, not the actual numeric
> data.
>
> You might want to convert them to factors where you control the order
> of the levels.
> > Factor.wrong <- factor(c("mm", "cm",
"m", "km"))
> > levels(Factor.wrong) ## alphabetical order, not meaning order
> [1] "cm" "km" "m"  "mm"
> >
> > Factor.right <- factor(c("mm", "cm",
"m", "km"),
> +                        levels=c("mm", "cm",
"m", "km"))
> > levels(Factor.right) ## meaning order
> [1] "mm" "cm" "m"  "km"
>
> Or you might want to construct a matrix of counts of your data and plot
> that.
>
> Rich
>
>
> On Wed, Oct 31, 2018 at 1:53 PM, P. Roberto Bakker
> <robertobakker at gmail.com> wrote:
> > This is part of the output text
> >
> > "de situatie in hoge mate van toepassing is voor u of uw
> supervisorengroep", STRING
> > "de situatie in hoge mate van toepassing is voor u of uw
> supervisorengroep", STRING
> > "de situatie enigszins van toepassing is voor u of uw
> supervisorengroep", STRING
> > "de situatie in hoge mate van toepassing is voor u of uw
> supervisorengroep" STRINK
> > ), `Ik waardeer de inbreng van de aios in de afdelingsvergadering`
> COLUMN LABEL= c("de
> > situatie in hoge mate van toepassing is voor u of uw
supervisorengroep",
> STRING
> > "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 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 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 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 hoge mate van toepassing is voor u of uw
> supervisorengroep",
> > "de situatie in hoge mate van toepassing is voor u of uw
> supervisorengroep",
> >
> > Op wo 31 okt. 2018 om 16:24 schreef Richard M. Heiberger <rmh at
temple.edu
> >:
> >>
> >> part is fine.  just be sure that the small part causes the
problem.
> >> I will need that to investigate what is happening.
> >>
> >>
> >> On Wed, Oct 31, 2018 at 11:15 AM, P. Roberto Bakker
> >> <robertobakker at gmail.com> wrote:
> >> > It is a very long result text. I can send it to you, or is
part of it
> >> > ok?[
> >> >
> >> > Op wo 31 okt. 2018 om 14:27 schreef Richard M. Heiberger
> >> > <rmh at temple.edu>:
> >> >>
> >> >> Please send me the
> >> >> dput(teamq)
> >> >>
> >> >>
> >> >> On Wed, Oct 31, 2018 at 03:51 P. Roberto Bakker
> >> >> <robertobakker at gmail.com>
> >> >> wrote:
> >> >>>
> >> >>> Thank you for you information. Package 'HH'
is interesting.
> >> >>>
> >> >>> Now I find another problem when using
'likert(teamq)'
> >> >>> I get an error message:
> >> >>> > likert(teamq)
> >> >>> Error in dimnames(x) <- `*vtmp*` :
> >> >>>   length of 'dimnames' [2] not equal to array
extent
> >> >>>
> >> >>> I checked:
> >> >>> > dim(teamq)
> >> >>> [1] 4 2
> >> >>> > ncol(teamq)
> >> >>> [1] 2
> >> >>> So it should be good.
> >> >>>
> >> >>> I used 'make.names' , in case the spaces in
the variable names would
> >> >>> be a
> >> >>> problem.
> >> >>> Same error.
> >> >>>
> >> >>> What could I do?
> >> >>>
> >> >>> Best and thank you in advance.
> >> >>> Roberto
> >> >>>
> >> >>>
> >> >>> Op ma 22 okt. 2018 om 20:10 schreef Richard M.
Heiberger
> >> >>> <rmh at temple.edu>:
> >> >>>>
> >> >>>> Try the likert function in
> >> >>>> install.packages("HH) ## if necessary
> >> >>>> library(HH)
> >> >>>>
> >> >>>> Then using David Carlson's example teamq
> >> >>>> likert(teamq)
> >> >>>>
> >> >>>> Your example in the 1:30PM (Eastern Daylight
Time) doesn't work.
> >> >>>> Error in revalue(teamq, c(`de situatie in zeer
geringe mate van
> >> >>>> toepassing is\nvoor u of uw supervisorengroep` =
"1",  :
> >> >>>>   x is not a factor or a character vector.
> >> >>>>
> >> >>>> There are many examples in
> >> >>>> ?likert
> >> >>>>
> >> >>>> Rich
> >> >>>>
> >> >>>>
> >> >>>> On Mon, Oct 22, 2018 at 1:30 PM, P. Roberto
Bakker
> >> >>>> <robertobakker at gmail.com> wrote:
> >> >>>> > Dear David,
> >> >>>> >
> >> >>>> > Thank you for you quite response.
> >> >>>> > My apologies for not giving some sample data
- this is due to
> AVG.
> >> >>>> > *But this minisample should not be a problem
(all in Dutch)*:
> >> >>>> >  teamq
> >> >>>> > # A tibble: 4 x 2
> >> >>>> >   `Ik volg bijscholing om mijn
opleiders-kwaliteiten op peil te
> >> >>>> > houden`
> >> >>>> >
> >> >>>> >   <chr>
> >> >>>> >
> >> >>>> > 1 de situatie in hoge mate van toepassing is
voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> > 2 de situatie in zeer hoge mate van
toepassing is voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> > 3 de situatie in zeer hoge mate van
toepassing is voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> > 4 de situatie in geringe mate van toepassing
is voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> >   `Ik weet precies wat de ?modernisering van
de opleiding?
> inhoudt`
> >> >>>> >
> >> >>>> >   <chr>
> >> >>>> >
> >> >>>> > 1 de situatie in hoge mate van toepassing is
voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> > 2 de situatie in zeer hoge mate van
toepassing is voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> > 3 de situatie in zeer hoge mate van
toepassing is voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> > 4 de situatie in geringe mate van toepassing
is voor u of uw
> >> >>>> > supervisorengroep
> >> >>>> >
> >> >>>> > As you see the likert items are in words,
and I should change
> them
> >> >>>> > in
> >> >>>> > nummeric - Am I correct?
> >> >>>> >
> >> >>>> > *To do this, I tried (see further below):*
> >> >>>> > plyr rename() ; I receive the message it
should be a factor or
> >> >>>> > character
> >> >>>> > dplyr recode() ; same message
> >> >>>> > mapvalues() ; it should be atomic, so I used
as.atomic(teamq) but
> >> >>>> > then
> >> >>>> > I
> >> >>>> > receive the nummers a strings.
> >> >>>> >
> >> >>>> > *The syntaxes*
> >> >>>> > require(plyr)
> >> >>>> > example2 <- revalue(teamq,
> >> >>>> >                     c("de situatie in
zeer geringe mate van
> >> >>>> > toepassing
> >> >>>> > is
> >> >>>> > voor u of uw supervisorengroep"=
"1",
> >> >>>> >                         "de situatie in
geringe mate van
> toepassing
> >> >>>> > is
> >> >>>> > voor
> >> >>>> > u of uw supervisorengroep"=
"2",
> >> >>>> >                         "de situatie
enigszins van toepassing is
> >> >>>> > voor
> >> >>>> > u of
> >> >>>> > uw supervisorengroep"= "3",
> >> >>>> >                         "de situatie in
hoge mate van toepassing
> is
> >> >>>> > voor u
> >> >>>> > of uw supervisorengroep"=
"4",
> >> >>>> >                         "de situatie in
zeer hoge mate van
> >> >>>> > toepassing
> >> >>>> > is
> >> >>>> > voor u of uw supervisorengroep"=
"5"))
> >> >>>> >
> >> >>>> > require(dplyr)
> >> >>>> > example2 <- recode(teamq,
> >> >>>> >                     c("de situatie in
zeer geringe mate van
> >> >>>> > toepassing
> >> >>>> > is
> >> >>>> > voor u of uw supervisorengroep"=
"1",
> >> >>>> >                       "de situatie in
geringe mate van toepassing
> >> >>>> > is
> >> >>>> > voor u
> >> >>>> > of uw supervisorengroep"=
"2",
> >> >>>> >                       "de situatie
enigszins van toepassing is
> voor
> >> >>>> > u
> >> >>>> > of uw
> >> >>>> > supervisorengroep"= "3",
> >> >>>> >                       "de situatie in
hoge mate van toepassing is
> >> >>>> > voor
> >> >>>> > u of
> >> >>>> > uw supervisorengroep"= "4",
> >> >>>> >                       "de situatie in
zeer hoge mate van
> toepassing
> >> >>>> > is
> >> >>>> > voor
> >> >>>> > u of uw supervisorengroep"=
"5"))
> >> >>>> >
> >> >>>> > mapvalues(as.matrix(teamq), from =
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"),
> >> >>>> >           to = c(1,2,3,4,5))
> >> >>>> >
> >> >>>> > What should I do?
> >> >>>> > Thank you in advance, Roberto
> >> >>>> >
> >> >>>> > Op ma 22 okt. 2018 om 17:13 schreef David L
Carlson
> >> >>>> > <dcarlson at tamu.edu>:
> >> >>>> >
> >> >>>> >> Your example is not reproducible since
you did not give us some
> >> >>>> >> sample
> >> >>>> >> data. I suspect that your data frame
consists of columns that
> >> >>>> >> represent
> >> >>>> >> questions and rows that represent
individuals who answered the
> >> >>>> >> questions.
> >> >>>> >> First create a simple example:
> >> >>>> >>
> >> >>>> >> set.seed(42)
> >> >>>> >> teamq <- data.frame(V1=sample(c(1, 2,
4, 5), 25, replace > TRUE),
> >> >>>> >>      V2=sample(c(1, 2, 3, 4, 5), 25,
replace=TRUE),
> >> >>>> >>      V3=sample(c(2, 3, 4, 5), 25,
replace=TRUE))
> >> >>>> >>
> >> >>>> >> Notice that this data frame ONLY
contains questions (and only 3
> >> >>>> >> questions). Here are 2 ways to get what
you want. The first one
> >> >>>> >> stacks the
> >> >>>> >> data:
> >> >>>> >>
> >> >>>> >> teamq.stack <- stack(teamq)
> >> >>>> >> str(teamq.stack)
> >> >>>> >> counts <- table(teamq.stack)
> >> >>>> >> str(counts)
> >> >>>> >>
> >> >>>> >> The second one converts each column to a
factor with levels 1 -
> 5:
> >> >>>> >>
> >> >>>> >> teamq2 <- data.frame(lapply(teamq,
factor, levels=1:5))
> >> >>>> >> str(teamq2)
> >> >>>> >> counts <- sapply(teamq2, table)
> >> >>>> >> str(counts)
> >> >>>> >>
> >> >>>> >> Now make the plots:
> >> >>>> >>
> >> >>>> >> cols <-
c("yellow","sandybrown","orange",
> >> >>>> >>
"darkolivegreen","green")
> >> >>>> >> barplot(counts[, 1], horiz=TRUE,
col=cols, legend=TRUE)
> >> >>>> >> barplot(counts[, 2], horiz=TRUE,
col=cols, legend=TRUE)
> >> >>>> >> barplot(counts[, 3], horiz=TRUE,
col=cols, legend=TRUE)
> >> >>>> >>
> >> >>>> >> You will need to adjust the xlim=
argument so that the legend
> does
> >> >>>> >> not
> >> >>>> >> print on top of the bars.
> >> >>>> >>
> >> >>>> >> ----------------------------------------
> >> >>>> >> David L Carlson
> >> >>>> >> Department of Anthropology
> >> >>>> >> Texas A&M University
> >> >>>> >> College Station, TX 77843-4352
> >> >>>> >>
> >> >>>> >>
> >> >>>> >> -----Original Message-----
> >> >>>> >> From: R-help <r-help-bounces at
r-project.org> On Behalf Of P.
> >> >>>> >> Roberto
> >> >>>> >> Bakker
> >> >>>> >> Sent: Monday, October 22, 2018 9:04 AM
> >> >>>> >> To: R mailing list <r-help at
r-project.org>
> >> >>>> >> Subject: [R] Different stack barplots -
same color legends
> >> >>>> >>
> >> >>>> >> Hi,
> >> >>>> >>
> >> >>>> >> I want to make barplots from different
questions (columns) in
> one
> >> >>>> >> data.frame.
> >> >>>> >> Each question has the same 5 likert
items.
> >> >>>> >> Now the problem: in some questions all
items are answered; in
> >> >>>> >> other
> >> >>>> >> less.
> >> >>>> >> From the syntax below I get nice stack
barplots - *but the
> legend
> >> >>>> >> colors do
> >> >>>> >> not* refer to the same likert-item,
which I understand - the
> >> >>>> >> colors
> >> >>>> >> go in
> >> >>>> >> sequence along the table.
> >> >>>> >> Question: how can I write a syntax that
each likert-item has the
> >> >>>> >> same
> >> >>>> >> legend color?
> >> >>>> >> Thank you in advance,
> >> >>>> >>
> >> >>>> >> Roberto
> >> >>>> >>
> >> >>>> >> SYNTAX:
> >> >>>> >> counts19 <- table(teamq[,19])
> >> >>>> >> counts20 <- table(teamq[,20])
> >> >>>> >> barplot(as.matrix(counts19), horiz = T,
> >> >>>> >>
col=c("yellow","sandybrown","orange",
> >> >>>> >>
"darkolivegreen","green"),
> >> >>>> >> legend=T)
> >> >>>> >> barplot(as.matrix(counts20), horiz = T,
> >> >>>> >>
col=c("yellow","sandybrown","orange",
> >> >>>> >>
"darkolivegreen","green"),
> >> >>>> >> legend=T)
> >> >>>> >>
> >> >>>> >>         [[alternative HTML version
deleted]]
> >> >>>> >>
> >> >>>> >>
______________________________________________
> >> >>>> >> 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.
> >> >>>> >>
> >> >>>> >
> >> >>>> >         [[alternative HTML version deleted]]
> >> >>>> >
> >> >>>> >
______________________________________________
> >> >>>> > 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.