[R] Bumps chart in R

Mike Lawrence Mike.Lawrence at dal.ca
Mon Apr 27 02:23:29 CEST 2009


Here's a ggplot2 based solution:

#load the ggplot2 library
library(ggplot2)

#here's the data provided by Andreas
countries <- c("U-lande", "Afrika syd for sahara", "Europa og
Centralasien", "Lantinamerika og Caribien","Mellemøstenog
Nordafrika","Sydasien","ØStasien og stillehaveet", "Kina",
"Brasilien")
poor_1990 <- c(28.7,46.7,0.5,10.2,2.3,43,29.8,33,14)
poor_2004 <- c(18.1,41.1,0.9,8.6,1.5,30.8,9.1,9.9,7.5)

#reformat the data
data = data.frame(countries,poor_1990,poor_2004)
data = melt(data,id=c('countries'),variable_name='year')
levels(data$year) = c('1990','2004')

#make a new column to make the text justification easier
data$hjust = 1-(as.numeric(data$year)-1)

#start the percentage plot
p = ggplot(
	data
	,aes(
		x=year
		,y=value
		,groups=countries
	)
)

#add the axis labels
p = p + labs(
	x = '\nYear'
	, y = '%\n'
)

#add lines
p = p + geom_line()

#add the text
p = p + geom_text(
	aes(
		label=countries
		, hjust = hjust
	)
)

#expand the axis to fit the text
p = p + scale_x_discrete(
	expand=c(2,2)
)

#show the plot
print(p)


#rank the countries
data$rank = NA
data$rank[data$year=='1990'] = rank(data$value[data$year=='1990'])
data$rank[data$year=='2004'] = rank(data$value[data$year=='2004'])

#start the rank plot
r = ggplot(
	data
	,aes(
		x=year
		,y=rank
		,groups=countries
	)
)

#add the axis labels
r = r + labs(
	x = '\nYear'
	, y = 'Rank\n'
)

#add the lines
r = r + geom_line()

#add the text
r = r + geom_text(
	aes(
		label=countries
		, hjust = hjust
	)
)

#expand the axis to fit the text
r = r + scale_x_discrete(
	expand=c(2,2)
)

#show the plot
print(r)


-- 
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~




More information about the R-help mailing list