0

I have been really struggling to create a bar chart in R, in which I could present responses to questionnaire questions that use a five point Likert scale. Considering that 1-Not familiar; 2- Somewhat familiar; 3-Neutral; 4-Familiar; 5-Very familiar. I have created chart in Excel, which looks like this:

Likert scale questionnaire responses

I am stuck in R with the table below:

 [,1] [,2] [,3] [,4] [,5]
row1    3   10    1    8    7
row2    3    6    3    4   13
row3    3    6    3    4   13
row4    2    5    1   10   11

Can anyone help me to create similar table in R? Please.

M_Gal
  • 1
  • 1
  • 1
    [,1] [,2] [,3] [,4] [,5] row1 3 10 1 8 7 row2 3 6 3 4 13 row3 3 6 3 4 13 row4 2 5 1 10 11 –  Jan 12 '15 at 23:45
  • See the examples under `?barplot`, specifically the 8 lines of code starting about 9 lines down (at the line `mp <- barplot(VADeaths) # default`, and ending with `title(main = "Death Rates in Virginia", font.main = 4)`) ... If you copypaste those 8 or so lines, you'll get something very like the plot you're trying to generate (but on built in data). Note that `VADeaths` is a matrix with named rows and columns. – Glen_b Jan 13 '15 at 02:03
  • I find this barplot rather hard to read and would prefer the centered stacked barplots that the `likert` package produces. It's on CRAN and comes with nice and easy-to-use examples. – Achim Zeileis Jan 13 '15 at 07:43

2 Answers2

1

You could try something like this:

    row1<-c(3,10,1,8,7)
    row2<-c(3,6,3,4,13)
    row3<-c(3,6,3,4,13)
    row4<-c(2,5,1,10,11)

X<-rbind(row1, row2, row3, row4)
    barplot(X, plot=TRUE, beside=TRUE, col=1:5, legend=c("Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree"))
StatsStudent
  • 1,384
  • 2
  • 10
  • 28
  • Thank you, I have tried this and that's what Im getting: –  Jan 13 '15 at 02:44
  • Error in barplot(X, plot = TRUE, beside = TRUE, col = 1:5, legend = c("Strongly Agree", : object 'X' not found –  Jan 13 '15 at 02:45
  • Sorry. I left out the rbind statement. I've added it now. You'll want to play with the axis labels and legend placement a bit, but that's the basic code. –  Jan 13 '15 at 02:46
1

If you prefer a ggplot2-based solution, as an alternative to suggested base R graphics solution, I think that it should be along the following lines. A minimal reproducible example (MRE), based on your data follows.

if (!suppressMessages(require(ggplot2))) install.packages('ggplot2')
if (!suppressMessages(require(reshape))) install.packages('reshape')
library(ggplot2)
library(reshape)

myData <- data.frame('Gov. agencies' = c(3, 10, 1, 8, 7), 'Local authority' = c(3, 6, 3, 4, 13), 'Police forces' = c(3, 6, 3, 4, 13), 'NGO/third sector' = c(2, 5, 1, 10, 11), response = c('Not familiar', 'Somewhat familiar', 'Neutral', 'Familiar', 'Very familiar'))

levels(myData$response) <- c('Not familiar', 'Somewhat familiar', 'Neutral', 'Familiar', 'Very familiar')

myDataMelted <- melt(myData, id.vars = 'response')

ggplot(myDataMelted, aes(x=response, y=value, fill = variable))+
    geom_bar(stat = "identity", position = "dodge", color = "black")

The result:

enter image description here

WARNING! Please note that the above code is posted as a proof-of-concept and it is not only not complete in terms of labeling/beautification, but it contains an error (I think, not a major one), which I hope more knowledgeable people here will help me to fix, so that you could have an alternative solution (and I could have some educational experience and peace of mind, after all the trouble :-). The error is that groups are not in the correct order / do not belong to the correct categories. I've tried to alleviate that problem via levels(), but probably still missed or forgot some other point.

Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64
  • Thank you, Im getting two errors: myDataMelted <- melt(myData, id.vars = 'response') Error: could not find function "melt" > > ggplot(myDataMelted, aes(x=response, y=value, fill = variable))+ + geom_bar(stat = "identity", position = "dodge", color = "black") Error: could not find function "ggplot" –  Jan 13 '15 at 03:06
  • @M_Gal: No problem. You're welcome. Let's hope that people will help in fixing the above-mentioned issue. – Aleksandr Blekh Jan 13 '15 at 03:21
  • Hi, do you know if its possible to change the scale into more accurate? – M_Gal Jan 16 '15 at 16:31
  • @M_Gal: What do you mean: more _tick marks_ or _zoom into_ a plot area? In any case, [this discussion](http://stackoverflow.com/q/11335836/2872891) contains excellent answers on both questions. Hope this is helpful. – Aleksandr Blekh Jan 16 '15 at 16:50