首页 文章

R ggplot facet:通过facet改变x轴格式和变化的美学

提问于
浏览
2

我希望有人可以帮助1)在小平面图中改变面板上x轴的格式2)显示小平面左侧的黄土更平滑 .

左侧是较长的历史,右侧是最后十二个月 . 真正的数据集有更多的数据(更多的时间和更多的变量),所以这些问题变得更糟,但我认为下面的代码应该显示我的问题 . 右手日期被压扁,只需十二分就不需要更顺畅 .

非常感谢任何帮助!谢谢艾丹

g<-structure(list(Date = structure(c(13909, 13938, 13969, 13999,
    14029, 14060, 14091, 14120, 14152, 14183, 14211, 14244, 14274,
    14302, 14334, 14364, 14393, 14425, 14456, 14487, 14517, 14547,
    14578, 14609, 14638, 14666, 14699, 14729, 14760, 14790, 14820,
    14852, 14882, 14911, 14943, 14974, 15005, 15033, 15064, 15093,
    15125, 15155, 15184, 15217, 15247, 15278, 15308, 15338, 15370,
    15399, 15429, 15460, 15125, 15155, 15184, 15217, 15247, 15278,
    15308, 15338, 15370, 15399, 15429, 15460), class = "Date"), value = c(199.8,
    195.7, 200.1, 201, 207.7, 215.1, 210.3, 202.9, 191, 186.5, 180.1,
    175.7, 164.6, 168.2, 169.9, 166.6, 174.7, 181.8, 181.3, 177.3,
    176.1, 172.2, 170, 170.7, 164.9, 164.6, 169.6, 172.3, 174.6,
    182.9, 182.1, 177.3, 171.4, 170.6, 170.2, 168.8, 157.9, 156.1,
    159.8, 161.1, 169.3, 175.6, 171.2, 171.2, 165.3, 160.8, 164,
    162.2, 154.6, 155.6, 164.8, 177.4, 169.3, 175.6, 171.2, 171.2,
    165.3, 160.8, 164, 162.2, 154.6, 155.6, 164.8, 177.4), variable = structure(c(1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "NAR Median Hse Price ($000)", class = "factor"),
        var2 = c("History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "History",
        "History", "History", "History", "History", "History", "LTM",
        "LTM", "LTM", "LTM", "LTM", "LTM", "LTM", "LTM", "LTM", "LTM",
        "LTM", "LTM")), .Names = c("Date", "value", "variable", "var2"
    ), row.names = c("1016", "1017", "1018", "1019", "1020", "1021",
    "1022", "1023", "1024", "1025", "1026", "1027", "1028", "1029",
    "1030", "1031", "1032", "1033", "1034", "1035", "1036", "1037",
    "1038", "1039", "1040", "1041", "1042", "1043", "1044", "1045",
    "1046", "1047", "1048", "1049", "1050", "1051", "1052", "1053",
    "1054", "1055", "1056", "1057", "1058", "1059", "1060", "1061",
    "1062", "1063", "1064", "1065", "1066", "1067", "10561", "10571",
    "10581", "10591", "10601", "10611", "10621", "10631", "10641",
    "10651", "10661", "10671"), class = "data.frame")

    gp<-ggplot(g,aes(x=Date,y=value,group=variable)) +
         opts(
            panel.background = theme_rect(size = 1, colour = "lightgray"),
            panel.grid.minor = theme_blank(),
            strip.background = theme_blank(),
            axis.title.x = theme_blank(),
            axis.title.y = theme_blank()
      ,strip.text.y = theme_text(size = 12,angle = 0)
        )
    gp<- gp + geom_line(size=1)
    gp <-gp + facet_grid(variable ~ var2, scales="free")
    gp<-gp+geom_smooth(method=loess,size=1,span=.35,alpha=.005)
    gp

1 回答

  • 4

    在一个面板上轻松实现更顺畅:

    gp<-ggplot(g,aes(x=Date,y=value,group=variable)) +
        opts(
            panel.background = theme_rect(size = 1, colour = "lightgray"),
            panel.grid.minor = theme_blank(),
            strip.background = theme_blank(),
            axis.title.x = theme_blank(),
            axis.title.y = theme_blank()
            ,strip.text.y = theme_text(size = 12,angle = 0)
        )
    gp<- gp + geom_line(size=1)
    gp <-gp + facet_grid(variable ~ var2, scales="free")
    gp<-gp+geom_smooth(data=g[g$var2=="History",],
                       method=loess,size=1,span=.35,alpha=.005)
    gp
    

    至于改变x轴上标签的外观,请查看以下选项:https://github.com/hadley/ggplot2/wiki/+opts()-List

相关问题