## 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:
Hi Rich,
Thank you for your answer.
The sentences are strings (likert scale: 'the situation is highly
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
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
"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
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:
Error in dimnames(x) <- `*vtmp*` :
length of 'dimnames' [2] not equal to array extent
I checked:
[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
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
voor
u of
uw supervisorengroep"= "3",
"de situatie in hoge mate van
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
voor
u of
uw supervisorengroep",
"de situatie in hoge mate van
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
sample
data. I suspect that your data frame consists of columns that
represent
questions and rows that represent individuals who answered
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
questions). Here are 2 ways to get what you want. The first
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
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]]