0

Consider the following minimal example:

tab = structure(c(187, 189, 205, 167, 273, 275, 295, 231, 385, 575, 
409, 363, 899, 707, 751, 667, 2007, 1953, 2101, 2531, 1043, 1023, 
1071, 971, 385, 575, 409, 363, 181, 241, 157, 163, 123, 163, 
133, 131), .Dim = c(4L, 9L), .Dimnames = list(NULL, c("-4", "-3", 
"-2", "-1", "0", "1", "2", "3", "4")))

The matrix tab contains counts of a 9-Point Likert Scale.

A basic plot is easy to produce:

    likert(tab,
           auto.key=list(between=0.5, between.columns=1),
           main="Attitudes",
           BrewerPaletteName="RdBu",as.percent=TRUE, rightAxisLabels=NULL, 
           rightAxis=NULL, ylab.right="")

And it looks nice. Now, the problems:

  • I would like to control the increments of the percentage axis. In some cases, with other counts, it only shows 50%. I want to enforce 10% increments
  • I want to add the % count as a label. Most importantly for the 0-category. Like in Excel, where you right click and say "add data labels". There must be something similar.

I also considered ggplot, however, it does not support the diverging plots. Plus labeling is also quite messy, especially for tikz export.

EDIT I have tried using panel.text. However, I still don't get the numbers alined correctly.

#does not work right!
trellis.focus("toplevel")
for(pp in 1:nrow(tab)){
panel.text(0.5, (pp/nrow(tab))*0.84+0.16, tab[pp,5], cex = 1, font = 2)
}
trellis.unfocus()

How can I find the right positions within each bar?

Inferrator
  • 519
  • 1
  • 5
  • 13

1 Answers1

2

To change x axis values you should add argument scales= and add at= argument for the x axis.

likert(tab,
       auto.key=list(between=0.5, between.columns=1),
       main="Attitudes",
       BrewerPaletteName="RdBu",as.percent=TRUE, rightAxisLabels=NULL, 
       rightAxis=NULL, ylab.right="",
       scales=list(
         x=list(
           at=seq(-50,50,10))))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • Wonderful! Task one completed, it works like a charm. Any idea on the value labels? – Inferrator Mar 06 '13 at 20:17
  • @user1216731 do you want something like this fir labels , `scales=list( x=list( labels = paste(seq(-50,50,10),'%',sep=''), at=seq(-50,50,10))))`? – agstudy Mar 06 '13 at 21:03
  • @agstudy great, now even with % sign. Thanks! What I still miss is to label the bars, i.e. put 42% on the 0-share of the first bar. – Inferrator Mar 06 '13 at 22:46
  • Essentially it is some form of a trellis/lattice barplot. There must be a straightforward way to put the percentages into each of the chunks of each bar. – Inferrator Mar 07 '13 at 03:16
  • There appears to be solution for labels, so I picked this answer as the solution for the solvable issue #1. – Inferrator Mar 08 '13 at 22:01