r - ggplot2 axis as time with 1 hour error -


this question has answer here:

i have question on formatting x axis time.

this sample of data:

dput(x) structure(list(sample = c("bk01", "bk02", "bk03", "bk04", "bk05",  "bk06", "bk07", "bk08", "bk09", "bk10", "bk11", "bk12", "bk13",  "bk14", "bk15", "bk16", "bk17", "bk18", "bk19", "bk20", "bk21",  "bk22", "bk23", "bk24", "bk25", "bk26", "bk27", "bk28", "bk29",  "bk30", "bk31", "bk32", "bk33"), breath.d13c = c(-25.62, -27.45,  -26.87, -25.21, -26.01, -24.33, -24.45, -23.73, -25.05, -26.11,  -27, -26.28, -24.62, -26.96, -24.55, -24.52, -21.24, -26.18,  -24.82, -26.12, -27.28, -26.5, -24.46, -22.83, -27.28, -25.55,  -27.12, -24.46, -23.07, -28.35, na, -25.98, -26.64), chms = structure(c(1470047400,  1470048300, 1470048300, 1470049200, 1470050100, 1470050100, 1470040200,  1470041100, 1470040200, 1470041100, 1470065400, 1470063600, 1470063600,  1470064500, 1470061800, 1470045600, 1470045600, 1470046500, 1470047400,  1470066300, 1470060000, 1470058200, 1470057300, 1470047400, 1470042000,  1470042000, 1470041100, 1470041100, 1470040200, 1470043800, na,  1470060000, 1470039300), class = c("posixct", "posixt"), tzone = "")), class = "data.frame", row.names = c(na,  -33l), .names = c("sample", "breath.d13c", "chms")) 

i want use ggplot2 build graph of breath.d13c vs chms (collection time).

library(ggplot2)  ggplot(x, aes(x=chms,y=breath.d13c)) + geom_point() +   scale_y_continuous(name=expression(delta^13*c["breath"]*" "("\u2030")),                      limits=c(-30,-10),                      breaks=seq(-30,-10,5),                      labels=fmt_decimals(1)) +   scale_x_datetime(name="collection time",                     labels = date_format("%h:00",tz="utc"),                    date_breaks = "1 hour") +   my_theme 

this code gives me this. times off hour. can see checking chms column or using normal r plots r plots

with code:

plot(x$chms,x$breath.d13c,cex=0.8) 

the 2 plots use same data set, have no idea what's causing error on ggplot2. i'd keep using it, though. ideas on doing wrong?

thank in advance

you need specify time zone in scale_x_datetime.

the function date_format() default set "utc". therefore, labels converted utc. use time zone e.g. used "europe/london" (to desired output), can following in ggplot code: labels = date_format("%h:%m", tz = "europe/london")

but firstly in order run code had define specified in code fmt_decimals used function given @joran:

fmt_dcimals <- function(decimals=0){    # return function responpsible formatting     # axis labels given number of decimals     function(x) as.character(round(x,decimals)) } 

so code looks this:

ggplot(x, aes(x=chms,y=breath.d13c)) +   geom_point() +   scale_y_continuous(name=expression(delta^13*c["breath"]*" "("\u2030")),                      limits=c(-30,-10),                      breaks=seq(-30,-10,5),                      labels=fmt_dcimals(1)) +     scale_x_datetime(name="collection time",                     labels = date_format("%h:%m", tz = "europe/london"),                    date_breaks = "1 hour")  

and output:

enter image description here


Comments