本文介绍了使用ggplot2仅将一个轴转换为log10刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下问题:我想在boxplot中显示一个离散变量和一个连续变量,其中后者有几个极高的值。这使得boxplot变得毫无意义(图表的点和甚至body太小),这就是为什么我想以log10的比例显示它的原因。我知道我可以忽略可视化中的极端值,但我并不打算这样做。 让我们看一个简单的钻石数据示例: m 这里的问题并不严重,但我希望你能想象为什么我想以log10的比例看到这些值。让我们试试看: m + geom_boxplot()+ coord_trans(y =log10) 正如你所看到的y轴是log10缩放并且看起来很好,但是x轴有一个问题,这使得情节非常奇怪。 scale_log 不会发生这个问题,但这不是我的选择,因为我无法使用这种方式的自定义格式化程序。例如: m + geom_boxplot()+ scale_y_log10() 我的问题:有没有人知道一个解决方案,在y轴上绘制log10刻度的boxplot,这些标签可以用 formatter 函数自由格式化,就像这个线程? 根据回答和评论编辑问题以帮助回复者: 我真正追求的是:一个log10转换轴(y),没有科学标签。我想标记它像美元(formatter =美元)或任何自定义格式。 如果我尝试@哈德利的建议我得到以下警告: > m + geom_boxplot()+ scale_y_log10(formatter = dollar)警告消息: 1:在max(x)中:返回-Inf 2:在max(x)中:没有非缺少参数为max;返回-Inf 3:在max(x)中:没有非缺少参数到max;返回-Inf 使用不变的y轴标签: 解决方案 最简单的方法就是给formatter参数指定日志函数的名称: m + geom_boxplot ()+ scale_y_continuous(formatter ='log10') 编辑:不喜欢那样的话,其中任何一个看起来都会给出相同的结果: m m EDIT2& 3:进一步的实验(放弃试图成功地在记录值前放置$符号的实验): fmtExpLg10 ggplot(diamonds,aes(color,log10(price) ))+ geom_boxplot()+ scale_y_continuous(Price,log10-scaling,formatter = fmtExpLg10) 注意在2017年中添加了有关程序包语法更改的评论: $ b scale_y_continuous(formatter ='log10')现在是scale_y_continuous(trans ='log10 ')(ggplot2 v2.2.1) I have the following problem: I would like to visualize a discrete and a continuous variable on a boxplot in which the latter has a few extreme high values. This makes the boxplot meaningless (the points and even the "body" of the chart is too small), that is why I would like to show this on a log10 scale. I am aware that I could leave out the extreme values from the visualization, but I am not intended to.Let's see a simple example with diamonds data:m <- ggplot(diamonds, aes(y = price, x = color))The problem is not serious here, but I hope you could imagine why I would like to see the values at a log10 scale. Let's try it:m + geom_boxplot() + coord_trans(y = "log10")As you can see the y axis is log10 scaled and looks fine but there is a problem with the x axis, which makes the plot very strange.The problem do not occur with scale_log, but this is not an option for me, as I cannot use a custom formatter this way. E.g.:m + geom_boxplot() + scale_y_log10() My question: does anyone know a solution to plot the boxplot with log10 scale on y axis which labels could be freely formatted with a formatter function like in this thread? Editing the question to help answerers based on answers and comments:What I am really after: one log10 transformed axis (y) with not scientific labels. I would like to label it like dollar (formatter=dollar) or any custom format.If I try @hadley's suggestion I get the following warnings:> m + geom_boxplot() + scale_y_log10(formatter=dollar)Warning messages:1: In max(x) : no non-missing arguments to max; returning -Inf2: In max(x) : no non-missing arguments to max; returning -Inf3: In max(x) : no non-missing arguments to max; returning -InfWith an unchanged y axis labels: 解决方案 The simplest is to just give the formatter argument the name of the log function:m + geom_boxplot() + scale_y_continuous(formatter='log10')EDIT:Or if you don't like that then either of these appears to give the same result:m <- ggplot(diamonds, aes(y = price, x = color), log="y"); m + geom_boxplot() m <- ggplot(diamonds, aes(y = price, x = color), log10="y"); m + geom_boxplot()EDIT2 & 3:Further experiments (after discarding the one that attempted successfully to put "$" signs in front of logged values):fmtExpLg10 <- function(x) paste(round_any(10^x/1000, 0.01) , "K $", sep="")ggplot(diamonds, aes(color, log10(price))) + geom_boxplot() + scale_y_continuous("Price, log10-scaling", formatter = fmtExpLg10)Note added mid 2017 in comment about package syntax change: scale_y_continuous(formatter = 'log10') is now scale_y_continuous(trans = 'log10') (ggplot2 v2.2.1) 这篇关于使用ggplot2仅将一个轴转换为log10刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 08:13