1
votes

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

2
Please, provide a representative sample of your data with dput. People will be more inclined to help you, if they have the data easily available (not in a picture).KoenV

2 Answers

1
votes

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

1
votes

Lorena!

First, there's no need to apply factor() neither table() in the dataset you showed. From what I gather, it looks fine.

R comes with some interesting plotting options, hist() is one of them.

Histogram with hist()

In the following example, I'll use the "Valenz" variable, as named in your dataset.

To get the frequency without needing to beautify it, you can simply ask:

hist(dataset, Valenz)

The first argument (dataset) informs where these values are; the second argument (Valenz) informs which values from dataset you want to use.

If you only want to know the frequency, without having to inform it in some elegant way, that oughta do it (:

Histogram with ggplot()

If you want to make it prettier, you can style your plot with the ggplot2 package, one of the most used packages in R.

First, install and then load the package.

install.packages("ggplot2")
library(ggplot2)

Then, create a histogram with x as the number of times some score occurred.

ggplot(dataset, aes(x = Valenz)) + 
geom_histogram(bins = 7, color = "Black", fill = "White") + 
labs(title = NULL, x = "Name of my variable", y = "Count of 'Variable'") +
theme_minimal()
  • ggplot() takes the value of your dataframe, then aes() specifies you want Valenz to be in the x-axis.
  • geom_histogram() gives you a histogram with "bins = 7" (7 options, since it's a likert scale), and the bars with "color = 'Black'" and "fill = 'White'".
  • labs() specifies the labels that appear beneath x ("x = "Name of my variable") and then by y (y = "Count of 'Variable'").
  • theme_minimal() makes the plot look cooler.

I hope I helped you in some way, Lorena. (: