Skip to content

Commit

Permalink
Updated likert code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbryer committed Apr 27, 2012
1 parent ec23655 commit d082765
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 59 deletions.
21 changes: 10 additions & 11 deletions R/likert.R
Expand Up @@ -132,27 +132,26 @@ summary.likert <- function(x, ...) {
#' @param neutral.color color for middle values. Only used when there are an odd
#' number of levels.
#' @param text.size size or text labels
#' @param text.color the color of text in heat map cells
#' @param type whether to plot a bar or heat map graphic
#' @export
#' @method plot likert
#' @S3method plot likert
plot.likert <- function(likert, low.color='blue', high.color='red',
neutral.color='white', text.colour='white', text.size=2,
type=c('bar','heat'), ...)
{
plot.likert <- function(likert, text.size=2, type=c('bar','heat'), ...) {
if(type[1] == 'bar') {
plot.likert.bar(likert, low.color=low.color, high.color=high.color,
neutral.color=neutral.color, text.size=text.size, ...)
plot.likert.bar(likert,
#low.color=low.color, high.color=high.color, neutral.color=neutral.color,
text.size=text.size, ...)
} else {
plot.likert.heat(likert, low.color=low.color, high.color=high.color,
plot.likert.heat(likert, #low.color=low.color, high.color=high.color,
text.size=text.size, ...)
}
}

#' Internal method.
#' @seealso plot.likert
plot.likert.bar <- function(likert, low.color='blue', high.color='red',
neutral.color='white', text.size=2, ...)
neutral.color='white', text.size=2, text.color='black', ...)
{
lowrange = 1 : ceiling(likert$nlevels / 2 - likert$nlevels %% 2)
highrange = ceiling(likert$nlevels / 2 + 1 ) : likert$nlevels
Expand Down Expand Up @@ -206,8 +205,8 @@ plot.likert.bar <- function(likert, low.color='blue', high.color='red',

#' Internal method.
#' @seealso plot.likert
plot.likert.heat <- function(likert, low.color='blue', high.color='red',
neutral.color='white', text.colour='white', text.size=2, ...) {
plot.likert.heat <- function(likert, low.color='white', high.color='blue',
neutral.color='white', text.color='black', text.size=2, ...) {
if(!is.null(likert$grouping)) {
stop('likert plots with grouping are not supported.')
}
Expand All @@ -221,7 +220,7 @@ plot.likert.heat <- function(likert, low.color='blue', high.color='red',
label=paste(format(mean, digits=3, drop0trailing=FALSE),
' (', format(sd, digits=2, drop0trailing=FALSE), ')', sep='')),
size=text.size) +
geom_tile() + geom_text(size=text.size, colour=text.colour) + coord_flip() +
geom_tile() + geom_text(size=text.size, colour=text.color) + coord_flip() +
scale_fill_gradient("Percent", low=low.color, high=high.color, limits=c(0,100)) +
xlab('') + ylab('') +
opts(panel.grid.major=theme_blank(),
Expand Down
33 changes: 18 additions & 15 deletions irutils.test.R
Expand Up @@ -3,12 +3,10 @@ library(ggplot2)

theme_update(panel.background=theme_blank(), panel.grid.major=theme_blank(), panel.border=theme_blank())


#Testing the plotting functions
#How much do you agree or disagree with these statements about reading?
data(pisa)
data(pisana)

items28 = pisa[,substr(names(pisa), 1,5) == 'ST24Q']
items28 = pisana[,substr(names(pisana), 1,5) == 'ST24Q']
head(items28); ncol(items28)
names(items28) = c("I read only if I have to.",
"Reading is one of my favorite hobbies.",
Expand All @@ -27,19 +25,17 @@ for(i in 1:ncol(items28)) {
}

l28 = likert(items28)
print(l28)
summary(l28)

plot(l28)
plot(l28, type='heat')

l28g = likert(item28, grouping = pisa$CNT)

plotBarchartTable(items28, low.color='maroon', high.color='burlywood4')
plotBarchartTable(items28, grouping=pisa$CNT, low.color='maroon', high.color='burlywood4')
plotHeatmapTable(items28)

#Group by country
l28g = likert(items28, grouping = pisana$CNT)
plot(l28g, low.color='maroon', high.color='burlywood4')

#How often do you read these materials because you want to?
items29 = pisa[,substr(names(pisa), 1,5) == 'ST25Q']
items29 = pisana[,substr(names(pisana), 1,5) == 'ST25Q']
head(items29); ncol(items29)
names(items29) = c("Magazines", "Comic books", "Fiction", "Non-fiction books", "Newspapers")
for(i in 1:ncol(items29)) {
Expand All @@ -48,8 +44,15 @@ for(i in 1:ncol(items29)) {
'Several times a month', 'Several times a week'), ordered=TRUE)
}

plotBarchartTable(items29, low.color='maroon', high.color='burlywood4') +
l29 = likert(items29)
summary(l29)

plot(l29, low.color='maroon', high.color='burlywood4') +
opts(title="How often do you read these materials because you want to?")
plotBarchartTable(items29, grouping=pisa$CNT, low.color='maroon', high.color='burlywood4') +
plot(l29, type='heat') + opts(title="How often do you read these materials because you want to?")

l29g = likert(items29, grouping=pisana$CNT)
summary(l29g)

plot(l29g, low.color='maroon', high.color='burlywood4') +
opts(title="How often do you read these materials because you want to?")
plotHeatmapTable(items29) + opts(title="How often do you read these materials because you want to?")
6 changes: 3 additions & 3 deletions man/plot.likert.Rd
Expand Up @@ -2,9 +2,7 @@
\alias{plot.likert}
\title{Plots a set of likert items.}
\usage{
\method{plot}{likert} (likert, low.color = "blue",
high.color = "red", neutral.color = "white",
text.colour = "white", text.size = 2,
\method{plot}{likert} (likert, text.size = 2,
type = c("bar", "heat"), ...)
}
\arguments{
Expand All @@ -21,6 +19,8 @@

\item{text.size}{size or text labels}

\item{text.color}{the color of text in heat map cells}

\item{type}{whether to plot a bar or heat map graphic}
}
\description{
Expand Down
30 changes: 15 additions & 15 deletions man/plot.likert.bar.Rd
@@ -1,15 +1,15 @@
\name{plot.likert.bar}
\alias{plot.likert.bar}
\title{Internal method.}
\usage{
plot.likert.bar(likert, low.color = "blue",
high.color = "red", neutral.color = "white",
text.size = 2, ...)
}
\description{
Internal method.
}
\seealso{
plot.likert
}

\name{plot.likert.bar}
\alias{plot.likert.bar}
\title{Internal method.}
\usage{
plot.likert.bar(likert, low.color = "blue",
high.color = "red", neutral.color = "white",
text.size = 2, text.color = "black", ...)
}
\description{
Internal method.
}
\seealso{
plot.likert
}

30 changes: 15 additions & 15 deletions man/plot.likert.heat.Rd
@@ -1,15 +1,15 @@
\name{plot.likert.heat}
\alias{plot.likert.heat}
\title{Internal method.}
\usage{
plot.likert.heat(likert, low.color = "blue",
high.color = "red", neutral.color = "white",
text.colour = "white", text.size = 2, ...)
}
\description{
Internal method.
}
\seealso{
plot.likert
}

\name{plot.likert.heat}
\alias{plot.likert.heat}
\title{Internal method.}
\usage{
plot.likert.heat(likert, low.color = "white",
high.color = "blue", neutral.color = "white",
text.color = "black", text.size = 2, ...)
}
\description{
Internal method.
}
\seealso{
plot.likert
}

0 comments on commit d082765

Please sign in to comment.