2015-09-18 18 views
6

Vorrei inserire i segni di spunta nel grafico seguente ogni giorno anziché ogni 2 giorni.ggplot2 scala x data?

df1 <- structure(
    list(
    Timestamp = structure(
     c(
     1441837436, 1441843661, 
     1441885583, 1441966341, 1441985621, 1442048926, 1442321691, 1442329081, 
     1442349761, 1442408140, 1442417679, 1442508871, 1442513339, 1442513395, 
     1442514010, 1442525088, 1442553226, 1442562304 
    ), tzone = "UTC", class = c("POSIXct", 
            "POSIXt") 
    ), number = 7:24 
), class = "data.frame", row.names = c(NA,-18L), .Names = c("Timestamp", "number") 
) 

ggplot(df1, aes(x = Timestamp, y = number)) + 
    geom_line(size=2) + geom_point(size=5) + 
    scale_y_continuous(breaks = seq(0, 50, by = 2)) 

Ho provato ad aggiungere + scale_x_date(breaks = "1 day") ma ottengo il seguente errore:

Error: Invalid input: date_trans works with objects of class Date only 

Ho anche provato scale_x_datetime(limits=c(as.POSIXct('2015-09-09'), as.POSIXct('2015-09-19')), breaks = 1)


ggplot(df1, aes(x = Timestamp, y = number)) + 
    geom_line(size=2) + geom_point(size=5) + scale_x_datetime(breaks = "1 day") 
Error in strsplit(unitspec, " ") : non-character argument 

non funziona neanche :(


ho provato in due computer diversi:

> sessionInfo() 
R version 3.2.2 (2015-08-14) 
Platform: x86_64-pc-linux-gnu (64-bit) 
Running under: Debian GNU/Linux 8 (jessie) 

locale: 
[1] LC_CTYPE=en_US.UTF-8  LC_NUMERIC=C    
[3] LC_TIME=en_US.UTF-8  LC_COLLATE=en_US.UTF-8  
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 
[7] LC_PAPER=en_US.UTF-8  LC_NAME=C     
[9] LC_ADDRESS=C    LC_TELEPHONE=C    
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets 
[6] methods base  

other attached packages: 
[1] ggplot2_1.0.1.9003 taucharts_0.3.2 
[3] dplyr_0.4.3  fasttime_1.0-1  
[5] lubridate_1.3.3 readr_0.1.1  

loaded via a namespace (and not attached): 
[1] Rcpp_0.12.1  digest_0.6.8  
[3] assertthat_0.1  grid_3.2.2   
[5] plyr_1.8.3   R6_2.1.1   
[7] gtable_0.1.2  DBI_0.3.1   
[9] magrittr_1.5  scales_0.3.0.9000 
[11] stringi_0.5-5  lazyeval_0.1.10 
[13] RColorBrewer_1.1-2 tools_3.2.2  
[15] stringr_1.0.0  htmlwidgets_0.5 
[17] munsell_0.4.2  parallel_3.2.2  
[19] colorspace_1.2-6 htmltools_0.2.6 
[21] memoise_0.2.1 

2.

> sessionInfo() 
R version 3.2.1 (2015-06-18) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 
[4] LC_NUMERIC=C       LC_TIME=English_United States.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] dplyr_0.4.3  ggplot2_1.0.1.9003 

loaded via a namespace (and not attached): 
[1] colorspace_1.2-6 scales_0.3.0.9000 lazyeval_0.1.10 magrittr_1.5  R6_2.1.1   assertthat_0.1 
[7] plyr_1.8.3  parallel_3.2.1 DBI_0.3.1   tools_3.2.1  gtable_0.1.2  Rcpp_0.12.1  
[13] grid_3.2.1  munsell_0.4.2 
+0

Hai una variabile 'POSIXct' ed è necessario utilizzare una scala datetime, ad esempio,' scale_x_datetime (pause = "1 giorno") '. – Roland

+1

Ho provato, ma mi dà questo errore: 'Errore in strsplit (unitspec," "): argomento non-carattere' – Ignacio

+0

L'opzione di @Roland funziona per me. In alternativa puoi usare 'as.Date (Timestamp)'. – Jaap

risposta

19

È possibile utilizzare il pacchetto scales. Sembra che è necessario utilizzare la funzione date_breaks, piuttosto che solo breaks per ottenere le etichette corrette:

EDIT: sembra l'argomento è stato cambiato in date_breaks:

library(scales) 
library(ggplot2) 
ggplot(df1, aes(x = Timestamp, y = number)) + 
    geom_line(size=2) + 
    geom_point(size=5) + 
    scale_y_continuous(breaks = seq(0, 50, by = 2)) + 
    scale_x_datetime(date_breaks = "1 day") 

originale:

library(scales) 
library(ggplot2) 
ggplot(df1, aes(x = Timestamp, y = number)) + 
    geom_line(size=2) + 
    geom_point(size=5) + 
    scale_y_continuous(breaks = seq(0, 50, by = 2)) + 
    scale_x_datetime(breaks = date_breaks("1 day")) 

enter image description here

Se si desidera modificare la modalità di visualizzazione della data nell'etichetta, è possibile utilizzare date_format all'interno della chiamata scale_x_datetime.

+0

Dalla [documentazione] (https://github.com/hadley/ggplot2/blob/master/man/scale_date.Rd) sembra che dovresti essere in grado di usare 'scale_x_datetime (date_breaks = "1 giorno") '. – Roland

+4

Ho avuto il codice che funziona per funzionare, ma solo recentemente ho iniziato a darmi l'errore 'Error in strsplit (unitspec," "): non-character' error. Il problema è stato risolto cambiando 'scale_x_date (labels = date_format ("% b "), breaks = '1 month')' a 'scale_x_date (labels = date_format ("% b "), date_breaks = '1 month')'. Dato che funzionava, forse questo problema è sorto solo con un recente aggiornamento a 'R' o' ggplot2'? Sto usando v2.1.0 di 'ggplot2' e 3.2.2 (2015-08-14) Fire Safety di' R'. –

+1

In effetti, funzionava.Ora '' breaks'' è diventato '' date_breaks'' – PatrickT

1

Invece di utilizzare scale_x_datetime (pause = "1 giorno"), provare per scale_x_datetime (date_breaks = "1 giorno")

Problemi correlati