ggplot2中的scale_x_labels错误

约翰

我正在尝试使用ggplot2重新创建本书中的图表我正在使用的数据位于的底部dput可从发布者的网站上获得。我正在重新创建第12页的时间序列图。

这是我正在使用的代码:

y_min <- min(chartout$LCL)
y_max <- max(chartout$UCL)

ggplot() + 
        geom_line(mapping=aes(x=Date, y=Rate), data=chartout, size=1, alpha=0.5) +
        geom_point(data = chartout, mapping=aes(x=Date,y=Rate), size=3) +
        expand_limits(y = c(y_min, y_max)) +
        geom_line(mapping=aes(x=Date, y=UCL), data=chartout, colour='red',  size=1, alpha=0.5) + 
        geom_line(mapping=aes(x=Date, y=LCL), data=chartout, colour='red' , size=1, alpha=0.5) +
        scale_x_date(labels = date_format("%m %d")) +
        theme_bw()

问题是我遇到以下错误:

错误:输入无效:date_trans仅适用于Date类的对象

我已经看过这个问题,但是您可以从结构中看到dputDate是Date格式。我知道这是因为我使用进行了转换lubridate

任何想法如何解决这一问题?

dput输出在这里:

chartout <- structure(list(Rate = c(0.2558, 0.2351, 0.4804, 0.2357, 0.2262, 
0.0433, 0.0839, 0.0422, 0.2569, 0.1283, 0.045, 0.2418, 0.3373, 
0.3214, 0.3026, 0.1307, 0.2986, 0.0431, 0.1259, 0.3339, 0.0432, 
0.2083, 0.3861, 0.0461, 0.4428, 0.1368, 0.3212, 0.1766, 0.1643, 
0.2503, 0.2033, 0.1199, 0.2084, 0.161, 0.3729, 0.4103, 0.4315, 
0.2195, 0.3619, 0.1698, 0.2368, 0.3717, 0.1943, 0.3199, 0.2495, 
0.3587, 0.1673, 0.3028, 0.2572, 0.2976, 0.4033, 0.2813, 0.3966, 
0.1609), Mean = c(0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 
0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411, 0.2411
), Date = structure(c(1072915200, 1075593600, 1078099200, 1080777600, 
1083369600, 1086048000, 1088640000, 1091318400, 1093996800, 1096588800, 
1099267200, 1101859200, 1104537600, 1107216000, 1109635200, 1112313600, 
1114905600, 1117584000, 1120176000, 1122854400, 1125532800, 1128124800, 
1130803200, 1133395200, 1136073600, 1138752000, 1141171200, 1143849600, 
1146441600, 1149120000, 1151712000, 1154390400, 1157068800, 1159660800, 
1162339200, 1164931200, 1167609600, 1170288000, 1172707200, 1175385600, 
1177977600, 1180656000, 1183248000, 1185926400, 1188604800, 1191196800, 
1193875200, 1196467200, 1199145600, 1201824000, 1204329600, 1207008000, 
1209600000, 1212278400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    UCL = c(0.6601, 0.6397, 0.6227, 0.6403, 0.6306, 0.6208, 0.6137, 
    0.6151, 0.6183, 0.618, 0.6294, 0.6464, 0.6456, 0.6341, 0.6204, 
    0.6221, 0.6174, 0.6197, 0.6139, 0.6126, 0.6205, 0.6122, 0.6187, 
    0.6351, 0.6258, 0.6324, 0.6042, 0.6252, 0.6091, 0.6125, 0.6069, 
    0.6032, 0.6124, 0.6047, 0.611, 0.6324, 0.62, 0.6238, 0.6045, 
    0.6163, 0.6006, 0.6103, 0.5973, 0.6033, 0.6118, 0.6026, 0.6131, 
    0.6206, 0.6185, 0.6167, 0.6051, 0.6043, 0.6016, 0.6046), 
    LCL = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Rate", 
"Mean", "Date", "UCL", "LCL"), row.names = c(NA, -54L), class = "data.frame")
研究

您应该使用,scale_x_datetime因为您有POSIXct

 scale_x_datetime(labels=date_format("%Y %H:%M"))

或将您的Date变量转换为Date(不带时间部分)

chartout$Date <- as.Date(chartout$Date) 

我会使用这种格式:

scale_x_date(labels = date_format("%m %b")) +

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ggplot2中的scale_x_labels错误

来自分类Dev

ggplot2:scale_x_datetime中的小中断

来自分类Dev

ggplot2错误:scale_x_date显示/不删除超出指定限制的数据

来自分类Dev

ggplot2:R中带有scale_x_date轴的多因子箱线图

来自分类Dev

在ggplot2中创建特定的日期范围(scale_x_date)

来自分类Dev

在ggplot2中,如何使stat_function服从scale_x_log10?

来自分类Dev

在ggplot2 :: scale_x_discrete()中包装标签文本-RTL语言不匹配顺序

来自分类Dev

在ggplot2中一起使用xlim(,)和scale_x_reverse()

来自分类Dev

不显示ggplot2中的轴百分比:`scale_y_continuous(labels = percent)`

来自分类Dev

ggplot2:在ROC图上使用scale_x_reverse

来自分类Dev

在ggplot2中使用scale_x_discrete()自定义x轴时出错

来自分类Dev

ggplot2中的x轴格式

来自分类Dev

ggplot2中的x轴格式

来自分类Dev

使用scale_x_log10时,如何在geom_histogram中设置ggplot2 binwidth?

来自分类Dev

在facet_grid上下文中设置ggplot2中scale_x_date的限制

来自分类Dev

ggplot2:barplot中的错误-X轴显示不正确

来自分类Dev

从ggplot2中的ggplot x轴上删除NA

来自分类Dev

ggplot2中的scale_colour_gradient与scale_fill_gradient

来自分类Dev

从ggplot2中的错误栏中删除端点

来自分类Dev

ggplot2和时间数据的时间条形图scale_x_chron

来自分类Dev

ggplot2 scale_x_discrete值导致不均匀的轴间距

来自分类Dev

GGPLOT2:无效输入:使用scale_x_datetime时time_trans

来自分类Dev

ggplot2:使用自定义月份标签覆盖 scale_x_datetime

来自分类Dev

ggplot2 中不同行的两个不同 scale_colour_gradient2()

来自分类Dev

在ggplot2 / metR中绘图时出现nullGrob错误

来自分类Dev

ggplot2中的facet_grid错误

来自分类Dev

ggplot2 stat_summary_bin中的错误栏

来自分类Dev

在 ggplot2 中创建绘图循环会导致错误

来自分类Dev

如何修复自定义 ggplot2 函数中的“替换有 [x] 行,数据有 [y]”错误?

Related 相关文章

  1. 1

    ggplot2中的scale_x_labels错误

  2. 2

    ggplot2:scale_x_datetime中的小中断

  3. 3

    ggplot2错误:scale_x_date显示/不删除超出指定限制的数据

  4. 4

    ggplot2:R中带有scale_x_date轴的多因子箱线图

  5. 5

    在ggplot2中创建特定的日期范围(scale_x_date)

  6. 6

    在ggplot2中,如何使stat_function服从scale_x_log10?

  7. 7

    在ggplot2 :: scale_x_discrete()中包装标签文本-RTL语言不匹配顺序

  8. 8

    在ggplot2中一起使用xlim(,)和scale_x_reverse()

  9. 9

    不显示ggplot2中的轴百分比:`scale_y_continuous(labels = percent)`

  10. 10

    ggplot2:在ROC图上使用scale_x_reverse

  11. 11

    在ggplot2中使用scale_x_discrete()自定义x轴时出错

  12. 12

    ggplot2中的x轴格式

  13. 13

    ggplot2中的x轴格式

  14. 14

    使用scale_x_log10时,如何在geom_histogram中设置ggplot2 binwidth?

  15. 15

    在facet_grid上下文中设置ggplot2中scale_x_date的限制

  16. 16

    ggplot2:barplot中的错误-X轴显示不正确

  17. 17

    从ggplot2中的ggplot x轴上删除NA

  18. 18

    ggplot2中的scale_colour_gradient与scale_fill_gradient

  19. 19

    从ggplot2中的错误栏中删除端点

  20. 20

    ggplot2和时间数据的时间条形图scale_x_chron

  21. 21

    ggplot2 scale_x_discrete值导致不均匀的轴间距

  22. 22

    GGPLOT2:无效输入:使用scale_x_datetime时time_trans

  23. 23

    ggplot2:使用自定义月份标签覆盖 scale_x_datetime

  24. 24

    ggplot2 中不同行的两个不同 scale_colour_gradient2()

  25. 25

    在ggplot2 / metR中绘图时出现nullGrob错误

  26. 26

    ggplot2中的facet_grid错误

  27. 27

    ggplot2 stat_summary_bin中的错误栏

  28. 28

    在 ggplot2 中创建绘图循环会导致错误

  29. 29

    如何修复自定义 ggplot2 函数中的“替换有 [x] 行,数据有 [y]”错误?

热门标签

归档