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.jpg 


barplot() 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

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to