[R] Different stack barplots - same color legends

P. Roberto Bakker robertob@kker @end|ng |rom gm@||@com
Mon Oct 22 19:30:24 CEST 2018


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]]




More information about the R-help mailing list