0

I have been trying to use sjp.xtab to do some proportional stacked bar plots (with flipped coordinates) but I have had some trouble.

I get the value labels in weird places (instead of in the middle of the stacked bars), and I would also like to know how to change the order of the columns, so that I can get some label in the right side, for example.

Here is an example I created to show what is going on:

I create a variable called question, which just simulates a questionnaire item, with three response options, b (low), m (medium), a (high)

question <- c("b", "b", "b", "m", "a", "a", "a", "a","a","a", "a", "m","m","a")
question <- factor(question, levels=c("b","m","a"))

question <- c("b", "b", "b", "m", "a", "a", "a", "a","a","a", "a", "m","m","a")
question <- factor(question, levels=c("b","m","a"))

Then a variable called rowvar which is the group variable, with three possible groups: mod1, mod2, mod3

rowvar <- c("mod1", "mod1", "mod1","mod1","mod1","mod2","mod2","mod2","mod2","mod2","mod2", "mod3","mod3","mod3")
rowvar <-factor(rowvar, levels=c("mod1","mod2","mod3"))

There's the cross tabs, those are the values that I would expect in the plot

prop.table(table(rowvar,question),1)

But when I use sjp.xtab, I get the value labels in weird positions...

library(sjPlot)
sjp.xtab(rowvar,question,margin="row",bar.pos="stack",coord.flip=TRUE)

The resulting plot is this one:

Resulting plot from example

I updated everything to the latest version (R, sjPlot, ggplot2...).

Anyone knows how to fix this without too much programming..? I am not a programmer, I am merely a social psychologist trying to build some ok looking plots. Sorry if this post is inadequate in any way.

  • I had some troubles with label positioning and the latest ggplot-update (2.2.0), so I guess this might be an issue I have overseen when updating my package to work with ggplot 2.2.0... I have opened an issue (https://github.com/sjPlot/devel/issues/163) and hopefully can fix this within the next days... – Daniel Nov 26 '16 at 18:17
  • See [ggplot2-news](https://github.com/tidyverse/ggplot2/blob/master/NEWS.md): order of stacked bars was reversed. The fix should be simply adding a `position = position_stack(reverse = TRUE)`, guess I can commit that fix tomorrow. – Daniel Nov 26 '16 at 18:19
  • That'll be great, thanks... Also, congrats on sjplot, it is a great package which made my life much easier... I was looking for something like that. – Joao Wachelke Dec 04 '16 at 17:51
  • Bug already fixed, if you like, you can download the GitHub-version of _sjPlot_ with _devtools_-package. See https://github.com/sjPlot/devel – Daniel Dec 04 '16 at 23:40
  • Hi @Daniel, I tried it, I could do it for sjmisc, but when I tried it for sjPlot I got: _Error in parse(outFile) : C:/Users/João/AppData/Local/Temp/Rtmpq6OOhn/devtools140c6ac0691b/sjPlot-devel-f8ede18/R/helpfunctions.R:616:42: unexpected symbol 615: # any valid model? 616: if (inherits(fit, c("plm", "ppgls")) return ^ ERROR: unable to collate and parse R files for package 'sjPlot'_ Anything wrong with my system...? – Joao Wachelke Dec 07 '16 at 00:37
  • I forgot a bracket. When I realized this bug, I committed another fix to GitHub. I guess this commit came shortly after you tried installing from GitHub, so pleas retry again now - it should work now. – Daniel Dec 07 '16 at 14:31
  • Yes it works fine! Thanks a lot @Daniel! – Joao Wachelke Dec 07 '16 at 17:09

0 Answers0