更改上限scale_fill_gradient2
也会影响值<0的颜色分类,因为0周围的颜色分布似乎总是对称的,是否有办法获得颜色值的不对称分布?
这是一个使用的情节的最小例子geom_tile()
:
data <- read.csv("http://protzkeule.de/data.csv")
p <- ggplot(data = data, aes(x = variable, y = meas)) + geom_tile(aes(fill = value))
Run Code Online (Sandbox Code Playgroud)
具有对称限制的图:
p + scale_fill_gradient2(low = "blue", mid = "white", high = "red", guide = "colorbar",
limits = c(-0.1, 0.1))
Run Code Online (Sandbox Code Playgroud)
但是当更改上限时,较低的颜色映射也会改变(观察颜色条):
p + scale_fill_gradient2(low = "blue", mid = "white", high = "red", guide = "colorbar",
limits = c(-0.1, 0.3))
Run Code Online (Sandbox Code Playgroud) 我终于设法将我的自定义拟合函数绘制在ggplot2中的数据上,但是当我对x轴进行对数转换时,绘制的函数完全搞砸了.它看起来scale_x_log10()
只适用于绘制的数据,但不适用于函数.
如何使功能以正确的比例显示?
以下是Hadley的stat_function()文档的修改示例:
x <- rnorm(100)
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red")
Run Code Online (Sandbox Code Playgroud)
现在使用log10 x轴:
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red") + scale_x_log10()
Run Code Online (Sandbox Code Playgroud)
好吧,我认为我的例子不是很有用所以我尝试的方式不同:
基本上我想要的是重现我用曲线()做的情节.我在我的数据中安装了Hill函数,现在想要绘制它:
# the function
HillFunction <- function(ec50,hill,rmax,x) {rmax/(1+(ec50/x)^hill)}
# fitted parameters
hill.args <- list(ec50=10^-2, hill=.7, rmax=1)
curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5,log="x")
Run Code Online (Sandbox Code Playgroud)
所以curve()给我一个平滑的S形曲线,如预期的那样.现在我尝试用ggplot重现相同的情节:
我添加一些数据从10 ^ -5到10 ^ 5只是为了定义绘图范围,不确定是否有更好的方法
p <- ggplot(data=data.frame(x=c(10^-5:10^5)), aes(x=x)) + stat_function(fun=HillFunction, args=hill.args, n=3000, color="red")
Run Code Online (Sandbox Code Playgroud)
现在,如果我绘制p
一切看起来很好,就像curve()
没有logscale 的情节:
p
curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5)
Run Code Online (Sandbox Code Playgroud)
如果我变换坐标系,我得到一个S形曲线,但根本不光滑,曲线看起来很陡,但可能来自x缩放:
p + coord_trans(x ="log10")
如果我将x刻度定义为对数刻度,则绘图看起来很平滑但在10 ^ 0处停止: …