0

Now I draw plot with ggplot2.

I want to draw circle in my plot.

So I searched it and found the solutions.

Draw a circle with ggplot2

However I can't use this solution, because my plot's x axis is Date format.

my_plot <- qplot(Day, value, data = target_data_melt, shape = variable, colour = variable, geom="line")
my_plot <- my_plot + scale_x_date(labels = date_format("%Y-%m"))

How can I draw a circle in my plot?

Is there any way to draw a circle in Date axis?


target_data_melt looks like this.

      Day  variable       value

1 2010-10-01 231 0.007009346

2 2010-10-03 231 0.005204835

3 2010-10-05 231 0.006214004

Community
  • 1
  • 1
NoSyu
  • 3
  • 3
  • 1
    can you dput your target_data_melt or at least a sample? – agstudy Nov 24 '12 at 12:15
  • Thanks for commenting my question. taget_data_melt look like this. Day variable value 1 2010-10-01 231 0.007009346 2 2010-10-03 231 0.005204835 3 2010-10-05 231 0.006214004 So I used @Alexander Vos de Wael solution and fixed it a little bit.^^ – NoSyu Nov 25 '12 at 01:44

1 Answers1

1

You can adapt the code from the link you provided to format the x-coordinate as Date:

require("date")

circle <- function(center_Date = as.Date("2012-11-24"), 
                   center_y = 0, 
                   r.x = 100,
                   r.y = 100,
                   npoints = 100) {
  cycle <- seq(0,2*pi,length.out = npoints)
  xx <- center_Date + r.x * cos(cycle)
  yy <- center_y + r.y * sin(cycle)
  return(data.frame(x = xx, y = yy))
}

And a demonstration:

df <- circle()
plot <- ggplot(df, aes(x, y)) + geom_path()
plot(plot)

Example image (with an adjusted date and y-center) here.

You'll have to set the r.x and r.y properly to get a perfect circle (rather than an oval). What these should be depends on the scales you use in your plots.

Community
  • 1
  • 1
  • Thanks a lot! Now I can draw circles in my plot! One interesting point is y axis is continuous one, so r.x and r.y scale is different!. So it's hard to draw exact circle, not ellipse. – NoSyu Nov 25 '12 at 01:43