Coloring ggplot density plot based on cutoff [duplicate]












0
















This question already has an answer here:




  • Shading a kernel density plot between two points.

    5 answers



  • Shade density plot to the left of vline?

    1 answer



  • Shade (fill or color) area under density curve by quantile

    2 answers



  • How to shade part of a density curve in ggplot (with no y axis data)

    1 answer




I'm trying to make a density plot in ggplot with different colors based on one cutoff. There are a couple questions floating around about how to conditionally color histograms based on a cutoff, and how to color the quantiles on a density plot. However, I haven't found any resources on how to color a density plot given a single cutoff. Here is my code so far:



ggplot(dde_test, aes(x = meanABC)) +
geom_vline(aes(xintercept=quantile(dde_test$meanABC)[4]), color="black", linetype="dashed", size=1) +
geom_density(alpha = 0.7, color = "black", fill = "#cb351e") + #, fill = "#405484"
scale_x_continuous(limits = c(3,12), expand = c(0, 0)) +
scale_y_continuous(limits = c(0,0.3), expand = c(0, 0)) +
xlab("") +
ylab("density") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))


which produces the following plot:



link to image, since my account is still too new to embed images



Very simply, I'd like to color everything above the dashed line red, and everything below it blue. I tried putting an if else statement in the fill part of geom_density(), but that didn't work. Any tips would be much appreciated- thank you!



p.s. I'd also like to keep the dashed line black throughout the plot; right now it's blending with the red density- would be great to get pointers on this as well. Apologies if these questions are very basic!



EDIT: I've seen this question before- Shading a kernel density plot between two points. but it's not the same as it isn't using geom_density. It's also coloring between two points, whereas I want to color before and after a single point.










share|improve this question















marked as duplicate by Axeman r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 23:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • re p.s.: switch the order of the geoms.

    – Axeman
    Nov 22 '18 at 22:59











  • There is some ggplot answers if you scroll down in the duplicate linked.

    – Axeman
    Nov 22 '18 at 23:01











  • I've seen the one you've linked. None of the answers there are using geom_density; that is a key difference. I'm also not trying to color between two points but rather before/after a single point.

    – krc3004
    Nov 22 '18 at 23:06











  • It's also not the same because I'd like to use two different colors; in the one you've linked they only use one. Can you remove the duplicate tag, @Axeman?

    – krc3004
    Nov 22 '18 at 23:09











  • They are not using geom_density because you can't (for the coloring at least). You'll need to do some other calculation, usually just calling density outside ggplot. Added another dupe.

    – Axeman
    Nov 22 '18 at 23:41


















0
















This question already has an answer here:




  • Shading a kernel density plot between two points.

    5 answers



  • Shade density plot to the left of vline?

    1 answer



  • Shade (fill or color) area under density curve by quantile

    2 answers



  • How to shade part of a density curve in ggplot (with no y axis data)

    1 answer




I'm trying to make a density plot in ggplot with different colors based on one cutoff. There are a couple questions floating around about how to conditionally color histograms based on a cutoff, and how to color the quantiles on a density plot. However, I haven't found any resources on how to color a density plot given a single cutoff. Here is my code so far:



ggplot(dde_test, aes(x = meanABC)) +
geom_vline(aes(xintercept=quantile(dde_test$meanABC)[4]), color="black", linetype="dashed", size=1) +
geom_density(alpha = 0.7, color = "black", fill = "#cb351e") + #, fill = "#405484"
scale_x_continuous(limits = c(3,12), expand = c(0, 0)) +
scale_y_continuous(limits = c(0,0.3), expand = c(0, 0)) +
xlab("") +
ylab("density") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))


which produces the following plot:



link to image, since my account is still too new to embed images



Very simply, I'd like to color everything above the dashed line red, and everything below it blue. I tried putting an if else statement in the fill part of geom_density(), but that didn't work. Any tips would be much appreciated- thank you!



p.s. I'd also like to keep the dashed line black throughout the plot; right now it's blending with the red density- would be great to get pointers on this as well. Apologies if these questions are very basic!



