2016-04-06 43 views
1

時間あたりのフリークエンシーを表示するggplotを作成しようとしています。私はx軸に問題があり、元の日付形式を2015-01-02 02:07:27から2015-01-02に変更しました。format(dates, format = "%Y-%m-%d %H")を使用しました。私のggplotではエースaes(x=as.Date(dates)..を使用しましたが、x軸には2015-01-02のフォーマットがあります。フォーマットは"%Y-%m-%d %H"としてx軸に日付を表​​示することは可能ですか?
ありがとうございました!ggplot x軸と時間(時)

+0

あなたは 'AES()'であなたの以前の形式を上書きしていないここで結果のグラフは次のようになります。 ? – mtoto

+1

Rでは、「日付」は_only_で、日、月、および年です。ですから、一旦 'as.Date()'を使うと、それはあなたが持つすべてのものです。時間が必要な場合、必要なタイプは_datetime_、おそらくPOSIXctです。 – joran

+0

scalesパッケージには、あなたが探しているものを実行するdate_format関数があります。これをggplotのscale_x_datetime関数と組み合わせて使用​​します。良い例があります:http://stackoverflow.com/questions/11748384/formatting-dates-on-x-axis-in-ggplot2 –

答えて

3

私はちょうど私のコメントと一緒に行く例を提供したいと思った。 パッケージのdate_format()関数を使用することができます。

require(ggplot2) 
require(scales) 

#Create a test sequence of dates 
test_dates = seq(from = as.POSIXct("2015-01-02 02:07:27", format="%Y-%m-%d %H:%M:%S"), 
       to = as.POSIXct("2015-01-04 02:00:00", format="%Y-%m-%d %H:%M:%S"), 
       by = "hour") 
#Set seed for random variable 
set.seed(1) 
#Create the test data 
time_data = 
    data.frame(dates = test_dates, 
       measurements = runif(n = length(test_dates), 
            min = 0, max = 1)) 
#Plot the data 
ggplot(time_data, aes(x = dates, y = measurements)) + 
    geom_line() + 
    #Here is where I format the x-axis 
    scale_x_datetime(labels = date_format("%Y-%m-%d %H"), 
        date_breaks = "8 hours") 

このようにすることの利点は、元のデータを変更/再フォーマットする必要がないことです。 enter image description here

UPDATE:ここではOPのコメントからテストデータを使用して別の例です:

require(ggplot2) 
require(scales) 

#Create the test data 
example_data <- 
    data.frame(a = as.POSIXct(c("2015-01-02 06:07:27", "2015-01-02 06:42:36", "2015-01-02 08:07:38", "2015-01-02 08:08:45", "2015-01-02 08:12:23", "2015-01-03 09:07:27", "2015-01-03 09:42:36")), 
       b = c("1","1","1","1","1","1","1")) 

#Pull out date and hour components 
example_data$days <- as.POSIXct(format(example_data$a, "%Y-%m-%d")) 
#This doesn't work because format just returns a character string, not a dateTime 
example_data$hours <- format(example_data$a, "%Y-%m-%d %H") 
#Instead, you need to re-cast the output of format as a dateTime 
example_data$hours <- as.POSIXct(format(example_data$a, "%Y-%m-%d %H"), format="%Y-%m-%d %H") 

#Plot the data 
ggplot(data = example_data, aes(x=days)) + geom_bar(stat="bin") 
ggplot(data = example_data, aes(x=hours)) + geom_bar(stat="bin") 

#Now use axis-scaling and date_format to get just the data and hours 
ggplot(data = example_data, aes(x=hours)) + 
    geom_bar(stat="bin") + 
    scale_x_datetime(labels = date_format("%Y-%m-%d %H")) 
+0

ありがとうございました。これは私の小さなデータです: 'a < - as.POSIXct(c(" 2015-01-02 06:07:27 "、" 2015-01-02 06:42:36 "、" 2015-01-02 08:07:38 "、" 2015-01-02 08:08:45 "、" 2015-01-02 08:12:23 "、" 2015-01-03 09:07:27 "、" 2015-01 -03 09:42:36 ")) b < - c(" 1 "、" 1 "、" 1 "、" 1 "、" 1 ") example_data < - data_frame a、b) example_data $ days < - as.POSIXct(形式(example_data $ a、 "%Y-%m-%d")) example_data $ hours < - format(example_data $ a、 "%Y-%m (%=%d%H)) –

+0

残念ですが、これは私のプロットです: 'ggplot(data = example_data、aes(x =日))+ geom_bar(stat =" bin ") ggplot(data = example_data、aes (x =時間))+ geom_bar(stat = "bin") '私は時間に問題があります。 –

+0

@ A.Trzcionkowska上記のテストデータを使用した別の例を追加しました。将来的には、dateTimesを使って作業するときに、* lubridate *パッケージを調べたいかもしれません。あなたの人生をはるかに簡単にすることができます。 –

関連する問題