2017-06-30 16 views
1

グループ変数Asとサブグループ変数ADsを使用して周波数データをプロットする必要があります。頻度、つまり円グラフやモザイクを視覚化する最良の方法は何ですか? ggplot2で利用できる関数はありますか?グループとサブグループの頻度を視覚化するには?

df <- data.frame(As=c('GeA','GeA','GeA', 'GA'), 
      ADs=c('A44','A33','A37','A141'), 
      freq=c(501,65,50,103)) 

# As ADs freq 
# 1 GeA A44 501 
# 2 GeA A33 65 
# 3 GeA A37 50 
# 4 GA A141 103 

いくつかの考えは以下のようなものです:

image1

どのようにこれまで、単一のプロットのグループとサブグループの両方を区別する方法はありますか?

提案された解決策のうち、two chartsが有望であると考えられました。

円グラフ私はマソウドによって提案された次のコードを使用している

piecharttileGraph

&タイルグラフ。

df.2 <- df 
df.2$ymax <- with(df.2, ave(freq, As, FUN=cumsum)) 
df.2$ymin <- lag(df.2$ymax, default = 0) 
df.2$ymin <- ifelse(lag(as.character(df.2$As), default = 0) != df.2$As, 0, df.2$ymin) 

df.legend <- df.2[with(df.2, order(As)), ] 

library(ggplot2) 
# Pie Chart 
ggplot(df.2) + 
    geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) + 
    geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) + 
    xlim(c(0, 4)) + 
    theme(aspect.ratio=1) + 
    coord_polar(theta="y") + 
    scale_x_continuous(breaks=c(0,3), labels=c("ADs", "As")) + 
    annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
      label= as.character(df.legend$ADs)) + 
    annotate("text", x=rep(3.5,2), y=c(50, 350), 
      label= as.character(unique(df.legend$As))) + 
    theme(legend.position="none", axis.title.x=element_blank(), 
     axis.title.y=element_blank()) 

# Tile Graph 
ggplot(df.2) + 
    geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) + 
    geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) + 
    xlim(c(0, 4)) + theme(aspect.ratio=1) + 
    scale_x_continuous(breaks=c(1.5,3.5), labels=c("ADs", "As")) + 
    annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
      label= paste(as.character(df.legend$ADs), df.legend$freq,sep= " = ")) + 
    annotate("text", x=rep(3.5,2), y=c(50, 350), 
      label= as.character(unique(df.legend$As))) + 
    theme(legend.position="none", axis.title.x=element_blank(), 
     axis.title.y=element_blank()) 

しかし、私は

円グラフ&タイルグラフ

piechart_errtileGraph_err

メッセージと同じ出力を取得できませんでした: 'X' のためのスケールはすでに存在しています。既存の縮尺を置き換える 'x'の別の縮尺を追加する。

問題の原因を教えてください。使用されているパッケージのバージョンに違いはありますか?

+0

積み上げ棒プロット – Masoud

+0

私が積み重ねられたバープロットを使用して、グループおよびサブグループの両方を視覚化する方法についての鮮明な画像を取得しておりません。あなたは大まかに引き分けて共有できますか? – Prradep

答えて

2

積み上げBarplot:

library(ggplot2) 
ggplot(data = df, aes(x = As, y = freq, fill = ADs)) + 
     geom_bar(stat = "identity") 

これを追加して、プロットのラベルを取得することができます::

p + geom_text(aes(label = paste(ADs, freq, sep=": ")), 
     position = position_stack(vjust = 0.5), size = 3) + #subgroups 
     stat_summary(fun.y = sum, aes(label = ..y.., group = As), geom = "text") + #groups 
     theme(legend.position="none") 

enter image description here

次の2つの答え

あなたが積み重ねられbarplotsを使用することができますこれについてはpostを参照してください。

タイルグラフ:我々はデータを微調整する必要がある。このため

df.2 <- df 
    df.2$ymax <- with(df.2, ave(freq, As, FUN=cumsum)) 
    df.2 <- df.2[with(df.2, order(As)), ] 

    #for some reason lag function does not work properly in R 3.3.3 
    library(data.table) 
    setDT(df.2)[, ymin:=c(0,ymax[-.N])] 


    df.legend <- df.2[with(df.2, order(As)), ] 

はその後、我々は再びggplotを使用することができます。

ggplot(df.2) + 
    geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) + 
    geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) + 
    xlim(c(0, 4)) + theme(aspect.ratio=1) + 
    scale_x_continuous(breaks=c(1.5,3.5), labels=c("ADs", "As")) + 
    annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
      label= paste(as.character(df.legend$ADs), df.legend$freq,sep= " = ")) + 
    annotate("text", x=rep(3.5,2), y=c(50, 350), 
     label= as.character(unique(df.legend$As))) + 
     theme(legend.position="none", axis.title.x=element_blank(), 
    axis.title.y=element_blank()) 

enter image description here

円グラフ:

ggplot(df.2) + 
geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) + 
geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) + 
xlim(c(0, 4)) + 
theme(aspect.ratio=1) + 
coord_polar(theta="y") + 
scale_x_continuous(breaks=c(0,3), labels=c("ADs", "As")) + 
annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
     label= as.character(df.legend$ADs)) + 
annotate("text", x=rep(3.5,2), y=c(50, 350), 
     label= as.character(unique(df.legend$As))) + 
theme(legend.position="none", axis.title.x=element_blank(), 
    axis.title.y=element_blank()) 

enter image description here

+0

プロットをありがとう。各サブグループのシェアを確認するといいでしょう。 – Prradep

+0

使用しているggplot2のパッケージ版を投稿できますか?何らかの理由で、私は最後の2つのプロットの色を取得していません。 – Prradep

+0

@Prradep質問を編集します。取得した結果を追加して、再度開くことができます。セッション情報とパッケージバージョンをできるだけ早く追加します。 – Masoud

関連する問題