Add vertical lines below a plot with no extra y-axis
I am trying to produce a normal distribution with vertical lines going down from the x-axis below the graph.
So far I have been able to draw the distribution curve and add lines that go above the x-axis (figure 1) and combine two plots where the lines are below (figure 2).
The problem with figure 1 is that they cover the graph instead of appearing below, and in figure 2 nothing is aligned. Can someone help me with this? I don't mind what method/how hacky the solution is as long as it looks ok.
Figure 1
Figure 2
Code:
library(gridExtra)
library(ggplot2)
# plot 1
p1 <- ggplot(data.frame(x = c(-3, 3)), aes(x)) +
stat_function(fun = dnorm,
fill = "mediumpurple",
alpha = 0.4,
xlim = c(-1.96,1.96),
geom = "area") +
stat_function(fun = dnorm) +
xlab(expression(mu)) +
ylab("") +
theme_classic() +
theme(axis.title.x = element_text(vjust=22, size = 25),
axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 13))
plot(p1)
# plot 2
p2 <- ggplot() +
geom_vline(xintercept =-1.96, linetype=2) +
geom_vline(xintercept = 1.96, linetype=2) +
geom_vline(xintercept = 0, size = 1.1) +
xlim(-3, 3) +
xlab("") +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
grid.arrange(p1, p2, nrow=2)
r ggplot2
add a comment |
I am trying to produce a normal distribution with vertical lines going down from the x-axis below the graph.
So far I have been able to draw the distribution curve and add lines that go above the x-axis (figure 1) and combine two plots where the lines are below (figure 2).
The problem with figure 1 is that they cover the graph instead of appearing below, and in figure 2 nothing is aligned. Can someone help me with this? I don't mind what method/how hacky the solution is as long as it looks ok.
Figure 1
Figure 2
Code:
library(gridExtra)
library(ggplot2)
# plot 1
p1 <- ggplot(data.frame(x = c(-3, 3)), aes(x)) +
stat_function(fun = dnorm,
fill = "mediumpurple",
alpha = 0.4,
xlim = c(-1.96,1.96),
geom = "area") +
stat_function(fun = dnorm) +
xlab(expression(mu)) +
ylab("") +
theme_classic() +
theme(axis.title.x = element_text(vjust=22, size = 25),
axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 13))
plot(p1)
# plot 2
p2 <- ggplot() +
geom_vline(xintercept =-1.96, linetype=2) +
geom_vline(xintercept = 1.96, linetype=2) +
geom_vline(xintercept = 0, size = 1.1) +
xlim(-3, 3) +
xlab("") +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
grid.arrange(p1, p2, nrow=2)
r ggplot2
add a comment |
I am trying to produce a normal distribution with vertical lines going down from the x-axis below the graph.
So far I have been able to draw the distribution curve and add lines that go above the x-axis (figure 1) and combine two plots where the lines are below (figure 2).
The problem with figure 1 is that they cover the graph instead of appearing below, and in figure 2 nothing is aligned. Can someone help me with this? I don't mind what method/how hacky the solution is as long as it looks ok.
Figure 1
Figure 2
Code:
library(gridExtra)
library(ggplot2)
# plot 1
p1 <- ggplot(data.frame(x = c(-3, 3)), aes(x)) +
stat_function(fun = dnorm,
fill = "mediumpurple",
alpha = 0.4,
xlim = c(-1.96,1.96),
geom = "area") +
stat_function(fun = dnorm) +
xlab(expression(mu)) +
ylab("") +
theme_classic() +
theme(axis.title.x = element_text(vjust=22, size = 25),
axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 13))
plot(p1)
# plot 2
p2 <- ggplot() +
geom_vline(xintercept =-1.96, linetype=2) +
geom_vline(xintercept = 1.96, linetype=2) +
geom_vline(xintercept = 0, size = 1.1) +
xlim(-3, 3) +
xlab("") +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
grid.arrange(p1, p2, nrow=2)
r ggplot2
I am trying to produce a normal distribution with vertical lines going down from the x-axis below the graph.
So far I have been able to draw the distribution curve and add lines that go above the x-axis (figure 1) and combine two plots where the lines are below (figure 2).
The problem with figure 1 is that they cover the graph instead of appearing below, and in figure 2 nothing is aligned. Can someone help me with this? I don't mind what method/how hacky the solution is as long as it looks ok.
Figure 1
Figure 2
Code:
library(gridExtra)
library(ggplot2)
# plot 1
p1 <- ggplot(data.frame(x = c(-3, 3)), aes(x)) +
stat_function(fun = dnorm,
fill = "mediumpurple",
alpha = 0.4,
xlim = c(-1.96,1.96),
geom = "area") +
stat_function(fun = dnorm) +
xlab(expression(mu)) +
ylab("") +
theme_classic() +
theme(axis.title.x = element_text(vjust=22, size = 25),
axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 13))
plot(p1)
# plot 2
p2 <- ggplot() +
geom_vline(xintercept =-1.96, linetype=2) +
geom_vline(xintercept = 1.96, linetype=2) +
geom_vline(xintercept = 0, size = 1.1) +
xlim(-3, 3) +
xlab("") +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
grid.arrange(p1, p2, nrow=2)
r ggplot2
r ggplot2
asked Nov 20 '18 at 8:40
RABRAB
1,275317
1,275317
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can align the two graphs using this (as per Left align two graph edges (ggplot)):
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
Then you just need to reduce the space between the two plots.
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:plot.margin=unit(c(1,1,-0.5,1), "cm")
in thetheme()
section :)
– RAB
Nov 20 '18 at 9:03
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%2f53389101%2fadd-vertical-lines-below-a-plot-with-no-extra-y-axis%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
You can align the two graphs using this (as per Left align two graph edges (ggplot)):
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
Then you just need to reduce the space between the two plots.
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:plot.margin=unit(c(1,1,-0.5,1), "cm")
in thetheme()
section :)
– RAB
Nov 20 '18 at 9:03
add a comment |
You can align the two graphs using this (as per Left align two graph edges (ggplot)):
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
Then you just need to reduce the space between the two plots.
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:plot.margin=unit(c(1,1,-0.5,1), "cm")
in thetheme()
section :)
– RAB
Nov 20 '18 at 9:03
add a comment |
You can align the two graphs using this (as per Left align two graph edges (ggplot)):
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
Then you just need to reduce the space between the two plots.
You can align the two graphs using this (as per Left align two graph edges (ggplot)):
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
Then you just need to reduce the space between the two plots.
answered Nov 20 '18 at 8:49
Anna BurnAnna Burn
1569
1569
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:plot.margin=unit(c(1,1,-0.5,1), "cm")
in thetheme()
section :)
– RAB
Nov 20 '18 at 9:03
add a comment |
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:plot.margin=unit(c(1,1,-0.5,1), "cm")
in thetheme()
section :)
– RAB
Nov 20 '18 at 9:03
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:
plot.margin=unit(c(1,1,-0.5,1), "cm")
in the theme()
section :)– RAB
Nov 20 '18 at 9:03
Thanks so much! Also to remove the excess space I figured out if you change the border properties on the original graph you can take away the space :) eg:
plot.margin=unit(c(1,1,-0.5,1), "cm")
in the theme()
section :)– RAB
Nov 20 '18 at 9:03
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%2f53389101%2fadd-vertical-lines-below-a-plot-with-no-extra-y-axis%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