tags:

views:

81

answers:

2

I need to plot a graph where the X values are all names -e.g. states of America

and the Y values are numberic and in descending order- e.g. population of the states of America.

Currently, when I use the plot function, it plots a graph but a) The Y values are not in descending order and b) the X Axis displays a bunch of ascending numbers, not the names of the states.

Thanks for the help.

EDIT:

I was using R's data sets: state.name and state.area

>state.name

[1]"Alabama" etc

>state.area

[1] 51609 etc

my actual data is very similar so I was just playing around with those.

Trial.Group   Mean Heart Rate      Upper Confidence Interval    Lower Confidence Interval
33subj-Male   80                    120                          70
+2  A: 

Sounds like you want a barchart (or maybe a dotplot). There are 3 different plotting systems in R; here are solutions in order of preference.

#Some US state data
data(state)
dfr <- data.frame(name = state.name, area = state.area)
dfr$name <- with(dfr, factor(name, levels = name[order(area)]))

#The ggplot way
library(ggplot2)
ggplot(dfr, aes(name, area)) + geom_bar() + coord_flip()

#The lattice way
library(lattice)
barchart(name ~ area, data = dfr)

#The base way
par(las = 1, mar = c(4, 7, 1, 1))
with(dfr, barplot(area, names.arg = name, horiz = TRUE))

EDIT: I made the bars horizontal in order to make it easier to read the labels.

Richie Cotton
A: 

Another example with traditional graphics

barplot(dfr$area,
        las=2,
        names.arg = dfr$name,
        cex.names=0.7)

The link below shows you how to fit long names under barplot. http://onertipaday.blogspot.com/2008/09/fitting-text-under-plot.html

ilya