2016-07-14 116 views
0

我有一个数据框,其中一个因变量(y)与自变量x1具有〜对数线性关系,而与自变量x2具有〜sigmoidal关系。带有一个对数坐标轴的热图

df<-data.frame(x1 = rep(c(0:10),11), 
      x2 = rep(c(0:10), each=11), 
      logx1 = log(rep(c(0:10),11)+1), 
      y = 0) 

for(i in 1:nrow(df)) df[i,4] = exp(df[i,2]) * (1/(1+exp(-df[i,1]))) 

我想用一个热图,以显示与X1对于变化和x2 y中的变化:

ggplot(df, aes(x=x1, y=x2, fill=y))+ 
theme_bw()+ 
scale_fill_distiller(palette = "Spectral")+ 
geom_tile(size=0.01) 

但很难看到关于X1 y的变化,所以我要绘制对数尺度X1:

ggplot(df, aes(x=logx1, y=x2, fill=y))+ 
theme_bw()+ 
scale_fill_distiller(palette = "Spectral")+ 
geom_tile(size=0.01)+ 

,但它导致了数据之间的较大的空间,而不是颜色的连续表面: gaps in heat map

我已经试过:

转换x1到一个因素,密谋:

df$x1<-factor(df$x1, levels = sort(unique(df$x1))) 

策划与插值栅格:

geom_raster(interpolate = TRUE 

改造轴本身:

scale_x_continuous(breaks = c(0,1,10)+1, limits = c(0,10)+1, trans = 'log') 

chan使用coord_equal()和coord_fixed()来绘制阴谋大小,

增加数据的分辨率(即,估计每0.001变化x1)

但我不能让空间走开!

我不一定卖的热图上,但我需要在y以显示变化相对于X1和X2两者,我需要做的是在ggplot

回答

1

如果y ~ exp(x)在你的榜样,那么将y(不是x)放在日志范围内可能是更好的显示其关系的方法。例如。

ggplot(df, aes(x=x1, y=x2, fill=log(y))) + 
    theme_bw() + 
    scale_fill_distiller(palette = "Spectral") + 
    geom_tile(size=0.01) 

heatmap 1 或者你可以显示在指数缩放X(注意,在你的榜样,它实际上是X2具有与为y的对数线性关系,X1 S形)。如果要在指数刻度上显示带x2的连续曲面热图,则需要指数刻度上等间距的x2的点。获得等分点的一种方法是pretty()函数。例如。

# generate new data with x2 equally space on exp scale 
newdat <- expand.grid(x1 = pretty(df$x1, 10), exp_x2 = pretty(exp(df$x2), 10)) 

# backtransform exp_x2 
newdat$x2 <- log(newdat$exp_x2) 

# generate y values (using raw x2 values, not exp_x2) 
newdat$y <- exp(newdat$x2) * (1/(1+exp(-newdat$x1))) 

# plot  
ggplot(newdat, aes(x=x1, y=exp_x2, fill=y)) + 
    theme_bw() + 
    scale_fill_distiller(palette = "Spectral") + 
    geom_tile(size=0.01) 

heatmap 2

+0

在对数轴绘制ý将是不适合的实际数据我有,但肯定是在这个例子中(一个MWE)一个很好的解决方案。 我确实使用类似于你的第二个帖子的东西,它运作良好,谢谢! – cmhoove14