[R] Bumps chart in R

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu May 7 13:17:33 CEST 2009


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

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.




More information about the R-help mailing list