2016-08-01 100 views
-1

我有一个问题,格式化x轴的时间。ggplot2轴作为时间与1小时的错误

这是我的数据样本:

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

我想用GGPLOT2打造Breath.d13C的VS中药真实性(收集时间)的图表。

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。然而,时间不到一个小时。 我可以通过检查Chms列或使用正常[R地块R plots

与此代码中看到这一点:

plot(x$Chms,x$Breath.d13C,cex=0.8) 

两个地块使用相同的数据集,所以我不知道是什么引起的错误ggplot2。不过,我想继续使用它。关于我做错什么的想法?

预先感谢您

+0

'错误check_breaks_labels(休息,标签):找不到功能 “fmt_decimals”'。您是否使用了这个函数:'fmt_dcimals < - 函数(小数= 0){返回一个可以格式化 #轴标签的函数 function(x)as.character(round(x,decimals )) }'而且,my'the_theme'是什么? – Miha

回答

1

您需要指定的时间段中scale_x_datetime

功能date_format()默认设置为“UTC”。因此,您的标签将转换为UTC。要使用时区,例如我用“欧洲/伦敦”(得到您想要的输出),你可以做你的ggplot代码如下:labels = date_format("%H:%M", tz = "Europe/London")

以运行代码

但是首先我也不得不定义你指定什么在你的代码fmt_decimals所以我使用@joran给出这样的功能:

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

所以,你的代码如下所示:

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

和输出:

enter image description here

+0

谢谢!我怀疑它与时区有关。但没有设法解决它。 (对不起,我忘了包含'fct_decimals'的函数,但你猜对了) – answer42

+0

@ answer42乐于帮助。 – Miha

1

的问题在于你选择时区,即UTC。你应该选择当前的时区。修正后的代码如下

 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)) + 
     scale_x_datetime(name="Collection Time", 
       labels = date_format("2016-08-01 %H:00",""), 
       date_breaks = "1 hour") 

See the plot as belpw