【发布时间】:2014-07-09 15:35:50
【问题描述】:
我有一个约 1.08 亿行数据的数据框,共 7 列。我使用这个 R 脚本来制作它的箱线图:
ggplot(expanded_results, aes(factor(hour), dynamic_nox)) +
geom_boxplot(fill="#6699FF", outlier.size = 0.5, lwd=.1) +
scale_y_log10() +
stat_summary(fun.y=mean, geom="line", aes(group=1, colour="red")) +
ylab(expression(Exposure~to~NO[x])) +
xlab(expression(Hour~of~the~day)) +
ggtitle("Hourly exposure to NOx") +
theme(axis.text=element_text(size=12, colour="black"),
axis.title=element_text(size=12, colour="black"),
plot.title=element_text(size=12, colour="black"),
legend.position="none")
图表如下所示。这非常好,但是最好在 Y 轴的顶部有一个值。我想它应该是 1000,因为 Y 轴是 log10 比例。不过我不知道该怎么做?
有什么想法吗?
编辑:回应 DrDom:
尝试添加scale_y_log10(breaks=c(0,10,100,1000))。这样做的输出是这样的:
执行以下操作的输出:
scale_y_log10(breaks=c(0,10,100,1000), limits=c(0,1000))
是一个错误:
Error in seq.default(dots[[1L]][[1L]], dots[[2L]][[1L]], length = dots[[3L]][[1L]]:
'from' cannot be NA, NaN or infinite
作为对 Jaap 的回应,他提出了以下代码:
library(ggplot2)
library(scales)
ggplot(expanded_results, aes(factor(hour), dynamic_nox)) +
geom_boxplot(fill="#6699FF", outlier.size = 0.5, lwd=.1) +
stat_summary(fun.y=mean, geom="line", aes(group=1, colour="red")) +
scale_y_continuous(breaks=c(0,10,100,1000,3000), trans="log1p") +
labs(title="Hourly exposure to NOx", x=expression(Hour~of~the~day), y=expression(Exposure~to~NO[x])) +
theme(axis.text=element_text(size=12, colour="black"), axis.title=element_text(size=12, colour="black"),
plot.title=element_text(size=12, colour="black"), legend.position="none")
它产生这个图表。我做错了什么吗?我仍然缺少“1000”刻度标签?考虑到大部分数据都在哪里,在 10 和 100 之间打勾也不错?
【问题讨论】:
-
尝试添加
scale_y_log10(breaks=c(0,10,100,1000))或scale_y_log10(breaks=c(0,10,100,1000), limits=c(0,1000)) -
您好 DrDom。感谢您的建议。结果已添加到我上面的帖子中。请注意,数据看起来有点不同,因为再次运行图形创建大约需要 20 分钟,所以我只是使用数据的一个子集来进行演示。还没有。 :-(
-
我写了一个自动执行此操作的函数:stackoverflow.com/a/54325289/3082472