2012-05-25 33 views
2

我希望有人可以幫助 1)改變面板圖上的X軸的格式 2)只顯示黃土平滑小平面的左側。R ggplot方面:改變X軸格式和改變美學方面

左側歷史較長,右側爲過去十二個月。 真實的數據集有更多的數據(更多的時間和更多的變量),所以這些問題變得更糟,但我認爲下面的代碼應該顯示我的問題。 右側的日期被壓扁,只有12點不需要更平滑。

非常感謝任何幫助! 感謝 艾丹

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 
+0

我也不確定你的觀點#1,但你可能想調查'scale'和'space'參數到'facet_grid' ... –

+0

感謝Ben。我試着用尺度和空間進行實驗,但我似乎無法得到稍微更大的間距或右側的其他格式更改,以使日期更清晰 – Aidan

回答

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

+0

感謝Gauden,解決了更平滑的問題。 – Aidan

+0

關於第二點,我的意思是說日期不明確(特別是在稍微有更多的數據後,但即使在這裏日期也在彼此之間運行) – Aidan

+0

@Aidan,很明顯:)嘗試使用列出的選項這裏:https://github.com/hadley/ggplot2/wiki/+opts()-List – gauden