简体   繁体   English

R ggplot构面:不同的x轴格式和构面的不同美学

[英]R ggplot facet: varying x axis formatting and varying aesthetics by facet

I was hoping someone could help with 1) varying the format of the x-axis across panels in a facet plot 2) showing the loess smoother only on the left side of the facet. 我希望有人可以提供帮助:1)在刻面图中改变面板上x轴的格式2)仅在刻面的左侧显示更光滑的黄土。

The left side is longer history, the right is last twelve months. 左侧是更长的历史,右侧是最近十二个月。 The real dataset has more data (more time and more variables), and so these problems become worse, but I think the below code should show my issues. 实际的数据集具有更多的数据(更多的时间和更多的变量),因此这些问题变得更糟,但是我认为下面的代码应该显示我的问题。 The right hand side dates are squashed, and the smoother is not needed with only twelve points. 右侧日期被压缩,仅十二点就不需要更平滑的日期了。

Very grateful for any help! 非常感谢您的帮助! Thanks Aidan 谢谢艾丹

    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

It is easy enough to get the smoother on one panel: 在一个面板上获得平滑效果很容易:

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

And as far changing the appearance of the labels on the x axis, look at the options here: https://github.com/hadley/ggplot2/wiki/+opts()-List 至于要更改x轴上标签的外观,请在此处查看选项: https : //github.com/hadley/ggplot2/wiki/+opts()-列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM