3

次のデータを使用して、素敵な (!) 極座標プロットを作成しようとしています。

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25, 30, 40)

df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
 rep(2, length(gr1)), rep(2, length(gr1))))



inner circle segment to mark
                   first tier, group 1, between 15, 20
                               group 3, between 5, 10
                                        between  40 to 60 
                   second tier, group 1, between 15, 20
                               group 3, between 5, 10
                                        between 10, 25
                                        between  40 to 60 

2 つの位置の間の間隔の間の 2 つの線の間の角度。

プロットする実際のデータには 2 つ以上の層があります。

マーカーは、セグメントまたは塗りつぶされた色などの他のマークのいずれかです。

可能であれば、異なるグループを色分けできます。

ここに画像の説明を入力

円セグメント マーキングの代替 (推奨)

ここに画像の説明を入力

ggplot2 で試してみました。

cx <- ggplot(df2, aes(x = pos, group = group) +   geom_bar(width = 1, colour = "black"))
Error in aes(x = pos, group = group) + geom_bar(width = 1, colour = "black") :
  non-numeric argument to binary operator
cx + coord_polar()

その他のトライアル:

df2$cpos <- cumsum (df2$pos)
cx <- ggplot(df2, aes(x = cpos, group = group) +   geom_bar(width = 1, colour = "black"))

編集 1:

問題を説明するのはひどいかもしれないと思います。ここに説明付きの図があります。 ここに画像の説明を入力

位置 (位置) は連続スケールであり、極座標の角度は位置の値に依存する必要があります。1 つのグループが完了すると、再開されます。私はこのトリックに cumsum するためのトリックを行いました (間違っている可能性があります)。

編集 2:

次の回答により、質問に対する考えがさらに広がりました。これは改善されたバージョンですが、カラー マネージメントが機能していません。

df2$cpos <- cumsum (df2$pos)
df2$y <- rep(3, length (df2$cpos))
cx <- ggplot(df2, aes(y = y, x = cpos))
cx + geom_bar(aes(stat = "identity",fill="yellow", colour = "yellow" )) + 
geom_point(aes(color = factor(group)), pch = 18, size = 2) + coord_polar() +
 scale_fill_manual(values=c("#CC6666", "#9999CC", "#66CC99"))+ ylim(0,3)

ここに画像の説明を入力

ylim (2,3) を設定しようとすると、同様に機能しないセグメントのように見えます。

Error in data$y[data$y == -Inf] <- range$y.range[1] : 
  replacement has length zero
4

1 に答える 1

5

あなたが何をしたいのか理解するのに苦労しています(バープロットは頻度を数えます、おそらくそれを知っています)。ただし、グループを色に割り当てる方法は次のとおりです。

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25, 30, 40)

df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
 rep(2, length(gr2)), rep(3, length(gr3))))
df2$cpos <- cumsum (df2$pos)

cx <- ggplot(df2, aes(fill = factor(group), x = cpos))

cx + geom_bar(width = 1, colour = "black", position = "dodge")  + coord_polar()

ここに画像の説明を入力

pos を周波数として取得する場合は、 のmelt()関数を使用しますreshape2

あなたの例のようにドットを使用したい場合、次のアプローチは機能しますか?

cx <- ggplot(df2, aes(y = group, x = cpos))

cx + geom_point(aes(color = factor(group))) + coord_polar() + ylim(0,3)

とにかく、パターンが見えますか?x軸を角度、y軸を中心からの距離として使用して通常の座標でプロットを作成し、それを極座標に変換するだけです。

ここに画像の説明を入力

Edit2への回答

もっと意味のあるプロットを作成できるかどうかはまだ疑問ですが、これを行う理由があるのか​​もしれません. これは、あなたの例に近いプロットです。完璧ではありません。明日オフィスに着いたら、教祖がもっと良い提案をしてくれるかもしれません。しばらくの間、このトレッドのリンクから詳細な仕様を探すことができます。

library(ggplot2)

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25, 30, 40)

df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
 rep(2, length(gr2)), rep(3, length(gr3))), y  = c(rep(1, length(gr1)),
 rep(2, length(gr1)), rep(2, length(gr1))))

df2$cpos <- cumsum (df2$pos)

cx <- ggplot(df2, aes(y = y, x = cpos))
cx + geom_point(aes(color = factor(group)), size = 4) + geom_line(aes(x = c(0,500), y = c(1)), color = "yellow") + 
geom_line(aes(x = c(0,500), y = c(2)), color = "blue") + scale_y_continuous(limits=c(0,2), breaks = c(0,1,2)) +
scale_x_continuous(labels = df2$pos, breaks = df2$cpos, limits = c(0,500)) + coord_polar() +
opts(panel.grid.major = theme_line(colour = "grey"), 
panel.grid.minor = theme_line(colour = "grey", linetype = "dashed"), 
panel.background = theme_rect(colour = "white"))

ここに画像の説明を入力

于 2012-05-06T11:52:08.700 に答える