[R] Bumps chart in R

Mike Lawrence Mike.Lawrence at dal.ca
Thu May 7 02:15:30 CEST 2009


(cross posting to the ggplot2 group for posterity)

Here's how I'd approach it:

library(ggplot2)
text = letters[1:20]
tal1 = rnorm (20,5,2)
tal2 = rnorm (20,6,3)
dif = tal2-tal1
df0 = data.frame(text,tal1,tal2)
df = melt(
	data = df0
	, id.vars = 'text'
	, variable_name = 'tal'
)
df$dif = dif
df$col = ifelse(dif>0,'red',ifelse(dif<0,'blue','black'))
df$size = abs(dif)

# draw the plot
ggplot(
	data=df
	, aes(
		x=tal
		, y=value
		, group=text
	)
) +
geom_line(
	aes(
		size=size
		, colour=col
	)
)+
geom_text(
	aes(
		label=text
	)
)+
opts(
	legend.position="none"
)+
theme_bw()


Unfortunately it's not perfect:
(1) col isn't being interpreted literally, so instead of truly red & blue.
(2) the lines ends are square whereas usually they're rounded.
(3) attempting to remove the legend via opts(legend.position="none")
seems to fail.

On Wed, May 6, 2009 at 6:44 PM, Andreas Christoffersen
<achristoffersen at gmail.com> wrote:
> Okay - first off: Thank you all for you kind help so far. (Especially
> to hadly for writing ggplot2). I feel that I am beginning to
> understand the grammar of graphics - but obviously still have a long
> way to go. Some of the road I need to travel has to do with basic R.
> Anyway ; instead of wrting a new post - I thought it best to post in
> the current topic. please correct me if I am wrong.
>
> getting to the point: I have expanded upon the original bumps chart
> (or what ever type of chart it is). I'd like the line width to be
> proportional to the change between time 1 and time 2. Also: I'd like
> colous to be red if the change is negative, and black if the change is
> positive.
>
> My solutions produces some problems for me:
>
> library(ggplot2) # Loads ggplot2
> text <- letters[1:20] # letters is proxy for categories
> tal1 <- rnorm (20,5,2) # random numbers for first
> tal2 <- rnorm (20,6,3) # random numbers for second
> dif <- tal2-tal1 # difference between second and first
> df0 <- cbind(tal1,tal2,dif) # do dataframe
> df <- melt(df0) # melt
> farve <- c(2,1,1,2,2,1,2,2,2,2,1,1,1,2,2,1,1,1,2,2) # define colours -
> black for positive change, red for negative change
> # these colours I handcode - depending on the random generated - so it
> will not fit new data.
>
> # draw the plot
> qplot(X2, value,data=df,  group=X1,geom="blank")+
>  geom_line(aes(group=X1),subset(df,X2!="dif"),size=scale(abs(subset(df,df$X2=="dif")$value),center=F,scale=T)[,1],colour=farve)+
>  geom_text(aes(label=X1),subset(df,X2=="tal2"),size=3,hjust=-3,vjust=1)+theme_bw()
>
> # My questions:
> # How to do colours automaticaly
> # how to remove "dif" from the X axis? - subset doesn't seem to work? - eg
> # qplot(subset(df,X2!="dif",X2, drop=T), value,data=df) - returns an error.
> # Hot to use melt better - so that text becomes the id? id=text doesn't work.
>
> thanks in advance
>
> On Tue, Apr 28, 2009 at 12:09 AM, Andreas Christoffersen
> <achristoffersen at gmail.com> wrote:
>>
>> My legend is removed! - Couldn't find it in your ggplot2 book - but
>> here it is. Brilliant - thank you very much.
>>
>
> ______________________________________________
> 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.
>



-- 
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