私の最初のデータフレームです。 ggplotを作るためにaes_stringを使ってgeom_pointの色を動的列から変更する
data.df
x y z label
2 3 4 1
1 2 3 2
2 4 3 3
、唯一の1列(ラベル)がある場合、これは動作します:
g <- ggplot(data.df) +
geom_point(data = data.df, aes(x= x, y= y,
color = ifelse((label == 2), "a", "b")+
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
return g
は "マージ" というボタンをクリックすると、新しい列動的に追加されます:
x y z label label2
2 3 4 1 1
1 2 3 2 2
2 4 3 3 2
ggplotではラベル列(label2、label3 ...の可能性があります)の代わりに最後の列を入力し、ggplotを更新します。
私は2つの方法を試しました。
g <- ggplot(data.df) +
geom_point(data = data.df, aes(x= x, y= y,
color = ifelse((data.df[, ncol(data.df)] == 2, "a", "b")+
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
return g
data.df [、NcoI部位(data.df)]を使用している間、私はエラーを取得してい示されているように:
Error: Aesthetics must be either length 1 or the same as the data (40): x, y, colour
私はaes_stringの代わりにAESを使用することができます感じています。
label <- paste("label", counter , sep="")
g <- ggplot(data.df) +
geom_point(data = data.df, aes_string(x= "x", y= "y",
color = ifelse((label == 2), a, b))) +
scale_colour_manual(values= c("a" = "blue", "b" = "green"))
私はこのエラーを取得しています:
Error in ifelse((label == 2), a, b))), : object a not found
これはPythonとどのように関連していますか?また、「これらのどれも動作していないように思われる」とは、プログラミング問題の適切な記述ではありません。 – Goyo
これを行うには、データを前処理してダイナミックラベルを作成し、次に 'ggplot'を使用します。 –
再現可能なデータを含めると、手助けするのがかなり簡単になります。http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –