R geom_ribbon not shading area under density curve properly over interval
up vote
1
down vote
favorite
I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.
library(ggplot2)
library(plyr)
library(dplyr)
library(tidyr)
x <- seq(0, 43, 0.1)
x_min <- 16
x_max <- 22
df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
sdlog = 0.24))
df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
"Participating", "Not Participating"))
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
gg <- gg + geom_ribbon()
gg <- gg + theme(legend.title = element_blank())
gg
r ggplot2 density-plot
add a comment |
up vote
1
down vote
favorite
I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.
library(ggplot2)
library(plyr)
library(dplyr)
library(tidyr)
x <- seq(0, 43, 0.1)
x_min <- 16
x_max <- 22
df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
sdlog = 0.24))
df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
"Participating", "Not Participating"))
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
gg <- gg + geom_ribbon()
gg <- gg + theme(legend.title = element_blank())
gg
r ggplot2 density-plot
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.
library(ggplot2)
library(plyr)
library(dplyr)
library(tidyr)
x <- seq(0, 43, 0.1)
x_min <- 16
x_max <- 22
df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
sdlog = 0.24))
df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
"Participating", "Not Participating"))
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
gg <- gg + geom_ribbon()
gg <- gg + theme(legend.title = element_blank())
gg
r ggplot2 density-plot
I am trying to shade the area under a lognormal density plot for a certain interval using the code below. This has worked for me in the past using other density functions and intervals, but for some reason now it produces the defect you can see in the graphic.
library(ggplot2)
library(plyr)
library(dplyr)
library(tidyr)
x <- seq(0, 43, 0.1)
x_min <- 16
x_max <- 22
df <- data.frame(x = x, f = dlnorm(x, meanlog = 2.5,
sdlog = 0.24))
df <- df %>% mutate(area = ifelse(x >= x_min & x < x_max,
"Participating", "Not Participating"))
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f, fill = area))
gg <- gg + geom_ribbon()
gg <- gg + theme(legend.title = element_blank())
gg
r ggplot2 density-plot
r ggplot2 density-plot
asked Nov 10 at 18:13
ben
3473923
3473923
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax
where the left area ends and ymax
where the right one starts, and the same for ymin
. Probably in the past you always used this method for the distribution tails and this problem never arose.
As to solve this, you are going to need to somehow manually have two geom_ribbon
. A not particularly intrusive way would be
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
gg <- gg + theme(legend.title = element_blank())
Depending on that you wish to do with colors, it potentially can be further modified/simplified.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax
where the left area ends and ymax
where the right one starts, and the same for ymin
. Probably in the past you always used this method for the distribution tails and this problem never arose.
As to solve this, you are going to need to somehow manually have two geom_ribbon
. A not particularly intrusive way would be
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
gg <- gg + theme(legend.title = element_blank())
Depending on that you wish to do with colors, it potentially can be further modified/simplified.
add a comment |
up vote
1
down vote
accepted
The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax
where the left area ends and ymax
where the right one starts, and the same for ymin
. Probably in the past you always used this method for the distribution tails and this problem never arose.
As to solve this, you are going to need to somehow manually have two geom_ribbon
. A not particularly intrusive way would be
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
gg <- gg + theme(legend.title = element_blank())
Depending on that you wish to do with colors, it potentially can be further modified/simplified.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax
where the left area ends and ymax
where the right one starts, and the same for ymin
. Probably in the past you always used this method for the distribution tails and this problem never arose.
As to solve this, you are going to need to somehow manually have two geom_ribbon
. A not particularly intrusive way would be
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
gg <- gg + theme(legend.title = element_blank())
Depending on that you wish to do with colors, it potentially can be further modified/simplified.
The issue here is that you are trying to have a ribbon consisting of two pieces. Consequently, the two intended red areas try to connect to each other: ymax
where the left area ends and ymax
where the right one starts, and the same for ymin
. Probably in the past you always used this method for the distribution tails and this problem never arose.
As to solve this, you are going to need to somehow manually have two geom_ribbon
. A not particularly intrusive way would be
gg <- ggplot(data = df, aes(x = x, ymin = 0, ymax = f))
gg <- gg + geom_ribbon(aes(fill = factor("Not participating")))
gg <- gg + geom_ribbon(data = df[df$area == "Participating", ], aes(fill = area))
gg <- gg + theme(legend.title = element_blank())
Depending on that you wish to do with colors, it potentially can be further modified/simplified.
edited Nov 11 at 17:34
answered Nov 10 at 18:37
Julius Vainora
26.6k75877
26.6k75877
add a comment |
add a comment |
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%2f53241976%2fr-geom-ribbon-not-shading-area-under-density-curve-properly-over-interval%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