時間あたりのフリークエンシーを表示する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軸と時間(時)
答えて
私はちょうど私のコメントと一緒に行く例を提供したいと思った。 パッケージの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")
このようにすることの利点は、元のデータを変更/再フォーマットする必要がないことです。
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"))
ありがとうございました。これは私の小さなデータです: '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)) –
残念ですが、これは私のプロットです: 'ggplot(data = example_data、aes(x =日))+ geom_bar(stat =" bin ") ggplot(data = example_data、aes (x =時間))+ geom_bar(stat = "bin") '私は時間に問題があります。 –
@ A.Trzcionkowska上記のテストデータを使用した別の例を追加しました。将来的には、dateTimesを使って作業するときに、* lubridate *パッケージを調べたいかもしれません。あなたの人生をはるかに簡単にすることができます。 –
- 1. d3時間スケールx軸とunixタイムスタンプ
- 2. ggplot X軸トラブル
- 3. 24時間日付時間グラフは、Excelでx軸上にプロット
- 4. x軸とy軸の間隔を同時に調整する
- 5. ハイチャート24時間をx軸に表示
- 6. x軸とy軸上に時間のあるプロットを作成する(R)
- 7. 時間中のx軸matplotlibの機能に以下と時間で
- 8. 時間軸python
- 9. ggplotの時間軸上のラベルをナッジします
- 10. 24時間X軸これと同様に
- 11. Matplotlib:時間軸と限界
- 12. X軸ラベルの順GGPLOT
- 13. ggplot2で線グラフをプロットする方法x軸上の時間間隔とy軸上の値
- 14. Flotr x軸日付/時刻
- 15. ggplotとx軸の文字列
- 16. 時間をx軸として使用するGnuplot
- 17. gnuplot複数の行とX軸の時間
- 18. x軸を時間軸とするRの3次元散布図
- 19. ggplot2 1時間の誤差での時間軸
- 20. Reporting Services RangeBar Chart X軸ラベルは日時値の時間部分のみです
- 21. 時間xと時間yの間のSqlite3
- 22. イベントの単純な時間軸をプロット
- 23. ggplotのx軸要素間のスペースを増やす
- 24. Flotチャートのx軸時間の問題... AARGHHH
- 25. C3.js、y軸時間
- 26. seaborn/matplotlibのx軸を時間軸で正しく配置する方法は?
- 27. Xの時間が
- 28. 並べ替えggplot barplot x軸facet_wrap
- 29. ggplot:凡例レベル、右のX軸
- 30. ggplot faceting - 空のx軸ラベルを削除
あなたは 'AES()'であなたの以前の形式を上書きしていないここで結果のグラフは次のようになります。 ? – mtoto
Rでは、「日付」は_only_で、日、月、および年です。ですから、一旦 'as.Date()'を使うと、それはあなたが持つすべてのものです。時間が必要な場合、必要なタイプは_datetime_、おそらくPOSIXctです。 – joran
scalesパッケージには、あなたが探しているものを実行するdate_format関数があります。これをggplotのscale_x_datetime関数と組み合わせて使用します。良い例があります:http://stackoverflow.com/questions/11748384/formatting-dates-on-x-axis-in-ggplot2 –