[R] x-axis (categorial variable) ordering with xyplot function (lattice package)

Duncan Mackay mackay at northnet.com.au
Thu Jul 25 00:41:04 CEST 2013


forgot to cc to list

Hi

For an xyplot you have not got the proper coding 
for the x value which should be numeric.

If you want to make a plot of the style of xyplot 
a numerical index of the country is needed and 
then use the scales argument to annote the labels with the country.
Do you want multiple panels ?

A self contained dataset via dput would help elicit further information.

Have a look at the outer and related arguments as 
well as the group arguments. A combined index for 
regions with countries may be necessary.

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au


At 00:17 25/07/2013, you wrote:
>Content-Type: text/plain
>Content-Disposition: inline
>Content-length: 2322
>
>Dear R mailing list readers,
>
>I am facing the following problem; for 
>simplicity imagine I am working on a data frame 
>of, say, 5 columns. The first column is a list 
>of European countries, the other four are an 
>index (continuous variable) of climate change 
>impact under 4 different scenarios.
>
>Country
>
>2050B2
>
>2050A2
>
>2080B2
>
>2080A2
>
>Austria
>
>-0.2
>
>-0.6
>
>...
>
>
>
>Belgium
>
>-0.2
>
>-0.6
>
>
>
>
>
>Bulgaria
>
>-0.5
>
>-0.8
>
>
>
>
>
>Czech republic
>
>-0.5
>
>-0.8
>
>
>
>
>
>United kingdom
>
>-0.2
>
>-0.6
>
>
>
>
>
>
>I am using the package lattice to make a nice 
>plot of the dots from the different scenario using the following code;
>
>my.plot <- xyplot(2050B2+2050A2+2080B2+2080A2~country, data=my.dat,
>scales=list(x=list(rot=45)))
>
>note: the part "scales=list(x=list(rot=45))" is pure aesthetic here.
>
>So far, so good. However, I wish to order the 
>x-axis (countries) by grouping them by European 
>region; i.e Austria, Belgium and United kingdom 
>are western Europe, while Bulgaria and Czech 
>republic are eastern Europe. In excel I added a 
>new "region) variable (i.e 1 for Western Europe, 
>2 for eastern Europe) and I re-ordered my data 
>frame according to this "region" variable.
>
>I then imported this updated data frame in R, 
>and checked how it looked with the usual code;
>
>pot_dat <-read.csv(file.choose(),header=TRUE, sep=";",dec=".")
>pot_dat
>
>Again, so far so good; my second column 
>("country") is now ordered according to the 
>values of the first column ("region").
>
>Region
>
>Country
>
>2050B2
>
>2050A2
>
>2080B2
>
>2080A2
>
>1
>
>Austria
>
>-0.2
>
>-0.6
>
>...
>
>
>
>1
>
>Belgium
>
>-0.2
>
>-0.6
>
>
>
>
>
>1
>
>United Kingdom
>
>-0.2
>
>-0.6
>
>
>
>
>
>2
>
>Bulgaria
>
>-0.5
>
>-0.8
>
>
>
>
>
>2
>
>Czech republic
>
>-0.5
>
>-0.8
>
>
>
>
>
>
>
>However, when I try to use the code as above, R 
>automatically re-order the x-axis (country) in 
>alphabetical order. This was not unexpected, but 
>I have spent the day (unsuccessfully) looking 
>for a way to simply tell R not to do that and to 
>keep the variable "country" as it is now ordered 
>in the data frame to construct the x-axis of my 
>plot. Is there any way to force it to keep the 
>order as it is in the data frame ?
>
>Any help would be really welcomed !
>
>Best,
>
>Arnaud Blaser
>PhD candidate
>University of Neuchâtel
>Institute of Economic Research (IRENE)
>Pierre-à-Mazel 7
>CH-2000 Neuchâtel
>
>
>         [[alternative HTML version deleted]]
>
>
>______________________________________________
>R-help at 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.



More information about the R-help mailing list