Playthings in the unreal world 2
Peek into beauty

Playthings in the unreal world 3

Some readers may be interested in the R code used to generate the small multiples charts.  The code also highlights one of the virtues of R, which is "elegance": because it natively handles vector and matrix computations, the programmer can (if he or she chooses to) reduce the use of (inelegant) looping.  (Yes, coding elegance is a kind of romantic ideal, and inelegant codes have many practical advantages -- easier to debug, easier to manage, easier to collaborate on with others, etc.)


# reading in data

bigmac = read.csv("bigmac.csv", header=T)

# initializing empty matrix

bigmac2 = matrix(0,nrow(bigmac),nrow(bigmac))
colnames(bigmac2) = bigmac$Country
rownames(bigmac2) = bigmac$Country


# main computation

for(i in colnames(bigmac2)) bigmac2[,i] = bigmac$Price/bigmac$Price[bigmac$Country=="i"]
bigmac3=round(bigmac2-1,3)


# this matrix holds the colors of each bar to be plotted

bigmaccol= (bigmac3>0)


# graphical parameters

par(mar=c(3,7,3,1), mfrow=c(2,2),cex.axis=0.8, cex=1)


# plotting

for (i in c("US","EuroArea","Japan","China")) {
    barplot(rev(bigmac3[,i]), horiz=T, xlim=c(-1,3),las=2, col=rev(bigmaccol[,i]), main=paste("Relative to ",i))
}


In the main computation step, the one formula takes the original vector of prices (the left column in the Economist's chart), computes relative prices 23 times using successively each country's price as the standard, and deposits all 23 vectors into a matrix.  Then, the next step takes the entire matrix, subtracts 1 from each entry, and rounds each entry to 3 decimal places.

In plotting, the default options are not satisfactory: the changes I made included switching from columns to bars, reversing the order of plotting, setting the left and right edges of the value axis, turning the country labels on its side (this also turns the value labels -- there is a way to set this for one axis only but not the other but I did not bother), making the positive bars black and the negatives white, and supplying titles that are dynamically assigned according to reference country.

Also, it is almost always true that the global graphical parameters need to be adjusted.  Here, I controlled the amount of white space on each side of the plotting window, set up a 2x2 grid of charts, and changed the font size on the axes.

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Andreas

very elegant I must say. I would have done this much more clonky. Could you provide a link to bigmac.csv mayby?

Bob

I second Andreas' request - self-contained demos are a great way for me to learn other people's R styles.

Kaiser

Here you go:

Country,Price
Norway,7.02
Switzerland,6.3
EuroArea,4.84
Australia,3.98
Canada,3.97
Hungary,3.86
Turkey,3.83
Britain,3.67
US,3.58
Japan,3.5
Singapore,3.19
UAE,2.99
SouthKorea,2.98
Poland,2.86
Mexico,2.5
SouthAfrica,2.46
Egypt,2.38
Taiwan,2.36
Russia,2.34
Indonesia,2.24
Thailand,2.11
Malaysia,2.08
China,1.83

Kerry26wS

Oh, hot knowledge referring to this post. Will please let me know how much time this will take? Because I would like to complete some thesis topic or may be this would be good to opt for the buy dissertation service. Thank you.

Notme

The double quotes around i shouldn't be there!

Thesis Writing

article abosuoltlty great for me, its so beautiful tips given to me,

The comments to this entry are closed.