2016-02-21 84 views
1

我想查看以下數據:酒店發現每年有部分客戶是回頭客。所以,每年大約一半的顧客是第一時間的顧客,20%是第二時間的顧客,等等。以下是一些包含數據和可視化的R代碼。然而,我並不滿意,我正在尋求改進:尋找更好的方式來顯示R和ggplot2中的分佈

  • R不喜歡顏色帶有很多顏色 - 所以也許組數據?
  • 步驟曲線會是一個更好的可視化嗎?
  • 訪問次數被視爲一個因素 - 這是正確的方法嗎?

  • 堆疊酒吧可以很容易地比較第一次客人,而不是其他人。我應該選擇一個不同的可視化?

    #! /usr/bin/env R CMD BATCH 
    
    library(ggplot2) 
    
    d <- read.table(header=TRUE, text=' 
        year visit count 
        2013 1 1641 
        2013 2 604 
        2013 3 256 
        2013 4 89 
        2013 5 32 
        2013 6 10 
        2013 7 4 
        2013 8 3 
        2014 1 1365 
        2014 2 637 
        2014 3 276 
        2014 4 154 
        2014 5 86 
        2014 6 39 
        2014 7 19 
        2014 8 6 
        2014 9 4 
        2014 10 2 
        2014 11 1 
        2014 12 1 
        2015 1 1251 
        2015 2 608 
        2015 3 288 
        2015 4 143 
        2015 5 88 
        2015 6 52 
        2015 7 21 
        2015 8 8 
        2015 9 8 
        2015 10 3 
        2015 11 2 
        2015 12 1') 
    
    d$year <- factor(d$year) 
    d$visit <- factor(d$visit) 
    
    p <- ggplot(d, aes(year,count)) 
    p <- p + geom_bar(aes(fill=visit),position="fill",stat="identity") 
    p <- p + xlab("Year") + ylab("Distribution") 
    # pdf("returners.pdf",9,6) 
    print(p) 
    # dev.off() 
    

enter image description here

+1

爲什麼不減少你的數據做更有意義的類別? (1,2,3,4和更多?) – Heroka

+0

我同意。 5次以上的數字不會增加太多。 –

回答

3

爲什麼不想像他們一樣實際分佈?

p <- ggplot(d, aes(visit, count)) 
p <- p + geom_bar(stat="identity", width=0.75) 
p <- p + scale_x_discrete(expand=c(0,0)) 
p <- p + scale_y_continuous(expand=c(0,0)) 
p <- p + facet_wrap(~year) 
p <- p + labs(x=NULL, y="Visits") 
p <- p + ggthemes::theme_tufte(base_family="Helvetica") 
p <- p + theme(legend.position="none") 
p <- p + theme(panel.grid=element_line(color="#2b2b2b", size=0.15)) 
p <- p + theme(panel.grid.minor=element_blank()) 
p <- p + theme(panel.grid.major.x=element_blank()) 
p <- p + theme(axis.ticks=element_blank()) 
p <- p + theme(strip.text=element_text(hjust=0)) 
p <- p + theme(panel.margin.x=unit(1, "cm")) 
p 

enter image description here

要查看一年訪問計數的增量,你可以交換方面:

d$year <- factor(d$year) 
d$visit <- sprintf("Visit: %d", d$visit) 
d$visit <- factor(d$visit, levels=unique(d$visit)) 

p <- ggplot(d, aes(year, count)) 
p <- p + geom_segment(aes(xend=year, yend=0), size=0.3) 
p <- p + geom_point() 
p <- p + scale_x_discrete(expand=c(0, 0.25)) 
p <- p + scale_y_continuous(label=scales::comma) 
p <- p + facet_wrap(~visit, scales="free_y") 
p <- p + labs(x="NOTE: Free y-axis scale", y="Count") 
p <- p + ggthemes::theme_tufte(base_family="Helvetica") 
p <- p + theme(legend.position="none") 
p <- p + theme(panel.grid=element_line(color="#2b2b2b", size=0.15)) 
p <- p + theme(panel.grid.minor=element_blank()) 
p <- p + theme(panel.grid.major.x=element_blank()) 
p <- p + theme(axis.ticks=element_blank()) 
p <- p + theme(strip.text=element_text(hjust=0)) 
p <- p + theme(panel.margin=unit(1.5, "cm")) 
p 

enter image description here

或者,你可以看看同比增幅由訪問(%):

library(dplyr) 

group_by(d, visit) %>% 
    arrange(year) %>% 
    mutate(lag=lag(count), 
     chg_pct=(count-lag)/lag, 
     chg_pct=ifelse(is.na(chg_pct), 0, chg_pct), 
     pos=as.character(sign(chg_pct))) -> d 

p <- ggplot(d, aes(year, chg_pct)) 
p <- p + geom_hline(yintercept=0, color="#2b2b2b", size=0.5) 
p <- p + geom_segment(aes(xend=year, yend=0, color=pos), size=0.3) 
p <- p + geom_point(aes(color=pos)) 
p <- p + scale_x_discrete(expand=c(0, 0.25)) 
p <- p + scale_y_continuous(label=scales::percent) 
p <- p + scale_color_manual(values=c("#b2182b", "#878787", "#7fbc41")) 
p <- p + facet_wrap(~visit, scales="free_y") 
p <- p + labs(x="NOTE: free y-axis", y="YoY % Difference per visit count") 
p <- p + ggthemes::theme_tufte(base_family="Helvetica") 
p <- p + theme(legend.position="none") 
p <- p + theme(panel.grid=element_line(color="#2b2b2b", size=0.15)) 
p <- p + theme(panel.grid.minor=element_blank()) 
p <- p + theme(panel.grid.major.x=element_blank()) 
p <- p + theme(axis.ticks=element_blank()) 
p <- p + theme(strip.text=element_text(hjust=0)) 
p <- p + theme(panel.margin=unit(1.5, "cm")) 
p <- p + theme(legend.position="none") 
p 

enter image description here

+0

當然,它總是取決於你想展示的東西。在這個圖表中,你不能說出回頭客的數量(和份額)是如何隨時間變化的。這是一個更好的圖表,它顯示了更多細節。我還喜歡你投入的許多小小的動作。 –

+1

增加的觀點表明,你開始難以吸引新的客人,但4-6年前在那裏的人喜歡回來,但其他人已經找到新的地方去:-) – hrbrmstr

