Search code examples
rbar-chartlikert

R: creating a likert scale barplot


I'm new to R and feeling a bit lost ... I'm working on a dataset which contains 7 point-likert-scale answers.

My data looks like this for example:

enter image description here

My goal is to create a barplot which displays the likert scale on the x-lab and frequency on y-lab.

What I understood so far is that I first have to transform my data into a frequency table. For this I used a code that I found in another post on this site:

    data <- factor(data, levels = c(1:7))
    table(data)

However I always get this output:

    data
    1 2 3 4 5 6 7 
    0 0 0 0 0 0 0 

Any ideas what went wrong or other ideas how I could realize my plan?

Thanks a lot! Lorena


Solution

  • This is a very simple way of handling your question, only using base-R

    ## your data
    my_obs <- c(4,5,3,4,5,5,3,3,3,6)
    
    ## use a factor for class data
    ## you could consider making it ordered (ordinal data)
    ## which makes sense for Likert data
    ## type "?factor" in the console to see the documentation
    my_factor <- factor(my_obs, levels = 1:7)
    
    ## calculate the frequencies
    my_table <- table(my_factor)
    
    ## print my_table
    my_table
     # my_factor
     # 1 2 3 4 5 6 7 
     # 0 0 4 2 3 1 0 
    
    ## plot
    barplot(my_table)
    

    yielding the following simple barplot:

    enter image description here

    Please, let me know whether this is what you want