ggplot2で、いくつかの変数のさまざまな線スタイルと他の変数のさまざまなマーカーを組み合わせた折れ線グラフを作成しようとしています。
例1は、各変数を異なる線スタイルでグラフ化し、例2はそれぞれ異なるマーカーでグラフ化し、例3はそれぞれ異なる線とマーカーでグラフ化します。
X2とX3を異なる線のスタイル(実線、破線)でグラフ化し、次にX4とX5を異なるマーカー(円、正方形など)で実線としてグラフ化しようとしています。
これを行う方法はありますか?
library(ggplot2)
library(reshape2)
set.seed <- 1
df <- data.frame(cbind(seq(1,10,1),matrix(rnorm(100,1,20), 10, 4)))
d <- melt(df, id="X1")
# Example 1: different line styles
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line(aes(linetype=variable), size=1)
# Example 2: different markers for each line
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line() + geom_point(aes(shape=variable, size=4))
# Example 3: differnt line styles & different markers (You see this graph below)
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line(aes(linetype=variable), size=1) +
geom_point(aes(shape=variable, size=4))
これが1つのアプローチです。さらに2つの関数を使用して、形状と線種を制御できます。scale_linetype_manual
線種を手動で割り当てることができます。同様に、scale_shape_manual
必要な形状を手動で割り当てることができます。
# Example 3: differnt line styles & different markers
ggplot(d, aes(x=X1, y=value, color=variable)) +
geom_line(aes(linetype=variable), size=1) +
geom_point(aes(shape=variable, size=4)) +
scale_linetype_manual(values = c(1,2,1,1)) +
scale_shape_manual(values=c(0,1,2,3))
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加