EDIT: I've seen this question before- Shading a kernel density plot between two points. but it's not the same as it isn't using geom_density. It's also coloring between two points, whereas I want to color before and after a single point.










share|improve this question















marked as duplicate by Axeman r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 23:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • re p.s.: switch the order of the geoms.

    – Axeman
    Nov 22 '18 at 22:59











  • There is some ggplot answers if you scroll down in the duplicate linked.

    – Axeman
    Nov 22 '18 at 23:01











  • I've seen the one you've linked. None of the answers there are using geom_density; that is a key difference. I'm also not trying to color between two points but rather before/after a single point.

    – krc3004
    Nov 22 '18 at 23:06











  • It's also not the same because I'd like to use two different colors; in the one you've linked they only use one. Can you remove the duplicate tag, @Axeman?

    – krc3004
    Nov 22 '18 at 23:09











  • They are not using geom_density because you can't (for the coloring at least). You'll need to do some other calculation, usually just calling density outside ggplot. Added another dupe.

    – Axeman
    Nov 22 '18 at 23:41
















0












0








0









This question already has an answer here:




  • Shading a kernel density plot between two points.

    5 answers



  • Shade density plot to the left of vline?

    1 answer



  • Shade (fill or color) area under density curve by quantile

    2 answers



  • How to shade part of a density curve in ggplot (with no y axis data)

    1 answer




I'm trying to make a density plot in ggplot with different colors based on one cutoff. There are a couple questions floating around about how to conditionally color histograms based on a cutoff, and how to color the quantiles on a density plot. However, I haven't found any resources on how to color a density plot given a single cutoff. Here is my code so far:



ggplot(dde_test, aes(x = meanABC)) +
geom_vline(aes(xintercept=quantile(dde_test$meanABC)[4]), color="black", linetype="dashed", size=1) +
geom_density(alpha = 0.7, color = "black", fill = "#cb351e") + #, fill = "#405484"
scale_x_continuous(limits = c(3,12), expand = c(0, 0)) +
scale_y_continuous(limits = c(0,0.3), expand = c(0, 0)) +
xlab("") +
ylab("density") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))


which produces the following plot:



link to image, since my account is still too new to embed images



Very simply, I'd like to color everything above the dashed line red, and everything below it blue. I tried putting an if else statement in the fill part of geom_density(), but that didn't work. Any tips would be much appreciated- thank you!



p.s. I'd also like to keep the dashed line black throughout the plot; right now it's blending with the red density- would be great to get pointers on this as well. Apologies if these questions are very basic!



EDIT: I've seen this question before- Shading a kernel density plot between two points. but it's not the same as it isn't using geom_density. It's also coloring between two points, whereas I want to color before and after a single point.










share|improve this question

















This question already has an answer here:




  • Shading a kernel density plot between two points.

    5 answers



  • Shade density plot to the left of vline?

    1 answer



  • Shade (fill or color) area under density curve by quantile

    2 answers



  • How to shade part of a density curve in ggplot (with no y axis data)

    1 answer




I'm trying to make a density plot in ggplot with different colors based on one cutoff. There are a couple questions floating around about how to conditionally color histograms based on a cutoff, and how to color the quantiles on a density plot. However, I haven't found any resources on how to color a density plot given a single cutoff. Here is my code so far:



ggplot(dde_test, aes(x = meanABC)) +
geom_vline(aes(xintercept=quantile(dde_test$meanABC)[4]), color="black", linetype="dashed", size=1) +
geom_density(alpha = 0.7, color = "black", fill = "#cb351e") + #, fill = "#405484"
scale_x_continuous(limits = c(3,12), expand = c(0, 0)) +
scale_y_continuous(limits = c(0,0.3), expand = c(0, 0)) +
xlab("") +
ylab("density") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))


which produces the following plot:



link to image, since my account is still too new to embed images



Very simply, I'd like to color everything above the dashed line red, and everything below it blue. I tried putting an if else statement in the fill part of geom_density(), but that didn't work. Any tips would be much appreciated- thank you!



