1

目标是使用 ggplot() 绘制堆积条形图。首先,我需要融化数据表。但是,x8.p 中的零值在我将表融化到 x8.pm 后变成了 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 标签——这里的很多用户都喜欢回答 ggplots。

堆叠条示例

也就是说,我采取了几个步骤表明您可能一直在思考。我将其创建为一个数组,然后将其融合为一个二维矩阵,为您提供一些不错的图形选项:

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 回答