[R] horiz barplot with 2 values for each year

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


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,

Try this:

# Create a matrix containing your data
# with one column per year, in this case 2
mydata <- matrix(c(3.2, 5.4, 3.5, 6), ncol = 2)

# Set the column names to be the years
colnames(mydata) <- c(2003, 2004)

# Look at the structure of the data
mydata

# Create the barplot, setting 'beside' = TRUE
# to generate pairs of bars per year
barplot(mydata, beside = TRUE)

By default the color of the first bars will be different than the colors 
of the second bars. You can also explicitly set the colors in the call 
to barplot() using the 'col' argument.

See ?barplot for more information.

Hope that helps,

Marc Schwartz



More information about the R-help mailing list