Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
First of all, credits for this script must go to Ethan Brown, whose ideas for creating Likert scales like plots with ggplot built the core of my sjPlotLikert.R-script.
All I did was some visual tweaking like having positive percentage values on both sides of the x-axis, adding value labels and so on… You can pass a lot of different parameters to modify the graphical output. Please refer to my blog postings on R to get some impressions of how to teak the plot (and/or look into the script header, which includes a description of all parameters).
Now to some examples:
likert_2 <- data.frame(as.factor(sample(1:2, 500, replace=T, prob=c(0.3,0.7))), as.factor(sample(1:2, 500, replace=T, prob=c(0.6,0.4))), as.factor(sample(1:2, 500, replace=T, prob=c(0.25,0.75))), as.factor(sample(1:2, 500, replace=T, prob=c(0.9,0.1))), as.factor(sample(1:2, 500, replace=T, prob=c(0.35,0.65)))) levels_2 <- list(c("Disagree", "Agree")) items <- list(c("Q1", "Q2", "Q3", "Q4", "Q5")) source("sjPlotLikert.R") sjp.likert(likert_2, legendLabels=levels_2, axisLabels.x=items, orderBy="neg")
What you see above is a scale with two dimensions, ordered from highest “negative” category to lowest. If you leave out the
orderBy
parameter, the plot uses the normal item order:
likert_4 <- data.frame(as.factor(sample(1:4, 500, replace=T, prob=c(0.2,0.3,0.1,0.4))), as.factor(sample(1:4, 500, replace=T, prob=c(0.5,0.25,0.15,0.1))), as.factor(sample(1:4, 500, replace=T, prob=c(0.25,0.1,0.4,0.25))), as.factor(sample(1:4, 500, replace=T, prob=c(0.1,0.4,0.4,0.1))), as.factor(sample(1:4, 500, replace=T, prob=c(0.35,0.25,0.15,0.25)))) levels_4 <- list(c("Strongly disagree", "Disagree", "Agree", "Strongly Agree")) items <- list(c("Q1", "Q2", "Q3", "Q4", "Q5")) source("sjPlotLikert.R") sjp.likert(likert_4, legendLabels=levels_4, axisLabels.x=items)
And finally, a plot with a different color set and items ordered from highest positive answer to lowest.
likert_6 <- data.frame(as.factor(sample(1:6, 500, replace=T, prob=c(0.2,0.1,0.1,0.3,0.2,0.1))), as.factor(sample(1:6, 500, replace=T, prob=c(0.15,0.15,0.3,0.1,0.1,0.2))), as.factor(sample(1:6, 500, replace=T, prob=c(0.2,0.25,0.05,0.2,0.2,0.2))), as.factor(sample(1:6, 500, replace=T, prob=c(0.2,0.1,0.1,0.4,0.1,0.1))), as.factor(sample(1:6, 500, replace=T, prob=c(0.1,0.4,0.1,0.3,0.05,0.15)))) levels_6 <- list(c("Very strongly disagree", "Strongly disagree", "Disagree", "Agree", "Strongly Agree", "Very strongly agree")) items <- list(c("Q1", "Q2", "Q3", "Q4", "Q5")) source("sjPlotLikert.R") sjp.likert(likert_6, legendLabels=levels_6, barColor="brown", axisLabels.x=items, orderBy="pos")
If you need to plot stacked frequencies that have no “negative” and “positive”, but only one direction, you can also use my sjPlotStackFrequencies.R script. Given that you use the likert-data frames from the above examples, you can run following code to plot stacked frequencies for scales that range from “low” to “high” and not from “negative” to “positive”.
levels_42 <- list(c("Independent", "Slightly dependent", "Dependent", "Severely dependent")) levels_62 <- list(c("Independent", "Slightly dependent", "Dependent", "Very dependent", "Severely dependent", "Very severely dependent")) source("lib/sjPlotStackFrequencies.R") sjp.stackfrq(likert_4, legendLabels=levels_42, axisLabels.x=items) sjp.stackfrq(likert_6, legendLabels=levels_62, axisLabels.x=items)
This produces following two plots:
That’s it!
Tagged: ggplot, Likert-Scale, R, rstats
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.