circles and arrows

36 views
Skip to first unread message

Roney Fraga

unread,
Oct 27, 2014, 9:41:00 AM10/27/14
to ggp...@googlegroups.com
Hello all,

I'm making a chart with the following code:

# ------------
# begin
# ------------
library(ggplot2)
library(grid)
mydf <- read.csv(file='http://dpaste.com/24KGSW9.txt', sep=',')
head(mydf)
g <- ggplot(data=mydf, aes( x=t.ano, y=nn.t.grupo, size=t.ttgrupo)) +
geom_point(shape=21) +
scale_size_area(max_size=12) +
scale_x_continuous(limits=c(min(mydf[,'t.ano']), max(mydf[,'t.ano'])), breaks = seq(min(mydf[,'t.ano']), max(mydf[,'t.ano']),by=1)) +
scale_y_continuous(limits=c(min(mydf[,'nn.t.grupo']), max(mydf[,'nn.t.grupo'])), breaks = seq(min(mydf[,'nn.t.grupo']), max(mydf[,'nn.t.grupo']),by=1)) +
geom_segment( data=mydf, aes( x = xbeg, y = ybeg, xend = xend, yend = yend),
             arrow = arrow(length = unit(0.30,"cm"), type='closed'), size=0.5, color='black') +
theme(legend.position='none')
print(g)
# ------------
# end
# ------------

The link to the graph is here.
http://i.imgur.com/o0nyYzA.png

I need to start and stop the arrows on the edges of the circles, given the size of the circles depends on the variable mydf$t.ttgrupo. I would have to know the radius of the circles to discounts them in variables mydf$xbeg and mydf$xend. Any tips on how to do this?

Best
Roney

Ben Bond-Lamberty

unread,
Oct 27, 2014, 10:43:06 AM10/27/14
to ggplot2
As always I'd prefer to work with a simpler example first. Is this the
effect you want?

d <- data.frame(x=1:3,z=c(1,18,3))
d$xend <- c(d$x[-1], NA)
d$nextz <- c(d$z[-1], NA)
d$xend <- d$xend - d$nextz/750 # this is a scaling factor, I just
fit by trying different values
p <- ggplot(d, aes(x,x)) + geom_point(shape=21,aes(size=z))
p + geom_segment(aes(xend=xend,yend=xend),arrow = arrow(length =
unit(0.30,"cm"), type='closed'))

The cheat here is that 'scaling factor', which should be computed
programatically from the ggplot size scale...but I just tried various
values to find one that looked good. This may or may not work for you.

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Roney Fraga

unread,
Oct 27, 2014, 12:39:02 PM10/27/14
to ggp...@googlegroups.com
Ben, his example made ​​the problem clearer. Thank you! 

This solution is good, but I would have to adjust the scale factor for each database that I am using. It would be great to have a general solution. 

Best, 
Roney

Ben Bond-Lamberty

unread,
Oct 27, 2014, 1:11:58 PM10/27/14
to Roney Fraga, ggplot2
I bet you could do something along these lines to extract the size
scale information:

http://stackoverflow.com/questions/7705345/how-can-i-extract-plot-axes-ranges-for-a-ggplot2-object

For example,

gp <- ggplot_build(p)
sizerange <- gp$plot$scales$scales[[1]]$range$range

...and then use this information to adjust the scaling factor in my
code example. (Sorry, busy day, can't work this out myself right now.)

Ben

Brandon Hurr

unread,
Oct 27, 2014, 1:18:55 PM10/27/14
to Ben Bond-Lamberty, Roney Fraga, ggplot2
I've done something similar before... 

p<-ggplot(data=mtcars, aes(x=mpg))+geom_histogram()

#dissect the guts for the range (default bins)
p.build<-ggplot_build(p)
p.build$panel$ranges[[1]]$y.range[2]

q<-p+scale_y_continuous(limits = c(0,p.build$panel$ranges[[1]]$y.range[2]*1.2))

Roney Fraga

unread,
Oct 27, 2014, 1:42:03 PM10/27/14
to ggp...@googlegroups.com, bpb...@gmail.com, roney...@gmail.com
I will work tonight in this suggestion.

Thanks.

Roney Fraga

unread,
Oct 28, 2014, 7:32:02 AM10/28/14
to ggp...@googlegroups.com, bpb...@gmail.com, roney...@gmail.com
I standardized the group to have 1 as a maximum value: 

    df2$t.ttgrupo.p <- df2$t.ttgrupo/max(df2$t.ttgrupo)

and now all the arrows and circles have the same proportions.

Thanks for the help. 

Roney
Reply all
Reply to author
Forward
0 new messages