Skip to content

Bumps chart in R

5 messages · Andreas Christoffersen, Michael Lawrence, ONKELINX, Thierry

#
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:
#
(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:

  
    
#
On Thu, May 7, 2009 at 2:15 AM, Mike Lawrence <Mike.Lawrence at dal.ca> wrote:
Thank you

with your melted data I was able to come very close to what I want.
legen.posistion="none" works for me... Annonying that colours don't
work like expected... It seams to be a frequent concern.

My code:
qplot(tal,value,data=df,group=text,geom="line",size=size,colour=col,xlab="Change
from time to time", ylab = "Points")+
geom_text(aes(label=text),subset(df,tal=="tal1"),size=3,hjust=2,vjust=0)+
theme_bw()+ opts(legend.position="none")+ opts(title="As time went
by") +opts(plot.title=theme_text(vjust=0,size=20))
#
Dear Andreas and Mike,

You need to use scale_colour_manual() if you want to set the colours yourself. Ggplot2 interpretes the "col" variable in the dataset as an ordinairy factor. 

library(ggplot2)
df0 <- data.frame(text = letters[1:21], tal1 = c(rnorm (20,5,2), 5), tal2 = c(rnorm (20,6,3), 5))
df0$dif <- df0$tal2 - df0$tal1
df <- melt(data = df0, id.vars = c('text', 'dif'), variable_name = 'tal')
df$Change <- factor(ifelse(df$dif>0,'Up',ifelse(df$dif<0,'Down','Stable')), levels = c("Up", "Stable", "Down"))
df$size <- abs(df$dif)
ggplot(df, aes(x = tal, y = value, label = text, group = text, size = size, col = Change)) + geom_line() + geom_text(data = subset(df, tal=="tal1"), size=3, hjust=2, vjust=0) + scale_x_discrete("Change from time to time") + scale_y_continuous("Points") + theme_bw()+ opts(legend.position="none")+ opts(title="As time went by\n") +opts(plot.title=theme_text(vjust=0,size=20)) + scale_colour_manual(values = c("red", "black", "blue"))

HTH,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Andreas Christoffersen
Verzonden: donderdag 7 mei 2009 12:22
Aan: Mike Lawrence; r-help at r-project.org; ggplot2 at googlegroups.com
Onderwerp: Re: [R] Bumps chart in R
On Thu, May 7, 2009 at 2:15 AM, Mike Lawrence <Mike.Lawrence at dal.ca> wrote:
Thank you

with your melted data I was able to come very close to what I want.
legen.posistion="none" works for me... Annonying that colours don't
work like expected... It seams to be a frequent concern.

My code:
qplot(tal,value,data=df,group=text,geom="line",size=size,colour=col,xlab="Change
from time to time", ylab = "Points")+
geom_text(aes(label=text),subset(df,tal=="tal1"),size=3,hjust=2,vjust=0)+
theme_bw()+ opts(legend.position="none")+ opts(title="As time went
by") +opts(plot.title=theme_text(vjust=0,size=20))

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

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.
#
Thank you Thierry! That was very helpfull

I have added "geom_point" which makes the lineending look more round.
Also - since the chart becomes verye clustered I also added text to
the right.

like so:
ggplot(df, aes(x = tal, y = value, label = text, group = text, size =
size, col = Change))+
 geom_line()+
 geom_text(data = subset(df, tal=="tal1"),size=3, hjust=1, vjust=0)+
 geom_text(data = subset(df, tal=="tal2"),hjust=-1, size=3)+
 geom_point() + scale_x_discrete("Change from time to time") +
 scale_y_continuous("Points") +
 theme_bw()+
 opts(legend.position="none")+
 opts(title="As time went by")+
 opts(plot.title=theme_text(vjust=0,size=20))+
 scale_colour_manual(values = c("red", "black", "blue"))

Thanks for all your help!

On Thu, May 7, 2009 at 1:17 PM, ONKELINX, Thierry
<Thierry.ONKELINX at inbo.be> wrote: