How to add trend line in a log-log plot (ggplot2)?
I need plot a data vector, which follow power law distribution. so if I plot them on log-log axis, and they will be a straight line.
However, if I do not explicitly provide "y" parameter, I do not know how to plot.
this is code
library("poweRlaw")
library("ggplot2")
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000])
#make plot with both axes log scale
ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
geom_smooth(stat = 'bin',mapping = aes(x=con_rns),method = "lm",se=FALSE)+
scale_x_log10() +
scale_y_log10()
The result is this:

But I want this

I know, I can manually bin data, provide "y" explicitly and then plot the line, like this
ggplot(data = data.frame(a = rnorm(50,0,1),b=5+rnorm(50,2,1)),mapping = aes(x = a,y=b))+
geom_point()+
geom_smooth(method = "lm",se=FALSE)
result:

But I want to know, how can I plot trend line with this code (geom_point(stat = 'bin', binwidth = 0.1)). It implicitly calculates data bin.
PS:
Well, thanks for Chris's answer. I still have a problem. If I want to plot different group, how can I draw it? The data are df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis? like this:

r ggplot2
add a comment |
I need plot a data vector, which follow power law distribution. so if I plot them on log-log axis, and they will be a straight line.
However, if I do not explicitly provide "y" parameter, I do not know how to plot.
this is code
library("poweRlaw")
library("ggplot2")
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000])
#make plot with both axes log scale
ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
geom_smooth(stat = 'bin',mapping = aes(x=con_rns),method = "lm",se=FALSE)+
scale_x_log10() +
scale_y_log10()
The result is this:

But I want this

I know, I can manually bin data, provide "y" explicitly and then plot the line, like this
ggplot(data = data.frame(a = rnorm(50,0,1),b=5+rnorm(50,2,1)),mapping = aes(x = a,y=b))+
geom_point()+
geom_smooth(method = "lm",se=FALSE)
result:

But I want to know, how can I plot trend line with this code (geom_point(stat = 'bin', binwidth = 0.1)). It implicitly calculates data bin.
PS:
Well, thanks for Chris's answer. I still have a problem. If I want to plot different group, how can I draw it? The data are df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis? like this:

r ggplot2
add a comment |
I need plot a data vector, which follow power law distribution. so if I plot them on log-log axis, and they will be a straight line.
However, if I do not explicitly provide "y" parameter, I do not know how to plot.
this is code
library("poweRlaw")
library("ggplot2")
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000])
#make plot with both axes log scale
ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
geom_smooth(stat = 'bin',mapping = aes(x=con_rns),method = "lm",se=FALSE)+
scale_x_log10() +
scale_y_log10()
The result is this:

But I want this

I know, I can manually bin data, provide "y" explicitly and then plot the line, like this
ggplot(data = data.frame(a = rnorm(50,0,1),b=5+rnorm(50,2,1)),mapping = aes(x = a,y=b))+
geom_point()+
geom_smooth(method = "lm",se=FALSE)
result:

But I want to know, how can I plot trend line with this code (geom_point(stat = 'bin', binwidth = 0.1)). It implicitly calculates data bin.
PS:
Well, thanks for Chris's answer. I still have a problem. If I want to plot different group, how can I draw it? The data are df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis? like this:

r ggplot2
I need plot a data vector, which follow power law distribution. so if I plot them on log-log axis, and they will be a straight line.
However, if I do not explicitly provide "y" parameter, I do not know how to plot.
this is code
library("poweRlaw")
library("ggplot2")
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000])
#make plot with both axes log scale
ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
geom_smooth(stat = 'bin',mapping = aes(x=con_rns),method = "lm",se=FALSE)+
scale_x_log10() +
scale_y_log10()
The result is this:

But I want this

I know, I can manually bin data, provide "y" explicitly and then plot the line, like this
ggplot(data = data.frame(a = rnorm(50,0,1),b=5+rnorm(50,2,1)),mapping = aes(x = a,y=b))+
geom_point()+
geom_smooth(method = "lm",se=FALSE)
result:

