Search code examples
rlikert

Create likert plot with two groups in R


I have issues trying to create a likert plot with two groups. I realized a survey in two communities. I now want to compare these two communities. [That's my data frame][1]. So far, I have loaded a sheet with 3 columns. One column refers to the location (community1, community2), one includes the answers regarding econ_comm (1-6) and one includes the answers regarding future_persp (1-6). I created a likert-object and a first figure.

g_likert = likert(g[1:6])
plot(g_likert, ordered = FALSE, group.order = names(g[2:3]))

...and it worked. Following is what I got so far. [![enter image description here][2]][2]

I assume it's now important to create a both-object first: both<-g$Location (worked)

Now I'm starting to get trouble. The following code shows me errors:

both_likert_2 = likert(both[, c(1:3), drop=FALSE], grouping = both$location)
plot(both_likert_2, include.histogram = TRUE)

The errors are:

Error in [.data.frame (g, 1:6) : undefined columns selected

Error in [.default (both, , c(1:3), drop = FALSE) : wrong number of dimensions - Objekt 'both_likert_2' not found

[I have now also attached a screenshot of my R, just to make sure.][4] I'm struggling for quite some time now and I would be very very grateful for some help. Best, Felix

EDIT: [![This is my current situation in R][5]][5] Here's my code to reproduce it:

library(likert)
g<-read.csv2("C:/Users/felix/OneDrive/Documents/R/SurveyData2.csv", sep=";", dec=",", header=TRUE)
both<-g$Location
g <-  within(g, {
  gold_21cent <- factor(gold_21cent, levels=1:6, labels=c("Completely agree", "Agree", "Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"))
  future_persp <- factor(future_persp, levels=1:6, labels=c("Completely agree", "Agree", "Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"))
 jobs_comm <- factor(jobs_comm, levels=1:6, labels=c("Completely agree", "Agree", "Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"))
} )
.........etc............
comm_likert = likert(g[,2:14], grouping=g[,1])
plot(comm_likert)
library(dplyr)
g %>%

rename(It offers important economic perspectives=future_persp, It provides economic prosperity to the community=econ_comm) %>% likert(grouping=Location) %>% plot()

EDIT: Using dput(g)

structure(list(Location = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Huan", "TP"), class = "factor"), 
gold_21cent = structure(c(6L, 6L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 6L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, NA, NA, 1L, 1L,
6L, 4L, 6L, 5L, 2L, 2L, 2L, 4L, 4L, 3L, 3L, 2L, 3L, 2L, 3L, 
3L, NA, 2L, 3L, 2L, NA, 2L, 2L, 3L, 5L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 2L, 3L, 6L), .Label = c("Completely agree", "Agree", 
"Slightly agree", "Slightly disagree", "Disagree", "Completely disagree"
), class = "factor"), life_quality = structure(c(3L, 3L, 
6L, 6L, 5L, 5L, 6L, 5L, 6L, 3L, 3L, 6L, 4L, 6L, 6L, 6L, 6L, 
6L, 6L, NA, NA, 3L, 5L, 6L, 2L, 6L, 5L, 3L, 2L, 3L, 1L, 1L, 
2L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 2L, NA, 
3L, 2L, 2L, 3L, 3L, 3L, 5L, 5L, 3L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), coexist_tradact = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 5L, 5L, 6L, 2L, 6L, 1L, 6L, 5L, 6L, 6L, 6L, 1L, 
4L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 4L, 3L, 2L, 3L, 3L, 3L, 
4L, 4L, 3L, 2L, 4L, 5L, 4L, 2L, 4L, 6L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), emigration_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 5L, NA, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, NA, NA, 4L, 2L, 1L, 3L, 1L, 1L, 6L, 6L, 6L, 2L, 
1L, 3L, 3L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 2L, 3L, 3L, 2L, 1L, 
4L, 5L, 5L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), future_persp = structure(c(3L, 
3L, 5L, 5L, 5L, 5L, 6L, 5L, 5L, 3L, 5L, 6L, 6L, 5L, 3L, 6L, 
5L, 5L, 3L, 3L, 3L, 2L, 1L, 6L, 3L, 6L, 5L, 3L, 3L, 3L, 1L, 
3L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 2L, 4L, 2L, 2L, 3L, 1L, 2L, 
4L, 4L, 4L, 2L, 4L, 2L, 3L, 5L, 5L, 3L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), workers_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 2L, 6L, 6L, 
6L, 6L, 6L, 4L, 5L, NA, 1L, 6L, 1L, 6L, 5L, 6L, 6L, 6L, 1L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 1L, 4L, 3L, 4L, 5L, 3L, 3L, 4L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), work_project = structure(c(6L, 
6L, 6L, 6L, 5L, 5L, 6L, 5L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 5L, 3L, 6L, 4L, 6L, 5L, 6L, 6L, 6L, 6L, 
3L, 2L, 2L, 1L, 1L, 3L, 6L, 3L, 3L, 2L, 6L, 3L, 3L, 3L, 1L, 
3L, 6L, 5L, 6L, 5L, 1L, 6L, 6L, 5L, 6L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), agree_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 6L, 5L, 6L, 
6L, 6L, 6L, 5L, 5L, 5L, 3L, 6L, 2L, 5L, 6L, 6L, 6L, 6L, 3L, 
3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 4L, 1L, 3L, NA, 3L, 2L, 
3L, 3L, NA, 3L, 3L, 4L, 3L, 3L, 3L, 4L, 2L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), informed_deals = structure(c(4L, 
4L, 1L, 1L, 5L, 6L, 5L, 5L, 5L, 6L, 4L, 5L, 5L, 6L, 4L, 6L, 
5L, 6L, 4L, 6L, 6L, 6L, 1L, 6L, 3L, 6L, 1L, 2L, 2L, 2L, 4L, 
2L, 3L, 3L, 2L, 1L, 5L, 6L, 5L, 2L, 3L, 5L, 3L, 3L, 4L, 4L, 
2L, 4L, 4L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), fear_environ = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 5L, 1L, 
1L, 1L, 1L, 6L, 6L, 1L, 2L, 1L, 4L, 1L, NA, 1L, 1L, 1L, 4L, 
4L, 3L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 1L, 
NA, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), water_quant = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 2L, 1L, 
1L, 1L, 6L, 6L, 6L, 5L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
3L, 2L, 1L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, NA, 2L, 1L, 
5L, 1L, 1L, 2L, 4L, 1L, 3L, 1L, 2L, 3L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), support_govern = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 5L, 6L, 6L, 4L, 5L, 6L, 1L, 5L, 6L, 3L, 3L, 3L, 3L, 
3L, 2L, 3L, 3L, 2L, 5L, 5L, 3L, 2L, 4L, 3L, 3L, NA, 1L, 1L, 
2L, 6L, 3L, 3L, 3L, 4L, 4L, 6L, 4L, 4L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), proud_comm = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 
3L, 1L, 2L, 1L, 1L, 2L, 5L, 2L, 1L, 2L, 2L, 2L, NA, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L, 1L, 3L, 2L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), econ_comm = structure(c(6L, 
6L, 6L, 6L, 5L, 6L, 5L, 5L, 6L, 6L, 3L, 6L, 6L, 5L, 3L, 5L, 
3L, 4L, 5L, 2L, 2L, 5L, 5L, 6L, 2L, 6L, 5L, 5L, 5L, 5L, 3L, 
2L, 3L, 3L, 1L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 1L, 
3L, 2L, 3L, 1L, 5L, 3L, 2L, 5L, 2L, 6L, 3L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), jobs_comm = structure(c(6L, 
6L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 4L, 6L, 4L, 
6L, 4L, 5L, 5L, 5L, 3L, 2L, 6L, 1L, 6L, 5L, 5L, 5L, 5L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L, 1L, 5L, 2L, 3L, 4L, 2L, 2L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), inequality_comm = structure(c(1L, 
1L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 1L, 3L, 6L, 1L, 2L, 6L, 2L, 
6L, 2L, 6L, NA, NA, 5L, 1L, 6L, 3L, 6L, 5L, 5L, 5L, 5L, 1L, 
3L, 3L, 2L, 3L, 4L, 2L, 3L, 4L, 3L, 2L, 4L, 3L, 3L, 5L, 3L, 
4L, 5L, 3L, 1L, 4L, 3L, 4L, 2L, 3L, NA, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), exp_growth = structure(c(6L, 
6L, 4L, 4L, 6L, 6L, 6L, 5L, 6L, 6L, 4L, 4L, 6L, 2L, 2L, 2L, 
5L, 2L, 3L, NA, NA, 3L, 3L, 6L, 1L, 6L, 5L, 3L, 2L, 6L, 4L, 
2L, 2L, 3L, 2L, 3L, 5L, 2L, 3L, 2L, 3L, 4L, 2L, 3L, 2L, 1L, 
2L, 5L, 3L, 4L, 2L, 4L, 2L, 6L, 3L, 3L, 3L, 3L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), future_region = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
5L, 6L, 5L, 2L, 2L, 2L, 2L, 6L, 3L, 6L, 5L, 6L, 6L, 3L, 4L, 
2L, 3L, 3L, 2L, 4L, 5L, 5L, 5L, 2L, 4L, 2L, 4L, 3L, 4L, 1L, 
NA, 6L, 3L, 3L, 3L, 4L, 3L, 4L, 5L, 5L, 2L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), contamination_environ = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 6L, 1L, 
6L, 1L, 1L, 6L, 6L, 6L, 4L, 1L, 4L, 6L, 1L, 1L, 1L, 1L, 4L, 
1L, 2L, 1L, 3L, 3L, 1L, 2L, 1L, 3L, 3L, 5L, 2L, 2L, 1L, 1L, 
NA, 1L, 1L, 1L, 1L, 5L, 2L, 1L, 3L, 2L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), conflicts_comm = structure(c(6L, 
6L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
2L, 1L, 6L, 5L, 5L, 5L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 
3L, 3L, 3L, 3L, 1L, 3L, 2L, 4L, 3L, 3L, 4L, 3L, NA, 4L, 2L, 
3L, 2L, 3L, 3L, 5L, 4L, 4L, 1L, 3L, 5L, 3L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), infrastructure_comm = structure(c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 5L, 6L, 6L, 4L, 6L, 5L, 6L, 5L, 6L, 
5L, 6L, 3L, 3L, 3L, NA, 1L, 6L, 1L, 6L, 6L, 4L, 4L, 3L, 1L, 
4L, 3L, NA, 3L, 1L, 5L, 6L, 2L, 3L, 3L, 4L, 3L, 2L, 1L, 2L, 
2L, 4L, 2L, 3L, 6L, 4L, 3L, 3L, 3L, 3L, 2L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), included_mining = structure(c(1L, 
1L, 1L, 1L, 5L, 6L, 6L, 6L, 6L, 6L, 3L, 6L, 6L, 6L, 5L, 6L, 
6L, 6L, 3L, 5L, 5L, 6L, 3L, NA, 3L, NA, NA, 6L, 5L, 5L, 1L, 
3L, 3L, 3L, 3L, 5L, 4L, 6L, 3L, 3L, 2L, 6L, 2L, NA, 2L, 3L, 
2L, 5L, 5L, 3L, 5L, 3L, 3L, 2L, 4L, 5L, 3L, NA), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), investment_region = structure(c(1L, 
1L, 3L, 3L, 6L, 6L, 6L, 5L, 6L, 1L, 2L, 1L, 6L, 2L, 4L, 1L, 
2L, 2L, 3L, NA, NA, 4L, 1L, 5L, 3L, 5L, 5L, 1L, 2L, 2L, 4L, 
2L, 2L, 2L, 4L, 1L, 2L, 3L, 3L, 2L, 3L, 1L, 3L, NA, 3L, 2L, 
NA, 2L, 3L, 3L, 4L, 4L, 3L, 3L, 2L, 2L, 2L, 4L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor"), water_qual = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 6L, 5L, 5L, 5L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
4L, 3L, 1L, 2L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, NA, 3L, 1L, 
4L, 1L, 1L, 3L, 4L, 2L, 2L, 1L, 2L, 3L, 3L, 2L), .Label = c("Completely agree", 
"Agree", "Slightly agree", "Slightly disagree", "Disagree", 
"Completely disagree"), class = "factor")), row.names = c(NA, 

-58L), class = "data.frame")


Solution

  • I think you just made a simple typing error. And you don't really need to create the both vector, as you can pass the grouping variable within the function call. Try:

    both_likert_2 = likert(g[, c(1:3), drop=FALSE], grouping = g$Location)
    

    Edit: Based on the new information provided, how about this:

    g <-  within(g, {
      future_persp <- factor(future_persp, levels=1:6)
      econ_comm <- factor(econ_comm, levels=1:6)
    } )
    
    comm_likert = likert(g[,2:3], grouping=g[,1])
    plot(comm_likert)
    

    Edit: OP wanted to change y-axis tick labels to something more appropriate. Change the names of the columns, but use dplyr so that you don't have to make permanent changes:

    library(dplyr)
    g %>%
       dplyr::select(future_persp, econ_comm) %>%
       rename(`It offers important economic perspectives`=future_persp,
              `It provides economic prosperity to the community`=econ_comm) %>%
       likert(grouping=g[, "Location"]) %>%
       plot()