Adding point to a facetHow to add different lines for facetsAdd a segment only to one facet using ggplot2Month-Year bar graph plot faceted and filled on year; with data input as date in char formatAdding R^2 on graph with facetsScaling data in R data frame and fitting gaussian to geom_pointggplot2: facet_wrap strip color based on variable in data setgeom_vlines multiple vlines per plotMapping shape number to legendHow to use the facet grid or facet wrap method in ggplot2 with stacked bar plot?leveled or factored variable used in facets and incorrect geom_text labelingChanging Chart colour scale in loopColor facets in ggplot by continuous variable
Using substitution ciphers to generate new alphabets in a novel
Does IPv6 have similar concept of network mask?
How to explain what's wrong with this application of the chain rule?
How much character growth crosses the line into breaking the character
The IT department bottlenecks progress. How should I handle this?
Add big quotation marks inside my colorbox
What happens if you are holding an Iron Flask with a demon inside and walk into an Antimagic Field?
Why would a new[] expression ever invoke a destructor?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
Why does a simple loop result in ASYNC_NETWORK_IO waits?
Hero deduces identity of a killer
Recommended PCB layout understanding - ADM2572 datasheet
Is there a RAID 0 Equivalent for RAM?
Quoting Keynes in a lecture
creating a ":KeepCursor" command
Pre-mixing cryogenic fuels and using only one fuel tank
Lowest total scrabble score
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
How should I respond when I lied about my education and the company finds out through background check?
Why is it that I can sometimes guess the next note?
Redundant comparison & "if" before assignment
Can I still be respawned if I die by falling off the map?
Adding point to a facet
How to add different lines for facetsAdd a segment only to one facet using ggplot2Month-Year bar graph plot faceted and filled on year; with data input as date in char formatAdding R^2 on graph with facetsScaling data in R data frame and fitting gaussian to geom_pointggplot2: facet_wrap strip color based on variable in data setgeom_vlines multiple vlines per plotMapping shape number to legendHow to use the facet grid or facet wrap method in ggplot2 with stacked bar plot?leveled or factored variable used in facets and incorrect geom_text labelingChanging Chart colour scale in loopColor facets in ggplot by continuous variable
First, the libraries
library(tidyr)
library(leaps)
library(ggplots2)
library(ggdark)
The value of the model
set.seed(1)
X = rnorm(100)
e = rnorm(100)
Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e
Fitting
data.all = data.frame(Y,X)
regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
(reg.summary = summary(regfit.full))
Then I get the minimum value for each variables
(reg.min.cp = which.min(reg.summary$cp))
(reg.min.bic = which.min(reg.summary$bic))
(reg.min.adjr2 = which.min(reg.summary$adjr2))
Creating the data frame for plot
df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
df$rownum = 1:nrow(df)
Reshaping the data frame
molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )
Plotting with facets
(lp = molten %>% ggplot(data=.) +
aes(x=rownum, y=value) +
geom_line(col="black") +
geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
facet_wrap(~variable, scales="free_y")
)

And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
How to make it in that way?
r ggplot2 facet
add a comment |
First, the libraries
library(tidyr)
library(leaps)
library(ggplots2)
library(ggdark)
The value of the model
set.seed(1)
X = rnorm(100)
e = rnorm(100)
Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e
Fitting
data.all = data.frame(Y,X)
regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
(reg.summary = summary(regfit.full))
Then I get the minimum value for each variables
(reg.min.cp = which.min(reg.summary$cp))
(reg.min.bic = which.min(reg.summary$bic))
(reg.min.adjr2 = which.min(reg.summary$adjr2))
Creating the data frame for plot
df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
df$rownum = 1:nrow(df)
Reshaping the data frame
molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )
Plotting with facets
(lp = molten %>% ggplot(data=.) +
aes(x=rownum, y=value) +
geom_line(col="black") +
geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
facet_wrap(~variable, scales="free_y")
)

And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
How to make it in that way?
r ggplot2 facet
add a comment |
First, the libraries
library(tidyr)
library(leaps)
library(ggplots2)
library(ggdark)
The value of the model
set.seed(1)
X = rnorm(100)
e = rnorm(100)
Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e
Fitting
data.all = data.frame(Y,X)
regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
(reg.summary = summary(regfit.full))
Then I get the minimum value for each variables
(reg.min.cp = which.min(reg.summary$cp))
(reg.min.bic = which.min(reg.summary$bic))
(reg.min.adjr2 = which.min(reg.summary$adjr2))
Creating the data frame for plot
df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
df$rownum = 1:nrow(df)
Reshaping the data frame
molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )
Plotting with facets
(lp = molten %>% ggplot(data=.) +
aes(x=rownum, y=value) +
geom_line(col="black") +
geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
facet_wrap(~variable, scales="free_y")
)

