BLASER Arnaud
2013-Jul-24 14:17 UTC
[R] x-axis (categorial variable) ordering with xyplot function (lattice package)
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]]
Duncan Mackay
2013-Jul-24 22:41 UTC
[R] x-axis (categorial variable) ordering with xyplot function (lattice package)
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.
Jim Lemon
2013-Jul-24 23:56 UTC
[R] x-axis (categorial variable) ordering with xyplot function (lattice package)
On 07/25/2013 12:17 AM, BLASER Arnaud wrote:> 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. > > ... > 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 ? >Hi Arnaud, If you want a non-alphabetic ordering of factors, you will have to explicitly specify the order: country<-factor( c("Austria","Belgium","Bulgaria","Czech Republic","United Kingdom"), levels=c("Austria","Belgium","United Kingdom","Bulgaria", "Czech Republic")) Jim