I'm creating a stacked bar chart using the likert command in the HH package. My data are in a data frame, with two numeric variables and a categorical variable, I can't get likert to use the column containing the categorical variable as a my y axis label. Here is a quick example: library(HH) #my data are: df<-data.frame(as.character(cat=c("group1","group2","group3","group4", "group5")),males=c(20,30,45,12,5),females=c(35,23,32,8,5)) #make a pyramid Likert chart p<-likert(df) as.pyramidLikert(p) It tries to plot three variables here when I just want two. I think I understand what is happening - my categorical variable is treated as a factor and I think it gets inserted as an integer into the matrix which the command derives from my data fame, to make the plot with(?) It's then used as a variable to be plotted just like the other two variables. what I don't get is how the example given in the package does something differently, which is how I want mine to work. ## Population Pyramid data(USAge.table) USA79 <- USAge.table[75:1, 2:1, "1979"]/1000000 PL <- likert(USA79, main="Population of United States 1979 (ages 0-74)", xlab="Count in Millions", ylab="Age", scales=list( y=list( limits=c(0,77), at=seq(1,76,5), labels=seq(0,75,5), tck=.5)) ) PL as.pyramidLikert(PL) This does exactly what I'm trying to achieve. here the two population counts are plotted in the likert plot and the age groups in the first columns are used as labels. I can't work out why in my example the age group variable is not used in the same way as the in my plot in the same way as the agegroups in this example, other than the example takes it's data from a table and mine is coming from a data frame. The end point I want is a stacked Likert bar chart based on a data frame where the column containing the description of my groups is used as the y axis labels and the other two columns are used to draw the bars. I'm sure I'm missing a simple solution. any help gratefully received. Gavin. [[alternative HTML version deleted]]
Gavin, thank you for using likert() There are several problems in the use of a data.frame. 1. df is a bad name to use because df is the name of a base function (that isn't an error, it is bad style). 2. the as.character() made the line you sent not work. 3. you indeed have the variable cat as a factor. you need to make it the row.names of the data.frame. mydata <- data.frame( row.names=c("group1", "group2", "group3", "group4", "group5"), males=c(20,30,45,12,5), females=c(35,23,32,8,5)) ## make a pyramid Likert chart as.pyramidLikert(likert(mydata), panel.width=.46) Rich On Tue, May 22, 2012 at 7:29 AM, Gavin Rudge <g.rudge@bham.ac.uk> wrote:> I'm creating a stacked bar chart using the likert command in the HH > package. My data are in a data frame, with two numeric variables and a > categorical variable, I can't get likert to use the column containing the > categorical variable as a my y axis label. > > Here is a quick example: > > library(HH) > #my data are: > df<-data.frame(as.character(cat=c("group1","group2","group3","group4", > "group5")),males=c(20,30,45,12,5),females=c(35,23,32,8,5)) > #make a pyramid Likert chart > p<-likert(df) > as.pyramidLikert(p) > > It tries to plot three variables here when I just want two. I think I > understand what is happening - my categorical variable is treated as a > factor and I think it gets inserted as an integer into the matrix which the > command derives from my data fame, to make the plot with(?) It's then used > as a variable to be plotted just like the other two variables. what I don't > get is how the example given in the package does something differently, > which is how I want mine to work. > > ## Population Pyramid > data(USAge.table) > USA79 <- USAge.table[75:1, 2:1, "1979"]/1000000 > PL <- likert(USA79, > main="Population of United States 1979 (ages 0-74)", > xlab="Count in Millions", > ylab="Age", > scales=list( > y=list( > limits=c(0,77), > at=seq(1,76,5), > labels=seq(0,75,5), > tck=.5)) > ) > PL > as.pyramidLikert(PL) > > This does exactly what I'm trying to achieve. here the two population > counts are plotted in the likert plot and the age groups in the first > columns are used as labels. > > I can't work out why in my example the age group variable is not used in > the same way as the in my plot in the same way as the agegroups in this > example, other than the example takes it's data from a table and mine is > coming from a data frame. The end point I want is a stacked Likert bar > chart based on a data frame where the column containing the description of > my groups is used as the y axis labels and the other two columns are used > to draw the bars. I'm sure I'm missing a simple solution. any help > gratefully received. > > Gavin. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Dear Richard On Tue, May 22, 2012 at 3:20 PM, Richard M. Heiberger <rmh at temple.edu> wrote:> mydata <- data.frame( > ? ?row.names=c("group1", "group2", "group3", "group4", "group5"), > ? ?males=c(20,30,45,12,5), > ? ?females=c(35,23,32,8,5)) > ## make a pyramid Likert chart > as.pyramidLikert(likert(mydata), panel.width=.46) >Loading library(HH) and running the code above fails with this error:> as.pyramidLikert(likert(mydata), panel.width=.46)Error in inherits(x, "trellis") : could not find function "likert" Am I doing something wrong? Liviu> sessionInfo()R version 2.14.2 (2012-02-29) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] splines grid stats graphics grDevices utils datasets [8] methods base other attached packages: [1] HH_2.2-23 latticeExtra_0.6-19 RColorBrewer_1.0-5 [4] leaps_2.9 multcomp_1.2-12 survival_2.36-12 [7] mvtnorm_0.9-9992 lattice_0.20-6 plyr_1.7.1 [10] Defaults_1.1-1 fortunes_1.5-0 sos_1.3-1 [13] brew_1.0-6