Add an exponential line in ggplot that goes through pre-specified points
The code below plots step functions of increasing accuracy toward the underlying polynomial or exponential curve. I am trying to add a curve to my plot that goes through the bottom of each step that I have added.
The commented lines are different attempts that I have tried, but nothing goes exactly through all of the bottom corners of each step down. Is anyone able to help me achieve this? Any help would be greatly appreciated.
library(ggplot2)
X1 <- seq(1, 5, by=0.25)
Y1 <- (0.74 * X^(-2)+0.25)*100
sm <- data.frame(X1, Y1)
X2 <- sort(rep(seq(1, 5, by=0.5), 2))[-18]
Y2 <- sort(rep(Y1[1:17 %% 2 == 1], 2), decreasing = T)[-18]
med <- data.frame(X1, Y2)
X3 <- sort(rep(seq(1,5), 4))[1:17]
Y3 <- sort(rep(Y1[c(1, 5, 9, 13, 17)], 4), decreasing = T)[1:17]
lg <- data.frame(X1, Y3)
ggplot() +
#stat_function(data=sm, mapping = aes(x = X), fun = function(x) exp(-1*x)*100+28) +
#geom_curve(aes(x=1, xend=5, y=99, yend=28), ncp = 17) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = y ~ poly(x,2), se=F, color= "black", fullrange=T) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = (y ~ exp(-1.9*x)), se=F, color= "black", fullrange=T) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=1, to=5, by=0.25), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = lg, aes(x=X1, y=Y3)) +
geom_step(colour = "purple", size = 1, data = med, aes(x=X1, y=Y2)) +
geom_step(colour = "orange", size = 1, data = sm, aes(x=X1, y=Y1)) +
theme_classic()
r ggplot2
add a comment |
The code below plots step functions of increasing accuracy toward the underlying polynomial or exponential curve. I am trying to add a curve to my plot that goes through the bottom of each step that I have added.
The commented lines are different attempts that I have tried, but nothing goes exactly through all of the bottom corners of each step down. Is anyone able to help me achieve this? Any help would be greatly appreciated.
library(ggplot2)
X1 <- seq(1, 5, by=0.25)
Y1 <- (0.74 * X^(-2)+0.25)*100
sm <- data.frame(X1, Y1)
X2 <- sort(rep(seq(1, 5, by=0.5), 2))[-18]
Y2 <- sort(rep(Y1[1:17 %% 2 == 1], 2), decreasing = T)[-18]
med <- data.frame(X1, Y2)
X3 <- sort(rep(seq(1,5), 4))[1:17]
Y3 <- sort(rep(Y1[c(1, 5, 9, 13, 17)], 4), decreasing = T)[1:17]
lg <- data.frame(X1, Y3)
ggplot() +
#stat_function(data=sm, mapping = aes(x = X), fun = function(x) exp(-1*x)*100+28) +
#geom_curve(aes(x=1, xend=5, y=99, yend=28), ncp = 17) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = y ~ poly(x,2), se=F, color= "black", fullrange=T) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = (y ~ exp(-1.9*x)), se=F, color= "black", fullrange=T) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=1, to=5, by=0.25), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = lg, aes(x=X1, y=Y3)) +
geom_step(colour = "purple", size = 1, data = med, aes(x=X1, y=Y2)) +
geom_step(colour = "orange", size = 1, data = sm, aes(x=X1, y=Y1)) +
theme_classic()
r ggplot2
add a comment |
The code below plots step functions of increasing accuracy toward the underlying polynomial or exponential curve. I am trying to add a curve to my plot that goes through the bottom of each step that I have added.
The commented lines are different attempts that I have tried, but nothing goes exactly through all of the bottom corners of each step down. Is anyone able to help me achieve this? Any help would be greatly appreciated.
library(ggplot2)
X1 <- seq(1, 5, by=0.25)
Y1 <- (0.74 * X^(-2)+0.25)*100
sm <- data.frame(X1, Y1)
X2 <- sort(rep(seq(1, 5, by=0.5), 2))[-18]
Y2 <- sort(rep(Y1[1:17 %% 2 == 1], 2), decreasing = T)[-18]
med <- data.frame(X1, Y2)
X3 <- sort(rep(seq(1,5), 4))[1:17]
Y3 <- sort(rep(Y1[c(1, 5, 9, 13, 17)], 4), decreasing = T)[1:17]
lg <- data.frame(X1, Y3)
ggplot() +
#stat_function(data=sm, mapping = aes(x = X), fun = function(x) exp(-1*x)*100+28) +
#geom_curve(aes(x=1, xend=5, y=99, yend=28), ncp = 17) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = y ~ poly(x,2), se=F, color= "black", fullrange=T) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = (y ~ exp(-1.9*x)), se=F, color= "black", fullrange=T) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=1, to=5, by=0.25), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = lg, aes(x=X1, y=Y3)) +
geom_step(colour = "purple", size = 1, data = med, aes(x=X1, y=Y2)) +
geom_step(colour = "orange", size = 1, data = sm, aes(x=X1, y=Y1)) +
theme_classic()
r ggplot2
The code below plots step functions of increasing accuracy toward the underlying polynomial or exponential curve. I am trying to add a curve to my plot that goes through the bottom of each step that I have added.
The commented lines are different attempts that I have tried, but nothing goes exactly through all of the bottom corners of each step down. Is anyone able to help me achieve this? Any help would be greatly appreciated.
library(ggplot2)
X1 <- seq(1, 5, by=0.25)
Y1 <- (0.74 * X^(-2)+0.25)*100
sm <- data.frame(X1, Y1)
X2 <- sort(rep(seq(1, 5, by=0.5), 2))[-18]
Y2 <- sort(rep(Y1[1:17 %% 2 == 1], 2), decreasing = T)[-18]
med <- data.frame(X1, Y2)
X3 <- sort(rep(seq(1,5), 4))[1:17]
Y3 <- sort(rep(Y1[c(1, 5, 9, 13, 17)], 4), decreasing = T)[1:17]
lg <- data.frame(X1, Y3)
ggplot() +
#stat_function(data=sm, mapping = aes(x = X), fun = function(x) exp(-1*x)*100+28) +
#geom_curve(aes(x=1, xend=5, y=99, yend=28), ncp = 17) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = y ~ poly(x,2), se=F, color= "black", fullrange=T) +
#geom_smooth(data = sm, aes(x=X1, y=Y1), method="lm", formula = (y ~ exp(-1.9*x)), se=F, color= "black", fullrange=T) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=1, to=5, by=0.25), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = lg, aes(x=X1, y=Y3)) +
geom_step(colour = "purple", size = 1, data = med, aes(x=X1, y=Y2)) +
geom_step(colour = "orange", size = 1, data = sm, aes(x=X1, y=Y1)) +
theme_classic()
r ggplot2
r ggplot2
asked Nov 13 '18 at 2:59
RABRAB
766116
766116
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I ended up re-doing the initial functions and made it all match up:
MyFunction <- function(x) 100*exp(-(1/4)*x)
Xyearly <- c(0:5)
Yyearly <- MyFunction(Xyearly)
Yearly <- data.frame(x=Xyearly, y=Yyearly)
X6monthly <- c(0:10/2)
Y6monthly <- MyFunction(X6monthly)
Month6 <- data.frame(x=X6monthly, y=Y6monthly)
X3monthly <- c(0:15/3)
Y3monthly <- MyFunction(X3monthly)
Month3 <- data.frame(x=X3monthly, y=Y3monthly)
X1monthly <- c(0:60/12)
Y1monthly <- MyFunction(X1monthly)
Month1 <- data.frame(x=X1monthly, y=Y1monthly)
ggplot() +
stat_function(data=data.frame(x = 0), mapping = aes(x = x), fun = MyFunction, size=1.2) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=0, to=5, by=0.5), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = Yearly, aes(x=x, y=y)) +
geom_step(colour = "purple", size = 1, data = Month6, aes(x=x, y=y)) +
geom_step(colour = "orange", size = 1, data = Month3, aes(x=x, y=y)) +
geom_step(colour = "limegreen", size = 1, data = Month1, aes(x=x, y=y))
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%2f53273151%2fadd-an-exponential-line-in-ggplot-that-goes-through-pre-specified-points%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
I ended up re-doing the initial functions and made it all match up:
MyFunction <- function(x) 100*exp(-(1/4)*x)
Xyearly <- c(0:5)
Yyearly <- MyFunction(Xyearly)
Yearly <- data.frame(x=Xyearly, y=Yyearly)
X6monthly <- c(0:10/2)
Y6monthly <- MyFunction(X6monthly)
Month6 <- data.frame(x=X6monthly, y=Y6monthly)
X3monthly <- c(0:15/3)
Y3monthly <- MyFunction(X3monthly)
Month3 <- data.frame(x=X3monthly, y=Y3monthly)
X1monthly <- c(0:60/12)
Y1monthly <- MyFunction(X1monthly)
Month1 <- data.frame(x=X1monthly, y=Y1monthly)
ggplot() +
stat_function(data=data.frame(x = 0), mapping = aes(x = x), fun = MyFunction, size=1.2) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=0, to=5, by=0.5), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = Yearly, aes(x=x, y=y)) +
geom_step(colour = "purple", size = 1, data = Month6, aes(x=x, y=y)) +
geom_step(colour = "orange", size = 1, data = Month3, aes(x=x, y=y)) +
geom_step(colour = "limegreen", size = 1, data = Month1, aes(x=x, y=y))
add a comment |
I ended up re-doing the initial functions and made it all match up:
MyFunction <- function(x) 100*exp(-(1/4)*x)
Xyearly <- c(0:5)
Yyearly <- MyFunction(Xyearly)
Yearly <- data.frame(x=Xyearly, y=Yyearly)
X6monthly <- c(0:10/2)
Y6monthly <- MyFunction(X6monthly)
Month6 <- data.frame(x=X6monthly, y=Y6monthly)
X3monthly <- c(0:15/3)
Y3monthly <- MyFunction(X3monthly)
Month3 <- data.frame(x=X3monthly, y=Y3monthly)
X1monthly <- c(0:60/12)
Y1monthly <- MyFunction(X1monthly)
Month1 <- data.frame(x=X1monthly, y=Y1monthly)
ggplot() +
stat_function(data=data.frame(x = 0), mapping = aes(x = x), fun = MyFunction, size=1.2) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=0, to=5, by=0.5), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = Yearly, aes(x=x, y=y)) +
geom_step(colour = "purple", size = 1, data = Month6, aes(x=x, y=y)) +
geom_step(colour = "orange", size = 1, data = Month3, aes(x=x, y=y)) +
geom_step(colour = "limegreen", size = 1, data = Month1, aes(x=x, y=y))
add a comment |
I ended up re-doing the initial functions and made it all match up:
MyFunction <- function(x) 100*exp(-(1/4)*x)
Xyearly <- c(0:5)
Yyearly <- MyFunction(Xyearly)
Yearly <- data.frame(x=Xyearly, y=Yyearly)
X6monthly <- c(0:10/2)
Y6monthly <- MyFunction(X6monthly)
Month6 <- data.frame(x=X6monthly, y=Y6monthly)
X3monthly <- c(0:15/3)
Y3monthly <- MyFunction(X3monthly)
Month3 <- data.frame(x=X3monthly, y=Y3monthly)
X1monthly <- c(0:60/12)
Y1monthly <- MyFunction(X1monthly)
Month1 <- data.frame(x=X1monthly, y=Y1monthly)
ggplot() +
stat_function(data=data.frame(x = 0), mapping = aes(x = x), fun = MyFunction, size=1.2) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=0, to=5, by=0.5), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = Yearly, aes(x=x, y=y)) +
geom_step(colour = "purple", size = 1, data = Month6, aes(x=x, y=y)) +
geom_step(colour = "orange", size = 1, data = Month3, aes(x=x, y=y)) +
geom_step(colour = "limegreen", size = 1, data = Month1, aes(x=x, y=y))
I ended up re-doing the initial functions and made it all match up:
MyFunction <- function(x) 100*exp(-(1/4)*x)
Xyearly <- c(0:5)
Yyearly <- MyFunction(Xyearly)
Yearly <- data.frame(x=Xyearly, y=Yyearly)
X6monthly <- c(0:10/2)
Y6monthly <- MyFunction(X6monthly)
Month6 <- data.frame(x=X6monthly, y=Y6monthly)
X3monthly <- c(0:15/3)
Y3monthly <- MyFunction(X3monthly)
Month3 <- data.frame(x=X3monthly, y=Y3monthly)
X1monthly <- c(0:60/12)
Y1monthly <- MyFunction(X1monthly)
Month1 <- data.frame(x=X1monthly, y=Y1monthly)
ggplot() +
stat_function(data=data.frame(x = 0), mapping = aes(x = x), fun = MyFunction, size=1.2) +
scale_y_continuous(name="Overall Survival (%)", limits=c(0, 100)) +
scale_x_continuous(breaks = seq(from=0, to=5, by=0.5), name = "Survival Time (years)") +
geom_step(colour = "red", size = 1, data = Yearly, aes(x=x, y=y)) +
geom_step(colour = "purple", size = 1, data = Month6, aes(x=x, y=y)) +
geom_step(colour = "orange", size = 1, data = Month3, aes(x=x, y=y)) +
geom_step(colour = "limegreen", size = 1, data = Month1, aes(x=x, y=y))
answered Nov 20 '18 at 3:27
RABRAB
766116
766116
add a comment |
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%2f53273151%2fadd-an-exponential-line-in-ggplot-that-goes-through-pre-specified-points%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