Hello, I am attempting to create a bar plot that contains a range of possible response values on the x-axis of 1 to 5 and contains barplots for the number of responses even in the event that there are 0 responses. For example, I have a data set that contains values of 2, 3, 4, and 5 but I would also like my graph to show that there are no 1's. I have attached the resulting graph. The appropriate values should be 0 - Strongly Disagree, 1 - Somewhat Disagree, 2 - Neutral, 7 - Somewhat Agree, and 12 - Strongly Agree. Any suggestions would be much appreciated as I am new to R. Thanks, Steve http://r.789695.n4.nabble.com/file/n2315414/graph.jpg -- View this message in context: http://r.789695.n4.nabble.com/Plotting-range-of-values-in-barplot-tp2315414p2315414.html Sent from the R help mailing list archive at Nabble.com.
On Aug 5, 2010, at 1:22 PM, yankeetilidie wrote:> > Hello, > > I am attempting to create a bar plot that contains a range of possible > response values on the x-axis of 1 to 5 and contains barplots for the number > of responses even in the event that there are 0 responses. For example, I > have a data set that contains values of 2, 3, 4, and 5 but I would also like > my graph to show that there are no 1's. > > I have attached the resulting graph. The appropriate values should be 0 - > Strongly Disagree, 1 - Somewhat Disagree, 2 - Neutral, 7 - Somewhat Agree, > and 12 - Strongly Agree. > > Any suggestions would be much appreciated as I am new to R. > > Thanks, > Steve > > http://r.789695.n4.nabble.com/file/n2315414/graph.jpgbarplot() will plot the tabulation of the variables included in the available data set. If you want to include missing categories, then you need to set the underlying raw data to a factor, specifying the additional levels for the missing categories. So, presuming that you have the raw data in a vector 'MyData': MyData <- c("Somewhat Disagree", rep("Neutral", 2), rep("Somewhat Agree", 7), rep("Strongly Agree", 12))> MyData[1] "Somewhat Disagree" "Neutral" "Neutral" [4] "Somewhat Agree" "Somewhat Agree" "Somewhat Agree" [7] "Somewhat Agree" "Somewhat Agree" "Somewhat Agree" [10] "Somewhat Agree" "Strongly Agree" "Strongly Agree" [13] "Strongly Agree" "Strongly Agree" "Strongly Agree" [16] "Strongly Agree" "Strongly Agree" "Strongly Agree" [19] "Strongly Agree" "Strongly Agree" "Strongly Agree" [22] "Strongly Agree"> table(MyData)MyData Neutral Somewhat Agree Somewhat Disagree Strongly Agree 2 7 1 12 Now, create a factor with the categories in the order that you want and with the additional level(s) that refer to missing categories: MyData <- factor(MyData, levels = c("Strongly Disagree", "Somewhat Disagree", "Neutral", "Somewhat Agree", "Strongly Agree"))> table(MyData)MyData Strongly Disagree Somewhat Disagree Neutral Somewhat Agree 0 1 2 7 Strongly Agree 12 Now do the barplot(): MyTab <- table(MyData) barplot(MyTab, names.arg = MyTab) HTH, Marc Schwartz
Works perfectly. Thank you! -- View this message in context: http://r.789695.n4.nabble.com/Plotting-range-of-values-in-barplot-tp2315414p2315725.html Sent from the R help mailing list archive at Nabble.com.
On 08/06/2010 04:22 AM, yankeetilidie wrote:> > Hello, > > I am attempting to create a bar plot that contains a range of possible > response values on the x-axis of 1 to 5 and contains barplots for the number > of responses even in the event that there are 0 responses. For example, I > have a data set that contains values of 2, 3, 4, and 5 but I would also like > my graph to show that there are no 1's. > > I have attached the resulting graph. The appropriate values should be 0 - > Strongly Disagree, 1 - Somewhat Disagree, 2 - Neutral, 7 - Somewhat Agree, > and 12 - Strongly Agree.Hi Steve, Given your labels, I would suggest the following: smw.dat<-factor(rep(c("Somewhat Disagree","Neutral", "Somewhat Agree","Strongly Agree"),c(1,2,7,12)), levels=c("Strongly Disagree","Somewhat Disagree","Neutral", "Somewhat Agree","Strongly Agree"),ordered=TRUE) library(prettyR) smw.freq<-freq(smw.dat,display.na=FALSE) library(plotrix) barp(smw.freq,names.arg=levels(smw.dat),staxx=TRUE, col=rainbow(5),main="Survey answers",xlab="Responses") Jim