And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
How to make it in that way?
r ggplot2 facet
First, the libraries
library(tidyr)
library(leaps)
library(ggplots2)
library(ggdark)
The value of the model
set.seed(1)
X = rnorm(100)
e = rnorm(100)
Y = 8 + 7*X + 2.5*X^2 - 9*X^3 + e
Fitting
data.all = data.frame(Y,X)
regfit.full = regsubsets(Y~poly(X,10,raw=T), data=data.all, nvmax=10)
(reg.summary = summary(regfit.full))
Then I get the minimum value for each variables
(reg.min.cp = which.min(reg.summary$cp))
(reg.min.bic = which.min(reg.summary$bic))
(reg.min.adjr2 = which.min(reg.summary$adjr2))
Creating the data frame for plot
df = data.frame(reg.summary$cp, reg.summary$bic, reg.summary$adjr2)
df$rownum = 1:nrow(df)
Reshaping the data frame
molten = df %>% gather(variable, value, reg.summary.cp:reg.summary.adjr2 )
Plotting with facets
(lp = molten %>% ggplot(data=.) +
aes(x=rownum, y=value) +
geom_line(col="black") +
geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) + # this is where I got the wrong plot
facet_wrap(~variable, scales="free_y")
)

And it shows wrong. What I expect is that the geom_point(data=molten, aes(xint=reg.min.adjr2, z="reg.summary.adjr2", col="red")) will just add the reg.min.adjr2 to the facet reg.summary.adjr2 and only one point.
How to make it in that way?
r ggplot2 facet
r ggplot2 facet
asked Mar 8 at 1:56
isemajisemaj
408
408
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I got some idea here from these two SO:
How to add different lines for facets
Add a segment only to one facet using ggplot2
What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.
I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.
min_plot = data.frame(
rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))
(lp = molten %>% ggplot(data=.)
+ aes(x=rownum, y=value)
+ geom_line(col="black")
+ facet_wrap(~variable, scales="free_y")
+ geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
)
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%2f55055659%2fadding-point-to-a-facet%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 got some idea here from these two SO:
How to add different lines for facets
Add a segment only to one facet using ggplot2
What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.
I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.
min_plot = data.frame(
rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))
(lp = molten %>% ggplot(data=.)
+ aes(x=rownum, y=value)
+ geom_line(col="black")
+ facet_wrap(~variable, scales="free_y")
+ geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
)
add a comment |
I got some idea here from these two SO:
How to add different lines for facets
Add a segment only to one facet using ggplot2
What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.
I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.
min_plot = data.frame(
rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))
(lp = molten %>% ggplot(data=.)
+ aes(x=rownum, y=value)
+ geom_line(col="black")
+ facet_wrap(~variable, scales="free_y")
+ geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
)
add a comment |
I got some idea here from these two SO:
How to add different lines for facets
Add a segment only to one facet using ggplot2
What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.
I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.
min_plot = data.frame(
rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))
(lp = molten %>% ggplot(data=.)
+ aes(x=rownum, y=value)
+ geom_line(col="black")
+ facet_wrap(~variable, scales="free_y")
+ geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
)
I got some idea here from these two SO:
How to add different lines for facets
Add a segment only to one facet using ggplot2
What I did is to create first a new data frame for the min values for cp , bic, and adjr2. And then add the points to the main plot.
I make sure that the value for x will be the rownum and the y are the min values. I also added a parameter variable to min_plot to make sure that it will be added to the right facet.
min_plot = data.frame(
rownum=c(reg.min.cp, reg.min.bic, reg.min.adjr2),
y = c(reg.summary$cp[reg.min.cp], reg.summary$bic[reg.min.bic], reg.summary$adjr2[reg.min.adjr2]),
variable=c("reg.summary.cp", "reg.summary.bic", "reg.summary.adjr2"))
(lp = molten %>% ggplot(data=.)
+ aes(x=rownum, y=value)
+ geom_line(col="black")
+ facet_wrap(~variable, scales="free_y")
+ geom_point(data = min_plot, aes(x=rownum, y=y), col="red")
)
answered Mar 8 at 2:35
isemajisemaj
408
408
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%2f55055659%2fadding-point-to-a-facet%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