1

目標は、ggplot() を使用して積み上げ棒グラフをプロットすることです。まず、データ テーブルを溶かす必要があります。ただし、テーブルを x8.pm に溶かした後、x8.p のゼロ値が NULL (x8.pm$Var.1) になり、データの一部であるゼロが失われました。

x8.p <-structure(c(70, 20, 7, 2, 2, 54, 33, 8, 0, 4, 37, 29, 23, 11, 
0, 46, 29, 18, 4, 2, 16, 29, 26, 20, 10, 37, 20, 9, 13, 22, 66, 
18, 14, 2, 0, 65, 20, 12, 3, 0, 47, 29, 18, 4, 3, 48, 32, 16, 
3, 1, 24, 25, 19, 22, 10, 41, 14, 18, 9, 18, 66, 25, 6, 3, 0, 
62, 23, 14, 1, 0, 47, 30, 14, 8, 1, 46, 31, 16, 6, 1, 19, 27, 
25, 22, 7, 34, 15, 17, 13, 21, 68, 22, 7, 2, 1, 62, 21, 16, 1, 
0, 45, 30, 18, 5, 1, 43, 28, 22, 5, 2, 20, 23, 25, 23, 10, 36, 
20, 12, 14, 19, 60, 25, 13, 2, 0, 62, 23, 10, 6, 0, 41, 24, 26, 
9, 0, 47, 31, 16, 5, 1, 13, 29, 22, 24, 13, 28, 16, 20, 18, 18
), class = "table", .Dim = c(5L, 6L, 5L), .Dimnames = structure(list(
    c("Highly Satisfied", "Moderately Satisfied", "Satisfied", 
    "Moderately Dis-Satisfied", "Extremely Dis-Satistfied"), 
    c("I've changed for work/ a new job/ gone on a work plan", 
    "I want a phone that 2degrees doesn't offer", "I want Best Mates/ Favourites", 
    "I was offered or saw a better offer on another network", 
    "Issues with the 2degrees network (poor coverage)", "Other"
    ), YearQuarter = c("2011-09-01", "2011-12-01", "2012-03-01", 
    "2012-06-01", "2012-09-01")), .Names = c("", "", "YearQuarter"
)))

x8.p.m <- melt(x8.p, id="var");x8.p.m

ggplot(data=x8.p.m, aes(x=as.factor(YearQuarter), y=value, fill=Var.1,na.rm=TRUE))+ geom_bar(stat="identity")+facet_wrap(~Var.2)
4

2 に答える 2

1

あなたの質問が見過ごされたようです。いくつかの推奨事項があります。

1) 持っているデータをうまく表示できましたが、完成したデータの構造がどのようになるかをより明確にすることを検討してください。2) 投稿に ggplot2 タグを追加することをお勧めします。ここの多くのユーザーは ggplot への回答が大好きです。

積み上げ棒グラフの例

そうは言っても、私はあなたが考えているかもしれないことを示すいくつかのステップを踏みました. これを配列として作成し、それを 2D マトリックスに溶かして、いくつかの優れたグラフ オプションを提供します。

library(reshape2)
library(ggplot2)
x8.New <-c(70, 20, 7, 2, 2, 54, 33, 8, 0, 4, 37, 29, 23, 11, 
                   0, 46, 29, 18, 4, 2, 16, 29, 26, 20, 10, 37, 20, 9, 13, 22, 66, 
                   18, 14, 2, 0, 65, 20, 12, 3, 0, 47, 29, 18, 4, 3, 48, 32, 16, 
                   3, 1, 24, 25, 19, 22, 10, 41, 14, 18, 9, 18, 66, 25, 6, 3, 0, 
                   62, 23, 14, 1, 0, 47, 30, 14, 8, 1, 46, 31, 16, 6, 1, 19, 27, 
                   25, 22, 7, 34, 15, 17, 13, 21, 68, 22, 7, 2, 1, 62, 21, 16, 1, 
                   0, 45, 30, 18, 5, 1, 43, 28, 22, 5, 2, 20, 23, 25, 23, 10, 36, 
                   20, 12, 14, 19, 60, 25, 13, 2, 0, 62, 23, 10, 6, 0, 41, 24, 26, 
                   9, 0, 47, 31, 16, 5, 1, 13, 29, 22, 24, 13, 28, 16, 20, 18, 18)

length(x8.New)

# names and structures your values
Exit.Survey <- array(x8.New, c(5, 6, 5), dimnames = list(
  Rating = c("Highly Satisfied", "Moderately Satisfied", "Satisfied", "Moderately Dis-Satisfied", "Extremely Dis-Satistfied"), 
  Churn.Reason = c("work change", "diff phone", "Best Mates/ Favourites", "better offer", "poor coverage", "Other"), 
  YearQuarter = c("2011-09-01", "2011-12-01", "2012-03-01", "2012-06-01", "2012-09-01") 
  ))

#please note - 3d array- just like a cube of data - 3 matrices (including 0's!)
dim(Exit.Survey)
Exit.Survey[,,1]

#melts the 3d array into a 2d structure by adding a YearQuarter variable
df1<- melt(Exit.Survey, id=names(Exit.Survey), measured="YearQuarter")
df1$Rating<- factor(c("Highly Satisfied", "Moderately Satisfied", "Satisfied", "Moderately Dis-Satisfied", "Extremely Dis-Satistfied"))
ggplot(data=df1, aes(x=factor(Rating), y=value, fill=Churn.Reason)) +
  geom_bar(colour="black", stat="identity") +
  facet_grid(. ~ YearQuarter) + ylab(c("# of Churned Customers")) + 
  xlab(c("Satisfaction Level")) + scale_fill_brewer(palette="Set1") + 
  theme(axis.text.x  = element_text(angle=90, vjust=0.5)) + 
  scale_x_discrete(limits= c("Highly Satisfied", "Moderately Satisfied", "Satisfied", 
                     "Moderately Dis-Satisfied", "Extremely Dis-Satistfied"))
于 2013-03-13T12:58:22.243 に答える
1

私はあなたの問題を再現することはできませんが、いずれにせよ、これはおそらく何よりもハックですが、 is.null 関数を使用するだけです。

于 2012-11-12T22:05:44.847 に答える