+0

哇。感謝您獲得更多圖表 - 您看起來很簡單。 –

1

看來你正試圖以參觀酒店總數的捐款之前訪問次數以及做了一年,與年的比較比較。以下代碼將這些放在一個圖表中。

d$year <- factor(d$year) 
# d$visit <- factor(d$visit) 
d <- transform(d[order(d$year, d$visit),], cum_count=ave(count, year, FUN=cumsum)) 

x_max <- max(d$visit) 
y_max <- max(d$cum_count) 
color_sch <- c("red","tan","blue") 

p <- ggplot(data=d, aes(x=visit, colour=year)) 
p <- p + geom_bar(aes(y= count, fill=year), position="dodge",stat="identity", width=.7) 
p <- p + geom_line(aes(y = cum_count), linetype="dotted", size=1) 
p <- p + geom_point(aes(y = cum_count), size=4) 
p <- p + scale_y_continuous(breaks = seq(0,y_max, 250)) 
p <- p + scale_x_continuous(breaks=1:x_max) 
p <- p + scale_colour_manual(values=color_sch) 
p <- p + scale_fill_manual(values=color_sch) 
p <- p + xlab("Visit") + ylab("Count and \nCummulative Count") 
p <- p + geom_text(aes(x = 2, y = count[2], label = "Count by Number of Visits"), hjust=-.5, vjust=-2.0, size=6, color="Black") 
p <- p + geom_text(aes(x = x_max-5, y = tail(cum_count,1), label = "Cummulative Count"), hjust=0, vjust=2.0, size=6, color="Black") 
# pdf("returners.pdf",9,6) 
print(p) 
# dev.off() 

這給圖表

enter image description here

這種表示意味着相比於前幾年在2015年的下降是由於較少的第一次顧客爲在返回那些相對的降低。

相關問題