2017-02-06 4 views
2

Googleの美しいビジュアライゼーションを私の会社が週に何人雇ったかを示す自分のデータセットで、GoogleのRhythm of Foodに複製しようとしています。ggplot2を使って食べ物リズムのリズムを複製する

ggplot(hiresbyweek,aes(x=WeekNum, y=total.Hires,fill=as.factor(Year))) 
+geom_histogram(stat="identity", aes(x=WeekNum, y=total.Hires,fill=as.factor(Year))) 
+coord_polar() 
+scale_fill_manual(values=c("#ACD9F4","#005DA6","#EC008C")) 
+scale_x_discrete(labels = as.factor(hiresbyweek$Month)) 
+scale_y_discrete(expand=c(0.5,0)) 
+theme(text=element_text(family="Avenir") 
     , axis.ticks = element_blank() 
     , panel.grid = element_blank() 
     , panel.background = element_blank() 
     ) 

これが近いものを生成します。データセット(hiresbyweekという名前の)このようになります(これは81行、link to full dataset hereの25である)

  Week Year total.Hires  Month WeekNum 
    2014-05-05 0:00:00 2014   1  May  18 
    2014-05-12 0:00:00 2014   1  May  19 
    2014-05-19 0:00:00 2014   1  May  20 
    2014-05-26 0:00:00 2014   1  May  21 
    2014-08-04 0:00:00 2014   1 August  31 
    2014-09-08 0:00:00 2014   1 September  36 
    2015-02-23 0:00:00 2015   3 February  08 
    2015-03-23 0:00:00 2015   4  March  12 
    2015-05-04 0:00:00 2015   1  May  18 
    2015-06-01 0:00:00 2015   1  June  22 
    2015-06-08 0:00:00 2015   1  June  23 
    2015-09-14 0:00:00 2015   3 September  37 
    2015-09-21 0:00:00 2015   4 September  38 
    2015-09-28 0:00:00 2015   15 September  39 
    2015-10-05 0:00:00 2015   20 October  40 
    2015-10-12 0:00:00 2015   47 October  41 
    2015-10-19 0:00:00 2015   40 October  42 
    2015-10-26 0:00:00 2015   39 October  43 
    2015-11-02 0:00:00 2015   5 November  44 
    2015-11-09 0:00:00 2015   2 November  45 
    2015-11-16 0:00:00 2015   7 November  46 
    2015-11-23 0:00:00 2015   1 November  47 
    2015-11-30 0:00:00 2015   7 November  48 
    2015-12-07 0:00:00 2015   3 December  49 
    2015-12-14 0:00:00 2015   7 December  50 

は現在、私はそれは限り、このように作られてきました

enter image description here

本質的な問題は次のとおりです。

1)これらのラベルは、どこにあるべきかのどこにもありません。 最大の数字は10月ですが、チャートによると、ほとんどの場合、4月または3月になります。

持てる者へニース:

1)私はグループに好きで、それらのタイトルラに食品チャートのリズムを回転させるので、シンプルなラベル )私はしたいが存在することになるだろう前記バーの相対的なサイズを大きく減少させる。私はカウント(geom_historgram(stat = "count")またはstat = "bin")として実行しましたが、それらはすべて同じになり、スケールの重要性が取り除かれます。

3)バーの間に空白を挿入したいと思います。私はggplot(hiresbyweek、aes(x = WeekNum、y = total.Hires、color = "white"、fill = as.factor(Year))とgeom_histogram(stat)の両方でcolor = "white"両方とも奇妙にピンクの輪郭を持っています...

の助けを借りてください。最初の部分が最も重要です(私はそれが提示可能だったと思うでしょうが)。あなたの時間と思考に感謝します。

+0

が、私はこの提出を書いていた間、私は気づいた項目は それを動作させるために1行。それを複数の行にフォーマットすることは、それを日常的に破った。それはなぜ誰かが私に説明する気に? – ike

+1

'+'記号は行末にある必要があります – GGamba

+0

本当ですか?正面の代わりに?それは?面白い、私はそれがどのように正面に見えるか好きだった。だからそうなるのです。 – ike

答えて

3

私は他の誰かがよりよくハッキリしていない回答を投稿するのを待っていましたが、これがその間にできることを願っています。

# 1. We can control the order of geom_bars based on the levels of the factor of X. 
# So we make a new factor variable and ensure that the levels are in the order of 
# < January1, January2, ..., February2, ..., December3, December4 > 
hiresbyweek <- hiresbyweek[order(hiresbyweek$WeekNum),] 
hiresbyweek$X <- factor(paste0(hiresbyweek$WeekNum, hiresbyweek$Month), 
        levels = unique(paste0(hiresbyweek$WeekNum, hiresbyweek$Month))) 

# 2. But we don't want the axis labels to be: "Jan1, Jan2, Jan3, ..." 
# Instead we'll extract only the month out of the X variable (though notice the weekNum 
# variable was important so we could get the right order and distinct factor levels) 
# But we also don't want repeated axis labels: "Jan, "Jan", "Jan", "Feb", "Feb", .... 
# So try to place the unique axis label close to the middle, and leave the rest blank 
# (ie. "", "Jan", "", "", "Feb") 
makeLabels <- function(x) { 
    x <- gsub("[0-9]", "", x) 
    labs <- c(); 
    for (a in unique(x)) { 
    b <- rep("", length(x[x == a])) 
    b[ ceiling(length(x[x==a])/2) ] <- a 
    labs <- append(labs, b) 
    } 
    return(labs) 
} 

# 3. Angle the axis labels to imitate Google's Rhythm of Food 
ang <- -360/length(unique(hiresbyweek$X)) * seq_along(hiresbyweek$X) 
ang[ang <= -90 & ang >= -300] <- ang[ang <= -90 & ang >= -300] -180 

ggplot(hiresbyweek, aes(x = X, y = total.Hires,fill = as.factor(Year))) + 
    geom_histogram(stat="identity", width = 0.5) + # Use width arg for more space between bars 
    coord_polar() + 
    scale_x_discrete(labels = makeLabels) + # Apply getLabel function to X 
    scale_y_discrete(expand=c(0.5,0)) + 
    scale_fill_manual(values=c("#ACD9F4","#005DA6","#EC008C")) + 
    theme(axis.ticks = element_blank(), 
    panel.grid = element_blank(), 
    panel.background = element_blank(), 
    text = element_text(family="Avenir"), 
    title = element_blank(), # Remove all titles 
    axis.text.x = element_text(angle= ang)) # Apply angles to x-axis labels 

結果:私は実際には私のrstudioにすることを貼り付け:rstudioは、私は上記のコードの書式設定をクリーンアップする場合は、それを嫌っているようだ。また、result

+0

これはかなり華麗です、ありがとうございます。スペーシング部分も私が解決しようとしていた問題でしたので、それを見つけることに感謝します。 私は月ラベルでも間隔を広げることを考える略語として月をやった2番目のバージョンを作ったが、それは実際にはそれがもっとびっくりするように思えたので、私はそれを残した。私もそれをする方法があれば面白いだろう。いずれにせよ、これはすばらしい改善でした。ありがとうございました。 – ike

関連する問題