control scale size for continuous variable?

2,240 views
Skip to first unread message

squid

unread,
Oct 5, 2009, 7:12:37 AM10/5/09
to ggplot2
Is it possible to to control the size mapping for a continuous
variable ?

For a discrete variable, scale_size_manual works, but I have ranges
of continuous data I'd like to have different thicknesses of lines
for.

I've uploaded a small example file called "scale_continuous_ex.txt"

Here's an example for the plot

test<-read.table("scale_continuous_ex.txt", header=T)

ggplot(data=test, aes(x_axis)) +
geom_line(aes(y = variable,size= thicknessOfLine ),colour="seagreen4")
+ scale_size(to = c(0.05, .5))


But what I'd like to do is force particular line thicknesses for
particular ranges. Say, 0-20000, 20000-40000, and above 40000. How
can control of continuous scale variables be achieved?

Thank you.
Squid.


Mike Lawrence

unread,
Oct 5, 2009, 8:05:04 AM10/5/09
to squid, ggplot2
Best I could come up with is to bin the thickness data outside ggplot2 first:

test$thick2 = with(test,ifelse(thicknessOfLine<20000,1,ifelse(thicknessOfLine<40000,2,3)))

ggplot(data=test, aes(x_axis)) +
geom_line(aes(y = variable,size= thick2 ),colour="seagreen4") +
scale_size(breaks = c(1,2,3),labels = c('0-20000','20000-40000','40000+'))
--
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. ~

baptiste auguie

unread,
Oct 5, 2009, 8:17:25 AM10/5/09
to squid, ggplot2
Hi,

You can specify the breaks as well as the size limits,

test<-read.table("scale_continuous_ex.txt", header=T)

ggplot(data=test, aes(x_axis)) +
geom_line(aes(y = variable,size= thicknessOfLine
),colour="seagreen4")+ scale_size(breaks = c(1,2,4)*1e4, to=
c(0.005,5))

However I think that if you want to set the size for each intermediate
interval, then you're effectively considering your variable as
discrete and you probably want to convert it to factor beforehand.

HTH,


baptiste

hadley wickham

unread,
Oct 5, 2009, 9:37:25 AM10/5/09
to baptiste auguie, squid, ggplot2
> However I think that if you want to set the size for each intermediate
> interval, then you're effectively considering your variable as
> discrete and you probably want to convert it to factor beforehand.

Yes, exactly. And have a look at cut for that purpose.

Hadley


--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages