[R] horiz barplot with 2 values for each year

Marc Schwartz mschwartz at medanalytics.com
Sun Apr 20 16:15:15 CEST 2003


Marc Schwartz wrote:
> yakov peretz wrote:
> 
>> Hi,
>>
>> I'm trying to plot a graph where for each year I have 2 numbers best and
>> worse. for example for year 2003 I will two values 3.2 and 5.4, and for
>> year 2004 I will have 3.5 and 6, and so on.
>>
>>
>>
>>  2003  XXXX
>>         XXXXXX
>>
>> 2004   XXXXX
>>         XXXXXXXX
>>
>> For the same year the 2 entries, if possible, will have different color,
>> and will be on the graph without space between them. The space will be
>> between  the years only.
>>
>> Thnx Yakov


Yakov,

Quick correction, I forgot to set the barplot to horizontal and I noted 
that you want the years to be decreasing as you go down the y axis.  So, 
  use this sequence instead:

mydata <- matrix(c(5.4, 3.2, 6, 3.5), ncol = 2)
colnames(mydata) <- c(2003, 2004)
mydata

# This call reverses the columns in 'mydata' by year and sets
# 'horiz' = TRUE to do a horizontal bar plot. The 'las = 1'
# sets the y axis labels to horizontal as well.

barplot(mydata[, rev(colnames(mydata))], beside = TRUE,
         horiz = TRUE, las = 1)

Sorry for the oversight.

Marc



More information about the R-help mailing list