2017-05-18 15 views
1

解決策以上のことは、なぜ簡単にすべきなのかを理解したいのですが、それは実際にはありません。ggplot2:yearmonスケールとgeom_bar

[私が問題に触れ異なるポストからのコードの一部を借りていますが、それは私が好きではなかった解決策になってしまった]

library(ggplot2) 
library(xts) 
library(dplyr) 
library(scales) 

csvData <- "dt,status 
2015-12-03,1 
2015-12-05,1 
2015-12-05,0 
2015-11-24,1 
2015-10-17,0 
2015-12-18,0 
2016-06-30,0 
2016-05-21,1 
2016-03-31,0 
2015-12-31,0" 

tmp <- read.csv(textConnection(csvData)) 
tmp$dt <- as.Date(tmp$dt) 
tmp$yearmon <- as.yearmon(tmp$dt) 
tmp$status <- as.factor(tmp$status) 

### Not good. Why? 
ggplot(tmp, aes(x = yearmon, fill = status)) + 
    geom_bar() + 
    scale_x_yearmon() 

### Almost good but long-winded and ticks not great 
chartData <- tmp %>% 
    group_by(yearmon, status) %>% 
    summarise(count = n()) %>% 
    as.data.frame() 
ggplot(chartData, aes(x = yearmon, y = count, fill = status)) + 
    geom_col() + 
    scale_x_yearmon() 

最初のプロットは、すべて間違っています。 2番目はほぼ完璧です(X軸上のダニは大きくありませんが、私はそれで生きることができます)。 geom_bar()はカウントジョブを実行するはずです。私は手動で2番目のチャートで実行する必要がありますか?

最初のグラフ poor plot

SECOND CHART better plot

私の質問は:なぜ最初のグラフはとても悪いですか?何かを示唆するための警告があります(「position_stackは重複しないx間隔を必要とします」)が、実際には理解できません。おかげさまで

私の個人的なANSWER

これは私が学んだことである(あなたのすべてのおかげでそんなに!):

  • それらを扱うscale_#_yearmonまたはscale_#_date、残念ながらggplotがあっても連続した数字としてのオブジェクトタイプ。これにより、geom_barは使用できなくなります。
  • geom_histogramトリックを行う可能性があります。しかし、あなたは麻酔の関連部分をコントロールできなくなります。
  • 一番下の行:あなたは
  • をグラフ前にXTSまたはlubridate(あなたがggplot2を使用する場合)を確認してくださいグループ/合計する必要はありません、私が達成しようとしていた何のために本当に便利です。私はどんな連続的な事件でも - 日付的に - 彼らが完璧だと思う。

    library(ggplot2) 
    library(dplyr) 
    library(scales) 
    
    csvData <- "dt,status 
    2015-12-03,1 
    2015-12-05,1 
    2015-12-05,0 
    2015-11-24,1 
    2015-10-17,0 
    2015-12-18,0 
    2016-06-30,0 
    2016-05-21,1 
    2016-03-31,0 
    2015-12-31,0" 
    
    tmp <- read.csv(textConnection(csvData)) 
    tmp$dt <- as.Date(tmp$dt) 
    tmp$yearmon <- as.Date(format(tmp$dt, "%Y-%m-01")) 
    tmp$status <- as.factor(tmp$status) 
    
    ### GOOD 
    chartData <- tmp %>% 
        group_by(yearmon, status) %>% 
        summarise(count = n()) %>% 
        as.data.frame() 
    
    ggplot(chartData, aes(x = yearmon, y = count, fill = status)) + 
        geom_col() + 
        scale_x_date(labels = date_format("%h-%y"), 
           breaks = seq(from = min(chartData$yearmon), 
              to = max(chartData$yearmon), by = "month")) 
    

    最終出力 final plot

はすべてで、私は私が後だ(XTSまたはlubridateの必要性がないか注意してください)完全にんいるこれで終了しました

+0

私は最初のプロットと2番目のプロットの間に違いは見られません。プロットの画像を投稿し、最初のプロットについて何が間違っていたかを指摘できますか? –

+0

要求が完了しました。それはプラットフォーム/パッケージのバージョンで何らかの問題になることがありますか?私はWIN10にいる。 Rバージョン3.4.0(2017-04-21); ggplot2 2.2.1 –

+0

代わりに、ggplot(tmp、aes(x = floor_date(dt、 "month")、fill = status))+ geom_bar()+ scale_x_date(labels = date_format "))、これはより良いx軸を与える。 –

答えて

1

最初のプロットがねじ込まれている理由は、基本的にはggplot2ではありません。正確にはyearmonはです。ここにあるように、ラベルは内部でnumです。

> as.numeric(tmp$yearmon) 
[1] 2015.917 2015.917 2015.917 2015.833 2015.750 2015.917 2016.417 2016.333 2016.167 2015.917 

したがって、前の集計なしでプロットすると、バーが広がります。あなたは、このようなgeom_histogram()を使用して、適切なbinwidthを割り当てる必要があります:

ggplot(tmp, aes(x = yearmon, fill = status)) + 
    geom_histogram(binwidth = 1/12) + 
    scale_x_yearmon() 

1/12は、各年の12ヶ月間に対応します。

@ed_sansのように、集計後のプロットでは、ティックを変更して軸ラベルを変更する方法をよく知っているので、lubridateを使用することをお勧めします。

chartData <- tmp %>% 
    mutate(ym = floor_date(dt,"month")) %>% 
    group_by(ym, status) %>% 
    summarise(count = n()) %>% 
    as.data.frame() 

ggplot(chartData, aes(x = ym, y = count, fill = status)) + 
    geom_col() + 
    scale_x_date(labels = date_format("%Y-%m"), 
       breaks = as.Date("2015-09-01") + 
       months(seq(0, 10, by = 2))) 
関連する問題