Duplicate categories on the lattice graph (symmetry function in R)

I am a novice user of R and trying to create a graph using the likert function from the HH package. My problem seems to come from repeating category labels. It’s easier to show the problem:

  library(HH) responses <- data.frame( Subtable= c(rep('Var1',5),rep('Var2',4),rep('Var3',3)), Question=c('very low','low','average','high','very high', '<12', '12-14', '15+', 'missing', '<25','25+','missing'), Res1=as.numeric(c(0.05, 0.19, 0.38, 0.24, .07, 0.09, 0.73, 0.17, 0.02, 0.78, 0.20, 0.02)), Res2=as.numeric(c(0.19, 0.04, 0.39, 0.22, 0.06, 0.09, 0.50, 0.16, 0.02, 0.75, 0.46, 0.20))) likert(Question ~ . | Subtable, responses, scales=list(y=list(relation="free")), layout=c(1,3), positive.order=TRUE, between=list(y=0), strip=FALSE, strip.left=strip.custom(bg="gray97"), par.strip.text=list(cex=.6, lines=3), main="Description of Sample",rightAxis=FALSE, ylab=NULL, xlab='Percent') 

Unfortunately, this creates weird spaces that are not actually present, as shown in the bottom panel of the following graph:

enter image description here

This seems to come from a re-category of "missing." My actual data has several repetitions (for example, "no", "other"), and whenever they are included, I get these extra spaces. If I run the same code but delete duplicate categories, it works correctly. In this case, this means changing the "responses" in the above code to responses[! responses$Question %in% 'missing',] responses[! responses$Question %in% 'missing',] .

Can someone tell me how to create a chart using all categories without getting extra spaces? Thanks for your help and patience.

-Z

 R 3.0.2 HH 3.0-3 lattice 0.20-24 latticeExtra 0.6-26 
+7
r lattice
source share
1 answer

Here is a solution using ggplot2 to create a graphical

 library(ggplot2) responses <- data.frame(Subtable = c(rep('Var1',5), rep('Var2',4), rep('Var3',3)), Question = c('very low','low','average','high','very high', '<12', '12-14', '15+', 'missing', '<25','25+', 'missing'), Res1 = as.numeric(c(0.05, 0.19, 0.38, 0.24, .07, 0.09, 0.73, 0.17, 0.02, 0.78, 0.20, 0.02)), Res2 = as.numeric(c(0.19, 0.04, 0.39, 0.22, 0.06, 0.09, 0.50, 0.16, 0.02, 0.75, 0.46, 0.20)), stringsAsFactors = FALSE) responses$Subtable <- factor(responses$Subtable, levels = paste0("Var", 1:3)) responses$Question <- factor(responses$Question, levels = c("missing", "25+","<25", "<12", "12-14", "15+", "very low", "low", "average", "high", "very high")) ggplot(responses) + theme_bw() + aes(x = 0, y = Question) + geom_errorbarh(aes(xmax = 0, xmin = Res1, color = "red")) + geom_errorbarh(aes(xmin = 0, xmax = -Res2, color = "blue")) + facet_wrap( ~ Subtable, ncol = 1, scale = "free_y") + scale_color_manual(name = "", values = c("red", "blue"), labels = c("Res1", "Res2")) + scale_x_continuous(breaks = c(-0.5, 0, 0.5), labels = c("0.5", "0", "0.5")) + ylab("") + xlab("Percent") + theme(legend.position = "bottom") 

enter image description here

0
source share

All Articles