2012-11-24 55 views
0

現在我用ggplot2繪製繪圖。用ggplot2在日期軸上繪製圓圈

我想在我的情節中畫圈。

所以我搜索了它並找到了解決方案。

Draw a circle with ggplot2

但是我不能用這個解決方案,因爲我的情節的x軸的日期格式。

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")) 

如何在我的情節中繪製一個圓圈?

有什麼辦法可以在Date軸上繪製一個圓圈嗎?


target_data_melt看起來像這樣。

 Day variable  value 

1 2010-10-01 231 0.007009346

2 2010-10-03 231 0.005204835

3 2010-10-05 231 0.006214004

+1

你可以輸入你的target_data_melt或至少一個樣本? – agstudy

+0

感謝您評論我的問題。 taget_data_melt看起來像這樣。 日變量值 1 2010-10-01 231 0.007009346 2 2010-10-03 231 0.005204835 3 2010-10-05 231 0.006214004 所以我用@Alexander Vos的德瓦埃勒溶液並固定它一點點。 ^^ – NoSyu

回答

1

可以適應您提供的link的代碼用於格式化x座標作爲日期:

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)) 
} 

而一個示範:

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

實施例的圖像(具有經調整的日期和y中心)here

您必須正確設置r.x和r.y以獲得完美的圓形(而不是橢圓形)。這些應該取決於你在劇情中使用的尺度。

+0

非常感謝! 現在我可以在我的情節畫圓圈! 有趣的一點是y軸是連續的,所以r.x和ry的比例是不同的!所以很難畫出精確的圓,而不是橢圓。 – NoSyu