【问题标题】:R ggplot facet: varying x axis formatting and varying aesthetics by facetR ggplot facet:通过 facet 改变 x 轴格式和改变美学
【发布时间】:2012-06-01 02:35:26
【问题描述】:

我希望有人能帮忙 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 点,但你可能想调查 facet_gridscalesspace 参数 ...
  • 感谢本。我尝试尝试使用比例和空间,但我似乎无法在右侧获得稍大的间距或其他格式更改以使日期更清晰

标签: r ggplot2 facet


【解决方案1】:

在一个面板上获得更流畅的效果很容易:

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

【讨论】:

  • 关于第二点,我的意思是日期不清晰(尤其是在数据稍微多一点之后,但即使在这里日期也有点重叠)
  • @Aidan,这很清楚 :) 尝试使用此处列出的选项:github.com/hadley/ggplot2/wiki/+opts()-List
  • 感谢您的链接。添加选项 axis.text.x=theme_text(angle=90) 使标签清晰可见。由于易读性只是右侧的一个问题,我试图想办法改变右轴。但我想这并不容易,以这种方式改变网格两侧的 x 轴效果很好。非常感谢您的帮助!
  • 是的,我不知道有什么方法可以在两个轴上有不同的选项。不过,如果可能的话,我有一半认为它看起来会很不整洁。我很高兴这对你有用。
猜你喜欢
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
相关资源
最近更新 更多