2014-07-09 21 views
3

我有一个大约108m行数据的数据框,分为7列。我用这个R脚本来制作一个盒子图:使用ggplot2 log10刻度上的小勾号

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比例。我不知道如何做到这一点,虽然?

enter image description here

任何想法吗?

编辑:回复DrDom: 尝试添加scale_y_log10(breaks=c(0,10,100,1000))。这样做的输出,是这样的:

enter image description here

做的输出如下: 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 

在respnonse到夏侯谁建议以下代码:

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之间的勾号也会很好?

enter image description here

+0

尝试添加'scale_y_log10(breaks = c(0,10,100,1000))'或'scale_y_log10(breaks = c(0,10,100,1000),limits = c(0,1000))' – DrDom

+0

Hi DrDom。感谢您的建议。结果添加到我的帖子上面。请注意,数据看起来有点不同,因为再次运行图形创建需要大约20分钟,所以我只是使用了一部分数据用于演示目的。还不完全。 :-( – TheRealJimShady

回答

3

而不是使用scale_y_log10的你也可以连同从scales包数变换使用scale_y_continuous。当您使用log1p改造,你也能在您休息一个0:然后scale_y_continuous(breaks=c(0,1,3,10,30,100,300,1000,3000), trans="log1p")

你完整的代码看起来就像这样(请注意,我也labs合并标题参数):

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,1,3,10,30,100,300,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") 
+0

我认为它应该阅读的 反=“log1p” 反=“LOG10” 代替 你能澄清? – TheRealJimShady

+0

@TheRealJimShady可以同时使用。然而,当你'dynamic_nox'有值0'时,这些观察值将被排除,'log1p'将给该值加'1',然后计算对数值,这样'0'值也会包含在内。 ](HTTP://计算器。com/questions/24646594/how-to-improve-the-aspect-of-ggplot-histograms-with-log-scales-and-discrete-valu/2464952220) – Jaap

+0

Hi Jaap。再次感谢您的指导。我在上面的问题中提出了你的建议的输出。你能看一下吗?我错过了一些刻度标记。不知道该怎么办?谢谢。 – TheRealJimShady

4

您可以通过添加参数breaks=scale_y_log10()修改日志的规模,只有在那里不应该因为这些值也日志被计算为0值。

df<-data.frame(x=1:10000,y=1:10000) 
ggplot(df,aes(x,y))+geom_line()+ 
     scale_y_log10(breaks=c(1,5,10,85,300,5000)) 
相关问题