p.s. I'd also like to keep the dashed line black throughout the plot; right now it's blending with the red density- would be great to get pointers on this as well. Apologies if these questions are very basic!



EDIT: I've seen this question before- Shading a kernel density plot between two points. but it's not the same as it isn't using geom_density. It's also coloring between two points, whereas I want to color before and after a single point.





This question already has an answer here:




  • Shading a kernel density plot between two points.

    5 answers



  • Shade density plot to the left of vline?

    1 answer



  • Shade (fill or color) area under density curve by quantile

    2 answers



  • How to shade part of a density curve in ggplot (with no y axis data)

    1 answer








r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 7:13









Mr. T

4,18791535




4,18791535










asked Nov 22 '18 at 22:57









krc3004krc3004

11




11




marked as duplicate by Axeman r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 23:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Axeman r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 23:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • re p.s.: switch the order of the geoms.

    – Axeman
    Nov 22 '18 at 22:59











  • There is some ggplot answers if you scroll down in the duplicate linked.

    – Axeman
    Nov 22 '18 at 23:01











  • I've seen the one you've linked. None of the answers there are using geom_density; that is a key difference. I'm also not trying to color between two points but rather before/after a single point.

    – krc3004
    Nov 22 '18 at 23:06











  • It's also not the same because I'd like to use two different colors; in the one you've linked they only use one. Can you remove the duplicate tag, @Axeman?

    – krc3004
    Nov 22 '18 at 23:09











  • They are not using geom_density because you can't (for the coloring at least). You'll need to do some other calculation, usually just calling density outside ggplot. Added another dupe.

    – Axeman
    Nov 22 '18 at 23:41





















  • re p.s.: switch the order of the geoms.

    – Axeman
    Nov 22 '18 at 22:59











  • There is some ggplot answers if you scroll down in the duplicate linked.

    – Axeman
    Nov 22 '18 at 23:01











  • I've seen the one you've linked. None of the answers there are using geom_density; that is a key difference. I'm also not trying to color between two points but rather before/after a single point.

    – krc3004
    Nov 22 '18 at 23:06











  • It's also not the same because I'd like to use two different colors; in the one you've linked they only use one. Can you remove the duplicate tag, @Axeman?

    – krc3004
    Nov 22 '18 at 23:09











  • They are not using geom_density because you can't (for the coloring at least). You'll need to do some other calculation, usually just calling density outside ggplot. Added another dupe.

    – Axeman
    Nov 22 '18 at 23:41



















re p.s.: switch the order of the geoms.

– Axeman
Nov 22 '18 at 22:59





re p.s.: switch the order of the geoms.

– Axeman
Nov 22 '18 at 22:59













There is some ggplot answers if you scroll down in the duplicate linked.

– Axeman
Nov 22 '18 at 23:01





There is some ggplot answers if you scroll down in the duplicate linked.

– Axeman
Nov 22 '18 at 23:01













I've seen the one you've linked. None of the answers there are using geom_density; that is a key difference. I'm also not trying to color between two points but rather before/after a single point.

– krc3004
Nov 22 '18 at 23:06





I've seen the one you've linked. None of the answers there are using geom_density; that is a key difference. I'm also not trying to color between two points but rather before/after a single point.

– krc3004
Nov 22 '18 at 23:06













It's also not the same because I'd like to use two different colors; in the one you've linked they only use one. Can you remove the duplicate tag, @Axeman?

– krc3004
Nov 22 '18 at 23:09





It's also not the same because I'd like to use two different colors; in the one you've linked they only use one. Can you remove the duplicate tag, @Axeman?

– krc3004
Nov 22 '18 at 23:09













They are not using geom_density because you can't (for the coloring at least). You'll need to do some other calculation, usually just calling density outside ggplot. Added another dupe.

– Axeman
Nov 22 '18 at 23:41







They are not using geom_density because you can't (for the coloring at least). You'll need to do some other calculation, usually just calling density outside ggplot. Added another dupe.

– Axeman
Nov 22 '18 at 23:41














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'