[R] ggplot2 and add circle

Dennis Murphy djmuser at gmail.com
Wed May 11 18:55:27 CEST 2011


Hi:

Here's a slight modification of the earlier code .

plot_shad <- function(d, r, dtxt) {
  require('ggplot2')
  plotdata <- melt(d)
  names(plotdata)<-c('x','y','z')
  xc <- mean(range(plotdata$x))
  yc <- mean(range(plotdata$y))
  theta <- seq(-pi, pi, length = 200)
  circ <- data.frame(xv = xc + r * cos(theta),
                     yv = yc + r * sin(theta))
  v <- ggplot(plotdata)
  print(v + geom_tile(aes(x = x, y = y, fill = z)) +
        geom_path(data = circ, aes(x = xv, y = yv),
                  color = 'white', size = 1) +
        geom_text(data = dtxt, aes(x = x, y = y, label = lab),
                  color = 'white') +
        coord_equal()
       )
 }

f <- matrix(data=seq(1:10000), nrow=100, ncol=100)
dftxt <- data.frame(x = 80, y = 90, lab = 'r = 0.227')

plot_shad(f, 10, dftxt)


It's not hard to create a separate text string with paste() and then
use it for the lab argument in the dftxt data frame, something like

txt <- paste('r =', with(mydata, round(cor(x, y), 3))
dftxt <- data.frame(x = 80, y = 90, lab = txt)
plot_shad(...)

HTH,
Dennis

On Tue, May 10, 2011 at 11:43 PM, Alaios <alaios at yahoo.com> wrote:
> Thanks a lot this worked nice.
> It it possible also in ggplot2 to add a small figure? Let's say that I want to have somewhere in the plot the value of r printed.
>
> How can I do that with ggplot 2?
>
> Regards
> Alex
>
> --- On Tue, 5/10/11, Dennis Murphy <djmuser at gmail.com> wrote:
>
>> From: Dennis Murphy <djmuser at gmail.com>
>> Subject: Re: [R] ggplot2 and add circle
>> To: "Alaios" <alaios at yahoo.com>
>> Cc: R-help at r-project.org
>> Date: Tuesday, May 10, 2011, 7:12 PM
>> Hi:
>>
>> Here's one way:
>>
>> plot_shad <- function(d, r) {
>>    require('ggplot2')
>>    plotdata <- melt(d)
>>    names(plotdata)<-c('x','y','z')
>>    xc <- mean(range(plotdata$x))
>>    yc <- mean(range(plotdata$y))
>>    theta <- seq(-pi, pi, length = 200)
>>    circ <- data.frame(xv = xc + r *
>> cos(theta),
>>
>>       yv = yc + r * sin(theta))
>>    v <- ggplot(plotdata)
>>    print(v + geom_tile(aes(x = x, y = y,
>> fill = z)) +
>>          geom_path(data =
>> circ, aes(x = xv, y = yv), color = 'white',
>> size = 1) +
>>          coord_equal()
>>         )
>>  }
>>
>> plot_shad(f, 10)
>>
>> HTH,
>> Dennis
>>
>> On Tue, May 10, 2011 at 10:15 AM, Alaios <alaios at yahoo.com>
>> wrote:
>> > Here you are :)
>> >
>> >
>> > plot_shad_f<-function(f){
>> >   library(ggplot2)
>> >   dev.new()
>> >   plotdata<-melt(f)
>> >   names(plotdata)<-c('x','y','z')
>> >   v<-ggplot(plotdata, aes(x, y, z = z))
>> >   print(v + geom_tile(aes(fill=z)))
>> >
>> > }
>> >
>> >
>> > f<-matrix(data=seq(1:10000),nrow=100,ncol=100)
>> > plot_shad_f(f)
>> >
>> >
>> > I would like to add a circle at the middle of this
>> region with a range of 10.
>> >
>> > Best Regards
>> >
>> > Alex
>> >
>> > --- On Tue, 5/10/11, Scott Chamberlain <scttchamberlain4 at gmail.com>
>> wrote:
>> >
>> > From: Scott Chamberlain <scttchamberlain4 at gmail.com>
>> > Subject: Re: [R] ggplot2 and add circle
>> > To: "Alaios" <alaios at yahoo.com>
>> > Cc: R-help at r-project.org
>> > Date: Tuesday, May 10, 2011, 5:59 PM
>> >
>> >
>> >
>> >                You should provide reproducible
>> data in addition to your code.
>> > S
>> >
>> >
>> >
>> >
>> >
>> >                On Tuesday, May 10, 2011 at
>> 11:54 AM, Alaios wrote:
>> >
>> >                    Dear all,
>> > today I have writted the following code,
>> > to plot the contents of some matrices I have
>> >
>> > plot_shad_f
>> > function(f){
>> >  library(ggplot2)
>> >  dev.new()
>> >  plotdata<-melt(f)
>> >  names(plotdata)<-c('x','y','z')
>> >  v<-ggplot(plotdata, aes(x, y, z = z))
>> >  print(v + geom_tile(aes(fill=z)))
>> > }
>> >
>> > I would like to ask your help add a small circle in
>> this plotting. What would be the easiest way to do that in
>> ggplot2?
>> >
>> > Best Regards
>> > Alex
>> >
>> > ______________________________________________
>> > 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.
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> >
>> > ______________________________________________
>> > 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.
>> >
>> >
>>
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list