But I want to know, how can I plot trend line with this code (geom_point(stat = 'bin', binwidth = 0.1)). It implicitly calculates data bin.
PS:
Well, thanks for Chris's answer. I still have a problem. If I want to plot different group, how can I draw it? The data are df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis? like this:

r ggplot2
r ggplot2
edited Nov 19 '18 at 1:47
BigMOoO
asked Nov 18 '18 at 12:15
BigMOoOBigMOoO
789
789
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
One way would be to recover the binned data from the plot using ggplot_build()
first I made the plot without the line of best fit:
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
scale_x_log10() +
scale_y_log10()
Then I added the binned data from the plot which can be found with ggplot_build(p)$data (and reversed the log10 transformation)
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y),method = "lm",se=FALSE)

UPDATE:
The additional problem was how to split the plot by different colour groups. I approached this in the same way but it was necessary for me to create a 'group' aesthetic so this data could be kept in the ggplot_build data.
library(poweRlaw)
library(ggplot2)
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T))
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1, aes(colour=factor(col), group=factor(col)))+
scale_x_log10() +
scale_y_log10()
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y, colour=factor(group)),method = "lm",se=FALSE)
Note that now we have grouped the data, some of the groups have a count of zero in their bin. This returns a warning when the log10 transformation is applied to zero, giving an infinite value. These points are removed from the plot and ignored in the trend lines.

very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
I think the process would be the same if you created a new plot withy=..density..but I'm not completely sure I understand the question. Perhaps make a new question?
– Chris
Nov 18 '18 at 16:19
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are 'df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)). How can I plot different color point group and color line group in log-log axis?
– BigMOoO
Nov 19 '18 at 0:23
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360743%2fhow-to-add-trend-line-in-a-log-log-plot-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way would be to recover the binned data from the plot using ggplot_build()
first I made the plot without the line of best fit:
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
scale_x_log10() +
scale_y_log10()
Then I added the binned data from the plot which can be found with ggplot_build(p)$data (and reversed the log10 transformation)
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y),method = "lm",se=FALSE)

UPDATE:
The additional problem was how to split the plot by different colour groups. I approached this in the same way but it was necessary for me to create a 'group' aesthetic so this data could be kept in the ggplot_build data.
library(poweRlaw)
library(ggplot2)
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T))
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1, aes(colour=factor(col), group=factor(col)))+
scale_x_log10() +
scale_y_log10()
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y, colour=factor(group)),method = "lm",se=FALSE)
Note that now we have grouped the data, some of the groups have a count of zero in their bin. This returns a warning when the log10 transformation is applied to zero, giving an infinite value. These points are removed from the plot and ignored in the trend lines.

very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
I think the process would be the same if you created a new plot withy=..density..but I'm not completely sure I understand the question. Perhaps make a new question?
– Chris
Nov 18 '18 at 16:19
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are 'df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)). How can I plot different color point group and color line group in log-log axis?
– BigMOoO
Nov 19 '18 at 0:23
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
add a comment |
One way would be to recover the binned data from the plot using ggplot_build()
first I made the plot without the line of best fit:
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
scale_x_log10() +
scale_y_log10()
Then I added the binned data from the plot which can be found with ggplot_build(p)$data (and reversed the log10 transformation)
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y),method = "lm",se=FALSE)

UPDATE:
The additional problem was how to split the plot by different colour groups. I approached this in the same way but it was necessary for me to create a 'group' aesthetic so this data could be kept in the ggplot_build data.
library(poweRlaw)
library(ggplot2)
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T))
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1, aes(colour=factor(col), group=factor(col)))+
scale_x_log10() +
scale_y_log10()
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y, colour=factor(group)),method = "lm",se=FALSE)
Note that now we have grouped the data, some of the groups have a count of zero in their bin. This returns a warning when the log10 transformation is applied to zero, giving an infinite value. These points are removed from the plot and ignored in the trend lines.

