r - Fitting a plot with multi-line y labels -
i implementing analysis in r: download dataset, creation of zoo object , plot dataset.
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 = "")
in order present better-looking graph, have introduced command \n
split y label names in 2 lines. graphical output puts the left y labels out of margin.
i have tried modify both mar , mai in function par()
without change @ of output. think maybe can linked object drawn (a zoo object).
tidyverse - ggplot
makes bit easier reasonably looking graphics without tuning three-letter-acronymed parameters ad infinitum:
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)
nb: can \n
wrapping automatically facet_wrap(..., labeller = labeller(ticker = label_wrap_gen(10))
nb2: of series did not download (all nas), did not bother
Comments
Post a Comment