2017-04-07 2 views
0

이 분석을 R로 구현하고 있습니다. 데이터 세트를 다운로드하고 동물원 개체를 만들고 데이터 집합을 그립니다. 더 나은 보이는 그래프를 제시하기 위해다중 행 y 레이블이있는 그림 맞추기

library(tseries) 
library(zoo) 

start <- "2011-01-01" 
end <- "2014-12-31" 

MET <- get.hist.quote("MET", quote="Close", start=start, end=end) 
MHK <- get.hist.quote("MHK", quote="Close", start=start, end=end) 
MJN <- get.hist.quote("MJN", quote="Close", start=start, end=end) 
MKC <- get.hist.quote("MKC", quote="Close", start=start, end=end) 
MLM <- get.hist.quote("MLM", quote="Close", start=start, end=end) 
MMC <- get.hist.quote("MMC", quote="Close", start=start, end=end) 
MMM <- get.hist.quote("MMM", quote="Close", start=start, end=end) 
MNK <- get.hist.quote("MNK", quote="Close", start=start, end=end) 
MNST <- get.hist.quote("MNST", quote="Close", start=start, end=end) 
MO <- get.hist.quote("MO", quote="Close", start=start, end=end) 
MON <- get.hist.quote("MON", quote="Close", start=start, end=end) 
MOS <- get.hist.quote("MOS", quote="Close", start=start, end=end) 
MPC <- get.hist.quote("MPC", quote="Close", start=start, end=end) 
MRK <- get.hist.quote("MRK", quote="Close", start=start, end=end) 
MRO <- get.hist.quote("MRO", quote="Close", start=start, end=end) 

Series <- zoo(cbind(MET, MHK, MJN, MKC, MLM, MMC, MMM, MNK, MNST, MO, 
        MON, MOS, MPC, MRK, MRO)) 
colnames(Series) <- c("MetLife", "Mohawk", "\nMead\nJohnson", 
         "McCormick", "Martin\nMarietta", 
         "Marsh and\nMcLennan", "3M", "Mallinckrodt", 
         "Monster\nBeverage", "Altria", "Monsanto", 
         "The Mosaic\nCompany", "Marathon\nPetroleum", 
         "Merck", "Marathon Oil") 
Series <- na.approx(Series) 

plot(Series, main = "", xlab = "") 

, 나는이 개 라인에서 y를 레이블 이름을 분할 명령 \n을 도입했습니다. 그러나 그래픽 출력은 왼쪽 y 레이블을 여백에서 제외시킵니다.

R Plot Example

내 모든 출력의 변화없이 기능 par()에 마르와 마이 모두를 수정하는 것을 시도했다. 나는 이것이 아마도 객체 (동물 객체)에 연결될 수 있다고 생각한다.

답변

0

tidyverse - 특히 ggplot이 세 글자-을 의미 함 매개 변수 무한히을 조정하지 않고 합리적으로 그래픽을 찾고 얻을하기가 좀 더 쉽게 :

library(tidyverse) 

Series %>% 
    as.data.frame %>% 
    mutate(date = row.names(.) %>% as.Date) %>% 
    gather(ticker, price, -date) %>% 
    filter (!is.na(price)) -> 
    dl 

dl %>% 
    ggplot(aes(date, price)) + 
    geom_line() + 
    facet_wrap(~ticker, strip.position = "left", ncol = 2) 

tickers in ggplot

NB : 당신도 \n 포장을 얻을 수 있습니다 자동으로 facet_wrap(..., labeller = labeller(ticker = label_wrap_gen(10))

NB2 : 일부 시리즈가 다운로드되지 않았습니다. (모든 NAs)

+0

나는 ggplot2에 대해서도 생각하고있었습니다. 그러나 나는 정상적인 음모에 의해 주어진 출력을 선호합니다. ggplot을 사용하여 모든 Y 축척을 부드럽게 유지합니다. 위 이미지의 여백을 수정하는 데 도움을 주시겠습니까? –

+0

'facet_wrap (.., scales = "free_y")';)와 같은 소리가납니다. 죄송합니다, 나는/전혀 기본 그래픽을 알지는 못하지만'동물원'은'par()'설정을 무시할 수있는 자신의'plot' 방법을 가지고있을 것이라고 생각합니다. – liborm

+0

약간의 인터넷 검색 후 [this] (https://timelyportfolio.github.io/rCharts_time_series/history.html)에서 autoplot.zoo()를 사용하면 데이터를 쉽게 그릴 수 있습니다. – liborm

관련 문제