这是csv数据:https://www.dropbox.com/s/87bwvhyo6i4f68u/test.csv,第一列是日期,第二列是对应的值.
我想用excel制作这样的图片:https://www.dropbox.com/s/13wyk1aeajgs6fq/test.png.注意:绘图中的ylim更改为对数刻度(基数10),这可以使图像比原始图像更好.我想在R中使用它,特别是使用ggplot使其美观,但我不能.所以我希望你能提供帮助.
ba <- as.data.frame(fread("test.csv"))
plot(as.factor(ba[, 1]), ba[, 2], log = "y", xaxt = "n", xlab = "")
idx <- seq(1, 100, length = 10)
labels <- ba[, 1]
labels <- labels[idx]
axis(1, seq(1, 100, length = 10), par("usr")[3], srt = 45, labels = labels,
adj = 1, xpd = TRUE)
Run Code Online (Sandbox Code Playgroud)
谢谢.
编辑:
[以下是rawr的答案].那是完美的.
tmp <- read.csv('~/desktop/test.csv', stringsAsFactors = FALSE,
strip.white = TRUE, header = FALSE)
tmp$V1 <- paste0(tmp$V1, '-01')
tmp <- within(tmp, {
V1 <- as.Date(V1)
date <- format(as.Date(tmp$V1, '%Y-%m-%d'), '%Y-%m')
stuff <- V2
})
par(tcl = -.1, xpd = FALSE)
with(tmp,
plot(V1, log(stuff), type = 'n', ylim = c(0,6),
col = 'royalblue1', lwd = 3, bty = 'l',
axes = FALSE, xlab = '', ylab = ''))
abline(h = 0:6, lwd = .5)
with(tmp, points(x = V1, y = log10(stuff), type = 'l',
col = 'royalblue1', lwd = 3))
par(xpd = TRUE)
axis(2, at = 0:6, cex.axis = .6,
labels = format(10 ** (0:6), scientific = FALSE, big.mark = ','), las = 2)
x <- with(tmp, seq(min(V1), max(V1), length = 12))
text(x = x, y = -.5, cex = .8, labels = format(x, '%Y-%m'), srt = 45)
Run Code Online (Sandbox Code Playgroud)
为什么你想要匹配excel所做的事情超出我的范围,但也许这会让你开始
set.seed(1618)
tmp <- data.frame(date = seq(as.Date("2000-1-1"),
by = "month", length.out = 12),
stuff = sort(rpois(12, 5)) * 10000)
par(tcl = -.1, xpd = NA)
with(tmp,
plot(date, stuff, type = 'l', ylim = c(0, max(stuff)),
col = 'royalblue1', lwd = 3, bty = 'l',
axes = FALSE, xlab = '', ylab = ''))
axis(2, at = pretty(seq(0, max(tmp$stuff))),
labels = format(pretty(seq(0, max(tmp$stuff), length = 5)),
scientific = FALSE), las = 2)
text(x = tmp$date, y = -5000, labels = format(tmp$date, '%Y-%m'), srt = 45)
par(xpd = FALSE)
abline(h = pretty(seq(0, max(tmp$stuff))), lwd = .5)
Run Code Online (Sandbox Code Playgroud)

编辑
以下是如何自己休息并使用逗号 prettyNum
par(tcl = -.1, xpd = NA)
with(tmp,
plot(date, stuff, type = 'l', ylim = c(0, 100000),
col = 'royalblue1', lwd = 3, bty = 'l',
axes = FALSE, xlab = '', ylab = ''))
nums <- c(0,10000, 30000, 70000, 100000)
prettyNum(nums, big.mark = ',', scientific = FALSE)
axis(2, at = nums,
labels = prettyNum(nums, big.mark = ',', scientific = FALSE),
las = 2)
text(x = tmp$date, y = -10000, labels = format(tmp$date, '%Y-%m'), srt = 45)
par(xpd = FALSE)
abline(h = nums, lwd = .5)
Run Code Online (Sandbox Code Playgroud)

编辑
tmp <- read.csv('~/desktop/test.csv', stringsAsFactors = FALSE,
strip.white = TRUE, header = FALSE)
tmp$V1 <- paste0(tmp$V1, '-01')
tmp <- within(tmp, {
V1 <- as.Date(V1)
date <- format(as.Date(tmp$V1, '%Y-%m-%d'), '%Y-%m')
stuff <- V2
})
par(tcl = -.1, xpd = FALSE)
with(tmp,
plot(V1, log(stuff), type = 'n', ylim = c(0,6),
col = 'royalblue1', lwd = 3, bty = 'l',
axes = FALSE, xlab = '', ylab = ''))
abline(h = 0:6, lwd = .5)
with(tmp, points(x = V1, y = log10(stuff), type = 'l',
col = 'royalblue1', lwd = 3))
par(xpd = TRUE)
axis(2, at = 0:6, cex.axis = .6,
labels = format(10 ** (0:6), scientific = FALSE, big.mark = ','), las = 2)
x <- with(tmp, seq(min(V1), max(V1), length = 12))
text(x = x, y = -.5, cex = .8, labels = format(x, '%Y-%m'), srt = 45)
Run Code Online (Sandbox Code Playgroud)
