Reputation: 53
I've got some coordinates: x and y. I use them to plot some points. Then I draw a regression line with all those points. No problem up here.
The problem is that I want to have a semi-log scale, so I set the y-axes on Log. I know that the regression line is not going to be straight because of the log. But then I also give the line some Log data. (without the log everything works) The code at that point :
require(stats)
library(ggplot2)
x <- c(2,2.5,2.8,3.1,3.2,3.6,4.2,4.6,4.8,5,5.5,6.2,6.9,8)
y <- c(1.8,2.3,2.6,2.9,3.5,3.9,4.2,4.5,4.6,5,5.1,5.3,6.2,7.3)
ggplot(data=NULL , aes(x=x, y=log(y))) +
coord_trans(y = "log10")+
geom_point(size=2.5, shape=20) +
geom_smooth(method=lm, formula= log(y)~x , se=FALSE, colour="black")
This code shows this graph :
The problem is that I want the line to "follow" the path of the points (not to be at the bottom) and to be straight.
It worked with another method but now with ggplot, it doesn't work.
Thank You
Upvotes: 1
Views: 880
Reputation: 1601
Edited after clarification from the question author
It sounds like you want scale_y_log10()
instead of coord_trans(y = "log10")
. Per the documentation of coord_trans
:
The difference between transforming the scales and transforming the coordinate system is that scale transformation occurs BEFORE statistics, and coordinate transformation afterwards. Coordinate transformation also changes the shape of geoms
To keep a straight line use scale_y_log10()
like so:
require(stats)
library(ggplot2)
x <- c(1,2,2,2,3,3,4,5,5,6,6,6,6,7,8,9,9,11,11,11,15,15,16,16,16,16,17,18,18,18)
y <- c(1,2,4,3,3,2,5,4,6,3,7,7,6,7,8,4,5,4,9,9,13,14,15,15,15,16,17,19,19,20)
ggplot(data=NULL , aes(x=x, y=y)) +
geom_point(size=2.5, shape=20) +
geom_smooth(method=lm, formula= y~x, se=FALSE, colour="black") +
scale_y_log10()
Upvotes: 2
Reputation: 53
I previously used this code : (I had to change for some aesthetic reasons)
require(stats)
x <- c(1,2,2,2,3,3,4,5,5,6,6,6,6,7,8,9,9,11,11,11,15,15,16,16,16,16,17,18,18,18)
y <- c(1,2,4,3,3,2,5,4,6,3,7,7,6,7,8,4,5,4,9,9,13,14,15,15,15,16,17,19,19,20)
plot(x,y, log="y", col="blue", pch=20,xlim=c(min(x), max(x)+2), ylim=c(min(y), max(y)+2))
mod1 <-lm(log10(y) ~ x)
abline(mod1, lwd=1, col="blue")
It gave me this (The scale was semi-log with a straight line):
Upvotes: 1