简体   繁体   English

R中的XY轴格式

[英]X-Y Axis Formatting in R

I am trying to plot a graph and format the y-axis such that the numbers have proper spacing (5,000 apart) and are formatted to have commas (eg 350,000). 我正在尝试绘制图形并对y轴进行格式化,以使数字具有适当的间距(相隔5,000)并被格式化为具有逗号(例如350,000)。 Also I am trying to get the x-axis (the date) to show more refinement such that I have more dates on the x-axis for the viewer to see. 另外,我正在尝试使x轴(日期)显示更多细化,以便在x轴上有更多日期供查看者查看。 Every time I plot the graph I get numbers on the x-axis, not dates. 每次绘制图表时,我都会在x轴上得到数字,而不是日期。

This is what I have so far 这就是我到目前为止

plot(date,sales,type="s",ylab="Sales",yaxt="n")
axis(1, at = seq(min(date), max(date), length.out = 1)
axis(2, at = seq(min(sales), max(sales), length.out = 20), labels = formatC(seq(min(revenue), max(revenue), length.out = 20),small.mark = " ", format = "d"), las = 2)

Here is the data I am using: 这是我正在使用的数据:

sales <- c(76103, 57300, 49875, 52113, 47891, 43531, 50909, 54182, 55884, 
63780, 57165, 59841, 65952, 67602, 70693, 76375, 83365, 82051, 
88568, 100717, 99344, 88980, 99034, 99593, 110497, 87223, 98350, 
102337, 116642, 116854, 138072, 137737, 84696, 64028, 74457, 
82260, 89841, 90251, 92486, 95298, 105186, 114004, 125486, 125330, 
121609, 124053, 127363, 115706, 115173, 108807, 106469, 112372, 
110860, 106773, 111647, 107490, 86029, 67618, 74113, 67344)

date <- structure(c(11292, 11382, 11474, 11566, 11657, 11747, 11839, 
11931, 12022, 12112, 12204, 12296, 12387, 12478, 12570, 12662, 
12753, 12843, 12935, 13027, 13118, 13208, 13300, 13392, 13483, 
13573, 13665, 13757, 13848, 13939, 14031, 14123, 14214, 14304, 
14396, 14488, 14579, 14669, 14761, 14853, 14944, 15034, 15126, 
15218, 15309, 15400, 15492, 15584, 15675, 15765, 15857, 15949, 
16040, 16130, 16222, 16314, 16405, 16495, 16587, 16679), class = "Date")

Unless your plot is enormous (much bigger than your screen), the number of labels you're asking for will be unreadable, because they'll overlay each other. 除非您的地块很大(比屏幕大得多),否则您要求的标签数将不可读,因为它们会相互重叠。 Following the spirit of what you're asking for (y-ticks every 5000, commas in y labels, more x labels that make sense): 遵循您所要求的精神(每5000个y标记,y标签中的逗号,更有意义的x标签):

# enlarge left margin to accommodate transposed labels
par(mar = c(5.1, 6.1, 2.1, 2.1))

# a sequence for tick marks
y_seq <- seq((min(sales) %/% 5000) * 5000, 
            (max(sales) %/% 5000 + 1) * 5000,
            by = 5000)

# a sequence for labels
y_labs <- prettyNum(y_seq, big.mark = ',')
y_labs[seq(1, length(y_labs), 2)] <- NA

plot(date, sales, type="s", ylab=NA, xaxt = 'n', yaxt = 'n')

# `axis.Date` makes sequencing and formatting easy; adapt as you like
axis.Date(1, date, at = seq.Date(min(date), max(date), by = 'year'), format = '%Y')
axis(2, at = y_seq, labels = y_labs, las = 2)

# add y label in a readable location
title(ylab = 'Sales', mgp = c(4.5, 1, 0))

which produces 产生 带有固定轴标签的图

If you want more labels, the approach is pretty customizable from here. 如果您想要更多标签,则可以从此处自定义方法。

One last point: While you can always fight base R graphics until they do what you want, sometimes the fight isn't worth it when ggplot2 has much better defaults. 最后一点:尽管您总是可以与基本R图形进行战斗直到它们完成您想要的操作,但是当ggplot2具有更好的默认值时,有时不值得进行这种战斗。

Just two lines: 仅两行:

library(ggplot2)
qplot(date, sales, geom = 'step')

makes 使

ggplot版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM