[R] Problem with names() in a plot after ordering a data.frame. Syntax or stupidity?

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Mar 27 00:27:17 CEST 2006


On 3/26/06, John Kane <jrkrideau at yahoo.ca> wrote:
> I am a complete newbie in R .
>    Using  R 2.2.0  Windows XP
>
> This started as a simple exercise to see if I could
> produce a simple
> Cleveland style dotchart with a line from the '2' axis
> to the dot rather
> than a continuous line as  do "dotchart" and
> "dotchart2".  At least I could not
> find that option.
>
> I got a crude program to work but it includes
> unnecessary steps.
> The data in Area (below) is sorted in reverse order to
> how I want it displayed
> I am doing to many 'sorts'  either a sort() or a rev()
> to handle this problem.
>
> When I change from sorting variables to using order()
> on the data.frame I
> lose my names() When I try to assign it, I get a NULL
> return.
> It is probably a very simple error but I cannot find
> it and I am not sure if
> it is syntax error, a more serious misunderstanding of
> the nature of the names()
> function or a confusion about how a data.frame and
> order() work.
>
> Any help gratefully  received.  Thanks
>
> #-----------------------Read in
> Data--------------------------------------------
>
> Name <- c( "Nunavut", "Québec", "Northwest
> Territories", "British Columbia",
>            "Ontario",  "Alberta",  "Saskatchewan",
> "Manitoba", "Yukon",
>          "Newfoundland",  "New Brunswick","Nova
> Scotia", "Prince Edward Island")
>
> Pcode <- c("NU","QC","NT","BC","ON",
> "AB","SK","MB","YT","NL","NB","NS","PE")
>
> Area <- c(1936113, 1365128, 1183085, 925186, 917741,
> 642317, 591670, 553556,
>             474391, 373872, 71450, 53338, 5660)
>
>  #  ------------------------Crude working
> program------------------------------
>
> provs <- data.frame(Name, Pcode, Area)

One possible solution (to your original problem) is

require(lattice)
dotplot(reorder(Name, Area) ~ Area, provs, type = c('p', 'h'))

-Deepayan




More information about the R-help mailing list