y轴的ggplot2对数刻度导致曲线 [英] ggplot2 log scale of y axis causing curved lines

查看:490
本文介绍了y轴的ggplot2对数刻度导致曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一些数据作为随着时间的推移组的方式,用各条线连接每组的不同平均时间点。



这个代码是:

  line< -ggplot( dat,aes(Time,Cortisol.ngmL,shape = T))

line +
stat_summary(fun.y = mean,geom =point,size = 4,aes(group = T))+
stat_summary(fun.y = mean,geom =line,aes(group = T),linetype =dashed,lwd = 0.7)

但是...我想记录y轴(log10)。当我这样做时,跨越时间的连线组变成曲线(下面的代码)

  line <-ggplot(dat,aes (时间,Cortisol.ngmL,shape = T))

line +
stat_summary(fun.y = mean,geom =point,size = 4,aes(group = T)) +
stat_summary(fun.y = mean,geom =line,aes(group = T),linetype =dashed,lwd = 0.7)+
coord_trans(y =log10)

有谁知道一种方法,我可以有一个对数刻度和直线?

解决方案

我使用这个函数在日志范围内用直线连接点:$ b​​
$ b

  log_line < - 函数(x,y,n = 1000){
l < - lapply(2:length(x),
function(i,n){$ b (x [i-1],x [i],(x [i] -x [i-1])/ n)
yl <-exp(log(y [ ())/(x [i] - x [i-1]))$ b $(x [i] b返回(data.frame(x = xl,y = yl))
},
n)

return(do.call(rbind,l))
}

这些参数是您希望用对数刻度中的直线连接的点的x和y坐标,以及您希望在每对原始点之间预测的n个点。



这个函数拟合每个点之间的对数刻度的直线,预测两个原始点之间的n个新点,并将它们转换回原始刻度。输出是一个带有预测值x和y坐标的数据帧。

它可以很容易地添加到ggplot中:

  v1 < -  1:10 
v2 < - exp(-v1)

ggplot()+
geom_point (aes(v1,v2))+
geom_line(aes(x,y),data = log_line(v1,v2))+
coord_trans(y =log)


I’d like to graph some data as means of groups over time, with lines connecting the different mean time points for each group.

The code for this is:

line<-ggplot(dat, aes(Time, Cortisol.ngmL, shape=T))

line+
stat_summary(fun.y=mean, geom="point", size=4, aes(group=T))+
stat_summary(fun.y=mean, geom="line", aes(group=T), linetype="dashed", lwd=0.7)

But…I want the y axis logged (log10). And when I do this the lines connecting the groups across time become curved (code below)

line<-ggplot(dat, aes(Time, Cortisol.ngmL, shape=T))

line+
stat_summary(fun.y=mean, geom="point", size=4, aes(group=T))+
stat_summary(fun.y=mean, geom="line", aes(group=T), linetype="dashed", lwd=0.7)+
coord_trans(y="log10")

Does anyone know a way I can have a log scale and straight lines?

解决方案

I use this function to connect points with straight lines in log scale:

log_line <- function(x, y, n = 1000) {
  l <- lapply(2:length(x),
              function(i, n) {
                xl <- seq(x[i - 1], x[i], (x[i] - x[i - 1]) / n)
                yl <- exp(log(y[i]) + (xl - x[i]) * (log(y[i]) - log(y[i - 1])) / (x[i] - x[i - 1]))
                return(data.frame(x = xl, y = yl))
              },
              n)

  return(do.call(rbind, l))
}

The arguments are the x and y coordinates of the points you want to connect with a straight line in log scale and the n number of points you want to predict between each pair of original points.

This function fits a straight line in log scale between each points, predicts n new points between the two original points and than converts them back to the original scale. The output is a data frame with the predicted values x and y coordinates.

It can be easily added to a ggplot:

v1 <- 1:10
v2 <- exp(-v1)

ggplot() +
  geom_point(aes(v1, v2)) +
  geom_line(aes(x, y), data = log_line(v1, v2)) +
  coord_trans(y = "log")

这篇关于y轴的ggplot2对数刻度导致曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