[R] Bumps chart in R
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:
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.
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
> 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")+
> # 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Department of Psychology
Looking to arrange a meeting? Check my public calendar:
~ Certainty is folly... I think. ~
More information about the R-help