2016-11-13 5 views
1

Q1.ggplotで次のような動作を避ける方法はありますか?ggplotの変更を避ける

x=c(1,3,4,5,6) 
y=c(0.5,2,3,7,1) 
n=2 
library(ggplot2) 
p <- ggplot(mtcars, aes(wt, mpg)) 
p <-p + geom_segment(aes(x = x[1], y = y[1], xend = x[2], yend = y[2]), colour = "red") 
p <-p + geom_segment(aes(x = x[2], y = y[2], xend = x[3], yend = y[3]), colour = "red") 
p 

enter image description here

x[1]=10 
p 

enter image description here

プロット上の図は、入力で与えられた数値の値に固定のままです。座標を格納するための他の変数を作成する必要がありますか?

Q2:ggplotにpolygonal chainを描画する方法?

n=length(x)-1 
library(ggplot2) 
p <- ggplot(mtcars, aes(wt, mpg)) 
for (i in 1:n){ 
    p <-p + geom_segment(aes(x = x[i], y = y[i], xend = x[i+1], yend = y[i+1]), colour = "red") 
    p$plot_env <- list2env(list(x=x,y=y)) 
    } 

ここで回答:Drawing polygonal chains in ggplot

+0

OK、私はそれを取り戻します、私はあなたの編集は実際に新しい質問であるべきだと思います。 (シフトされた座標 'x [-1]、x [-length(x)]、... 'を使用するか、おそらくはより良い' 'geom_path()' ")を使用していくつかの答えがあります。 –

+0

@Ben Bolker多角形チェーンはgeom_path()を使用していますか? – user3083324

+0

はい、私はそう考えています(グループの美しさを使用してください) –

答えて

0

これはハックのように思えるが、明らかに動作します:xを変更する前に、

p$plot_env <- list2env(list(x=x,y=y)) 

を行うその後

x[1] <- 10 
p 

はそのままプロットを残します。

+0

これはforサイクルでx [i]を扱うために適応できると思いますか? – user3083324

+0

あなたは何を意味するか分かりません。あなたの質問を編集しますか? (あなたは 'assign()'によって環境変数を変更することができます...) –

+0

Q2を見てください。 – user3083324

関連する問題