[R] Different stack barplots - same color legends

P. Roberto Bakker robertob@kker @end|ng |rom gm@||@com
Sun Nov 4 11:32:14 CET 2018


Dear Richard,

I ran your syntax and the plots look great and cool.
Again, thank you so much for helping me out a lot and for all the
programming.
And again, Jim en David, thank you also so much.
Now, I can continue with my work and I learned a lot from you.

Best,
Roberto

Op za 3 nov. 2018 om 14:06 schreef P. Roberto Bakker <
robertobakker using gmail.com>:

> Dear Richard,
>
> Thank you so much for all your work and time you punt in it.
> I will start with your suggestions and let you know how far I come.
> Also thanks to the others who helpt me.
>
> Best Roberto
>
> Op do 1 nov. 2018 om 23:42 schreef Richard M. Heiberger <rmh using temple.edu>:
>
>> ## 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 using gmail.com> wrote:
>> > 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 using 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 using 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 using 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 using 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 using temple.edu>:
>> >> >> >>
>> >> >> >> Please send me the
>> >> >> >> dput(teamq)
>> >> >> >>
>> >> >> >>
>> >> >> >> On Wed, Oct 31, 2018 at 03:51 P. Roberto Bakker
>> >> >> >> <robertobakker using 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 using 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 using 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 using 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 using r-project.org> On Behalf Of P.
>> >> >> >>>> >> Roberto
>> >> >> >>>> >> Bakker
>> >> >> >>>> >> Sent: Monday, October 22, 2018 9:04 AM
>> >> >> >>>> >> To: R mailing list <r-help using 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 using 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 using 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]]




More information about the R-help mailing list