very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
I think the process would be the same if you created a new plot withy=..density..but I'm not completely sure I understand the question. Perhaps make a new question?
– Chris
Nov 18 '18 at 16:19
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are 'df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)). How can I plot different color point group and color line group in log-log axis?
– BigMOoO
Nov 19 '18 at 0:23
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
add a comment |
One way would be to recover the binned data from the plot using ggplot_build()
first I made the plot without the line of best fit:
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
scale_x_log10() +
scale_y_log10()
Then I added the binned data from the plot which can be found with ggplot_build(p)$data (and reversed the log10 transformation)
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y),method = "lm",se=FALSE)

UPDATE:
The additional problem was how to split the plot by different colour groups. I approached this in the same way but it was necessary for me to create a 'group' aesthetic so this data could be kept in the ggplot_build data.
library(poweRlaw)
library(ggplot2)
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T))
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1, aes(colour=factor(col), group=factor(col)))+
scale_x_log10() +
scale_y_log10()
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y, colour=factor(group)),method = "lm",se=FALSE)
Note that now we have grouped the data, some of the groups have a count of zero in their bin. This returns a warning when the log10 transformation is applied to zero, giving an infinite value. These points are removed from the plot and ignored in the trend lines.

One way would be to recover the binned data from the plot using ggplot_build()
first I made the plot without the line of best fit:
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
scale_x_log10() +
scale_y_log10()
Then I added the binned data from the plot which can be found with ggplot_build(p)$data (and reversed the log10 transformation)
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y),method = "lm",se=FALSE)

UPDATE:
The additional problem was how to split the plot by different colour groups. I approached this in the same way but it was necessary for me to create a 'group' aesthetic so this data could be kept in the ggplot_build data.
library(poweRlaw)
library(ggplot2)
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T))
p <- ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1, aes(colour=factor(col), group=factor(col)))+
scale_x_log10() +
scale_y_log10()
p + geom_smooth(data = ggplot_build(p)$data[[1]],
mapping = aes(x=10^x, y= 10^y, colour=factor(group)),method = "lm",se=FALSE)
Note that now we have grouped the data, some of the groups have a count of zero in their bin. This returns a warning when the log10 transformation is applied to zero, giving an infinite value. These points are removed from the plot and ignored in the trend lines.

edited Nov 19 '18 at 14:06
answered Nov 18 '18 at 13:19
Chris Chris
1,096614
1,096614
very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
I think the process would be the same if you created a new plot withy=..density..but I'm not completely sure I understand the question. Perhaps make a new question?
– Chris
Nov 18 '18 at 16:19
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are 'df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)). How can I plot different color point group and color line group in log-log axis?
– BigMOoO
Nov 19 '18 at 0:23
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
add a comment |
very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
I think the process would be the same if you created a new plot withy=..density..but I'm not completely sure I understand the question. Perhaps make a new question?
– Chris
Nov 18 '18 at 16:19
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are 'df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)). How can I plot different color point group and color line group in log-log axis?
– BigMOoO
Nov 19 '18 at 0:23
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
very good, thank you. I also want to know some implicit variable.such as, using "..density.." shift geom_bar to geom_histogram.. ggplot calculate some variable, like "..density.." . how can I get these variable in statistical transform of ggplot?
– BigMOoO
Nov 18 '18 at 15:05
I think the process would be the same if you created a new plot with
y=..density.. but I'm not completely sure I understand the question. Perhaps make a new question?– Chris
Nov 18 '18 at 16:19
I think the process would be the same if you created a new plot with
y=..density.. but I'm not completely sure I understand the question. Perhaps make a new question?– Chris
Nov 18 '18 at 16:19
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are '
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis?– BigMOoO
Nov 19 '18 at 0:23
Well, I may consider another question. But if I want to plot different group, how can I draw it? The data are '
df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis?– BigMOoO
Nov 19 '18 at 0:23
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
@BigMOoO see my update
– Chris
Nov 19 '18 at 14:06
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
Thank you very much~!!!!very good answer~!!!!
– BigMOoO
Nov 20 '18 at 13:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360743%2fhow-to-add-trend-line-in-a-log-log-plot-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown