Aidan
Aidan

Reputation: 109

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.

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

Upvotes: 2

Views: 1962

Answers (1)

daedalus
daedalus

Reputation: 10923

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

Upvotes: 4

Related Questions