2016-04-06 21 views
1

Saat başına sıklık gösteren ggplot yapmayı denerim. X ekseni ile ilgili bir sorunum var, orijinal tarih formatını değiştirdim, 2015-01-02 02:07:27 - 2015-01-02 02 arası, format(dates, format = "%Y-%m-%d %H") kullanıyorum. Benim ggplot'umda aes(x=as.Date(dates)..'u kullandım ama x ekseninde 2015-01-02 formatına sahibim. X ekseni üzerinde tarih "%Y-%m-%d %H" biçiminde göstermek mümkün mü?
Yardımlarınız için teşekkürler!ggplot x-ekseni saat olarak tarih olarak

+0

Eğer' aes içinde önceki biçimi üzerine yazma değildir: Burada OP'ın yorumlardan test verilerini kullanarak başka örnek: enter image description here

GÜNCELLEME: Burada gibi Ortaya çıkan grafik nasıl göründüğü ve ? – mtoto

+1

R, bir "Tarih" _only_ bir gün, ay ve yıl. Yani “as.Date()' yi kullandığınızda, sahip olduğunuz tek şey budur. Eğer saat isterseniz, ihtiyacınız olan tip _datetime_, muhtemelen POSIXct. – joran

+0

Terazi paketi, aradığınız şeyi yapması gereken bir date_format işlevine sahiptir. Bunu ggplot'un scale_x_datetime işleviyle birlikte kullanın. İşte iyi bir örnek: http://stackoverflow.com/questions/11748384/formatting-dates-on-x-axis-in-ggplot2 –

cevap

3

Sadece yorum yazmam için bir örnek verebileceğimi düşündüm. date_format() işlevini ölçekleri paketinden kullanabilirsiniz.

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") 

Bu şekilde yapmanın avantajı, orijinal verilerinizi değiştirmenize/yeniden biçimlendirmenize gerek yoktur. `) (

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

Yardımlarınız için çok teşekkür ederim. Bu benim küçük veri eksikliğim: '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 ") example_data <- data_frame (a, b) example_data $ gün <- as.POSIXct (biçim (örnek_data $ a, "% Y-% m-% d")) example_data $ saatler <- biçim (example_data $ a, "% Y-% m -% d% H ") –

+0

Üzgünüm unuttum, bu benim arsa: ' ggplot (data = example_data, aes (x = gün)) + geom_bar (stat = "bin") ggplot (data = example_data, aes (x = saat)) + geom_bar (stat = "bin") Saatlerce sorun yaşıyorum. –

+0

@ A.Trzcionkowska Yukarıdaki test verilerinizi kullanarak başka bir örnek ekledim. Gelecekte, dateTimes ile çalışırken * lubridate * paketine bakmak isteyebilirsiniz. Hayatını daha kolaylaştırabilir. –

İlgili konular