{"id":879,"date":"2015-08-14T15:49:35","date_gmt":"2015-08-14T22:49:35","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=879"},"modified":"2015-08-14T15:49:35","modified_gmt":"2015-08-14T22:49:35","slug":"position-scale-date","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/08\/14\/position-scale-date\/","title":{"rendered":"Position scale, date"},"content":{"rendered":"<h2>Usage<\/h2>\n<pre>scale_x_date(..., expand = waiver(), breaks = pretty_breaks(), minor_breaks = waiver())\n\nscale_y_date(..., expand = waiver(), breaks = pretty_breaks(), minor_breaks = waiver())<\/pre>\n<h2>Arguments<\/h2>\n<dl>\n<dt>breaks<\/dt>\n<dd>A vector of breaks, a function that given the scale limits returns a vector of breaks, or a character vector, specifying the width between breaks. For more information about the first two, see <code><a href=\"http:\/\/docs.ggplot2.org\/current\/continuous_scale.html\">continuous_scale<\/a><\/code>, for more information about the last, see<code><a href=\"http:\/\/www.inside-r.org\/r-doc\/scales\/date_breaks\">date_breaks<\/a><\/code>`.<\/dd>\n<dt>minor_breaks<\/dt>\n<dd>Either <code>NULL<\/code> for no minor breaks, <code>waiver()<\/code> for the default breaks (one minor break between each major break), a numeric vector of positions, or a function that given the limits returns a vector of minor breaks.<\/dd>\n<dt>&#8230;<\/dt>\n<dd>common continuous scale parameters: <code>name<\/code>, <code>breaks<\/code>, <code>labels<\/code>, <code>na.value<\/code>, <code>limits<\/code> and <code>trans<\/code>. See <code><a href=\"http:\/\/docs.ggplot2.org\/current\/continuous_scale.html\">continuous_scale<\/a><\/code> for more details<\/dd>\n<dt>expand<\/dt>\n<dd>a numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes.<\/dd>\n<\/dl>\n<div class=\"Description\">\n<h2>Description<\/h2>\n<p>Position scale, date<\/p>\n<\/div>\n<h2 id=\"examples\">Examples<\/h2>\n<div class=\"input\"># We&#8217;ll start by creating some nonsense data with dates df &lt;- data.frame( date = seq(Sys.Date(), len=100, by=&#8221;1 day&#8221;)[sample(100, 50)], price = runif(50) ) df &lt;- df[order(df$date), ] dt &lt;- qplot(date, price, data=df, geom=&#8221;line&#8221;) + theme(aspect.ratio = 1\/4) # We can control the format of the labels, and the frequency of # the major and minor tickmarks. See ?format.Date and ?seq.Date # for more details. library(scales) # to access breaks\/formatting functions dt + scale_x_date()<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-2.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">dt + scale_x_date(labels = date_format(&#8220;%m\/%d&#8221;))<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-4.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">dt + scale_x_date(labels = date_format(&#8220;%W&#8221;))<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-6.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">dt + scale_x_date(labels = date_format(&#8220;%W&#8221;), breaks = date_breaks(&#8220;week&#8221;))<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-8.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">dt + scale_x_date(breaks = date_breaks(&#8220;months&#8221;), labels = date_format(&#8220;%b&#8221;))<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-10.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">dt + scale_x_date(breaks = date_breaks(&#8220;4 weeks&#8221;), labels = date_format(&#8220;%d-%b&#8221;))<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-12.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\"># We can use character string for breaks. # See \\code{\\link{by}} argument in \\code{\\link{seq.Date}}. dt + scale_x_date(breaks = &#8220;2 weeks&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-14.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">dt + scale_x_date(breaks = &#8220;1 month&#8221;, minor_breaks = &#8220;1 week&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-16.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\"># The date scale will attempt to pick sensible defaults for # major and minor tick marks qplot(date, price, data=df[1:10,], geom=&#8221;line&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-18.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">qplot(date, price, data=df[1:4,], geom=&#8221;line&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-20.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">df &lt;- data.frame( date = seq(Sys.Date(), len=1000, by=&#8221;1 day&#8221;), price = runif(500) ) qplot(date, price, data=df, geom=&#8221;line&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-22.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\"># A real example using economic time series data qplot(date, psavert, data=economics)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-24.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">qplot(date, psavert, data=economics, geom=&#8221;path&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-26.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">end &lt;- max(economics$date) last_plot() + scale_x_date(limits = c(as.Date(&#8220;2000-1-1&#8221;), end))<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-28.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">last_plot() + scale_x_date(limits = c(as.Date(&#8220;2005-1-1&#8221;), end))<\/div>\n<pre class=\"examples\"><strong class=\"message\">Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-31.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">last_plot() + scale_x_date(limits = c(as.Date(&#8220;2006-1-1&#8221;), end))<\/div>\n<pre class=\"examples\"><strong class=\"message\">Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-34.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\"># If we want to display multiple series, one for each variable # it&#8217;s easiest to first change the data from a &#8220;wide&#8221; to a &#8220;long&#8221; # format: library(reshape2) # for melt em &lt;- melt(economics, id = &#8220;date&#8221;) # Then we can group and facet by the new &#8220;variable&#8221; variable qplot(date, value, data = em, geom = &#8220;line&#8221;, group = variable)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-36.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n<div class=\"input\">qplot(date, value, data = em, geom = &#8220;line&#8221;, group = variable) + facet_grid(variable ~ ., scale = &#8220;free_y&#8221;)<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/docs.ggplot2.org\/current\/scale_date-38.png\" alt=\"\" width=\"400\" height=\"400\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usage scale_x_date(&#8230;, expand = waiver(), breaks = pretty_breaks(), minor_breaks = waiver()) scale_y_date(&#8230;, expand = waiver(), breaks = pretty_breaks(), minor_breaks = waiver()) Arguments breaks A vector&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-879","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=879"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/879\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}