Gradient fill in ggplot2
Say if have the following plot.
library(ggplot2)
n <- 1169
df22 <- data.frame(x = 1:n, val = seq(0, 0.5, length.out = n), type = 1)
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, fill = type, group = type))
Instead of the blue color i would like to have a Gradient fill (from blue to red - vertically. So starting with blue at the bottom and red on top with a Parameter to Control the smoothness of Color change).
I found the following resource:
https://ggplot2.tidyverse.org/reference/scale_gradient.html
Unfortunately, it didnt work out for me as my data is not continous(?).
r ggplot2
add a comment |
Say if have the following plot.
library(ggplot2)
n <- 1169
df22 <- data.frame(x = 1:n, val = seq(0, 0.5, length.out = n), type = 1)
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, fill = type, group = type))
Instead of the blue color i would like to have a Gradient fill (from blue to red - vertically. So starting with blue at the bottom and red on top with a Parameter to Control the smoothness of Color change).
I found the following resource:
https://ggplot2.tidyverse.org/reference/scale_gradient.html
Unfortunately, it didnt work out for me as my data is not continous(?).
r ggplot2
2
There's not an easy way to do this.scale_gradient
is made for mapping data to different colors. You don't have data corresponding to the different colors. There are hacky work-arounds (possible dupes), e.g. How to produce a gradient fill within a polygon? and How to make a gradient color filled timeseries plot?
– Gregor
Nov 20 '18 at 16:24
add a comment |
Say if have the following plot.
library(ggplot2)
n <- 1169
df22 <- data.frame(x = 1:n, val = seq(0, 0.5, length.out = n), type = 1)
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, fill = type, group = type))
Instead of the blue color i would like to have a Gradient fill (from blue to red - vertically. So starting with blue at the bottom and red on top with a Parameter to Control the smoothness of Color change).
I found the following resource:
https://ggplot2.tidyverse.org/reference/scale_gradient.html
Unfortunately, it didnt work out for me as my data is not continous(?).
r ggplot2
Say if have the following plot.
library(ggplot2)
n <- 1169
df22 <- data.frame(x = 1:n, val = seq(0, 0.5, length.out = n), type = 1)
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, fill = type, group = type))
Instead of the blue color i would like to have a Gradient fill (from blue to red - vertically. So starting with blue at the bottom and red on top with a Parameter to Control the smoothness of Color change).
I found the following resource:
https://ggplot2.tidyverse.org/reference/scale_gradient.html
Unfortunately, it didnt work out for me as my data is not continous(?).
r ggplot2
r ggplot2
asked Nov 20 '18 at 16:13
ThanksGuysThanksGuys
17417
17417
2
There's not an easy way to do this.scale_gradient
is made for mapping data to different colors. You don't have data corresponding to the different colors. There are hacky work-arounds (possible dupes), e.g. How to produce a gradient fill within a polygon? and How to make a gradient color filled timeseries plot?
– Gregor
Nov 20 '18 at 16:24
add a comment |
2
There's not an easy way to do this.scale_gradient
is made for mapping data to different colors. You don't have data corresponding to the different colors. There are hacky work-arounds (possible dupes), e.g. How to produce a gradient fill within a polygon? and How to make a gradient color filled timeseries plot?
– Gregor
Nov 20 '18 at 16:24
2
2
There's not an easy way to do this.
scale_gradient
is made for mapping data to different colors. You don't have data corresponding to the different colors. There are hacky work-arounds (possible dupes), e.g. How to produce a gradient fill within a polygon? and How to make a gradient color filled timeseries plot?– Gregor
Nov 20 '18 at 16:24
There's not an easy way to do this.
scale_gradient
is made for mapping data to different colors. You don't have data corresponding to the different colors. There are hacky work-arounds (possible dupes), e.g. How to produce a gradient fill within a polygon? and How to make a gradient color filled timeseries plot?– Gregor
Nov 20 '18 at 16:24
add a comment |
1 Answer
1
active
oldest
votes
The following code will do it (but horizontally):
library(scales) # for muted
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, group = type)) +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
If you want to make it vertically, you may flip the coordinates using coord_flip()
upside down.
ggplot(df22, aes(x = val, y = x)) +
geom_ribbon(aes(ymax = val, ymin = 0)) +
coord_flip() +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
Or, if you want it to be horizontal with a vertical gradient (as you requested), you might need to go around it by playing with your data and using the geom_segment()
instead of geom_ribbon()
, like the following:
vals <- lapply(df22$val, function(y) seq(0, y, by = 0.001))
y <- unlist(vals)
mid <- rep(df22$x, lengths(vals))
d2 <- data.frame(x = mid - 1, xend = mid + 1, y = y, yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 1) +
scale_color_gradient2(low = "blue", mid = muted("blue"), high = "red", midpoint = median(d2$y))
This will give you the following:
Hope you find it helpful.
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
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%2f53397131%2fgradient-fill-in-ggplot2%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
The following code will do it (but horizontally):
library(scales) # for muted
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, group = type)) +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
If you want to make it vertically, you may flip the coordinates using coord_flip()
upside down.
ggplot(df22, aes(x = val, y = x)) +
geom_ribbon(aes(ymax = val, ymin = 0)) +
coord_flip() +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
Or, if you want it to be horizontal with a vertical gradient (as you requested), you might need to go around it by playing with your data and using the geom_segment()
instead of geom_ribbon()
, like the following:
vals <- lapply(df22$val, function(y) seq(0, y, by = 0.001))
y <- unlist(vals)
mid <- rep(df22$x, lengths(vals))
d2 <- data.frame(x = mid - 1, xend = mid + 1, y = y, yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 1) +
scale_color_gradient2(low = "blue", mid = muted("blue"), high = "red", midpoint = median(d2$y))
This will give you the following:
Hope you find it helpful.
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
add a comment |
The following code will do it (but horizontally):
library(scales) # for muted
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, group = type)) +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
If you want to make it vertically, you may flip the coordinates using coord_flip()
upside down.
ggplot(df22, aes(x = val, y = x)) +
geom_ribbon(aes(ymax = val, ymin = 0)) +
coord_flip() +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
Or, if you want it to be horizontal with a vertical gradient (as you requested), you might need to go around it by playing with your data and using the geom_segment()
instead of geom_ribbon()
, like the following:
vals <- lapply(df22$val, function(y) seq(0, y, by = 0.001))
y <- unlist(vals)
mid <- rep(df22$x, lengths(vals))
d2 <- data.frame(x = mid - 1, xend = mid + 1, y = y, yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 1) +
scale_color_gradient2(low = "blue", mid = muted("blue"), high = "red", midpoint = median(d2$y))
This will give you the following:
Hope you find it helpful.
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
add a comment |
The following code will do it (but horizontally):
library(scales) # for muted
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, group = type)) +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
If you want to make it vertically, you may flip the coordinates using coord_flip()
upside down.
ggplot(df22, aes(x = val, y = x)) +
geom_ribbon(aes(ymax = val, ymin = 0)) +
coord_flip() +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
Or, if you want it to be horizontal with a vertical gradient (as you requested), you might need to go around it by playing with your data and using the geom_segment()
instead of geom_ribbon()
, like the following:
vals <- lapply(df22$val, function(y) seq(0, y, by = 0.001))
y <- unlist(vals)
mid <- rep(df22$x, lengths(vals))
d2 <- data.frame(x = mid - 1, xend = mid + 1, y = y, yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 1) +
scale_color_gradient2(low = "blue", mid = muted("blue"), high = "red", midpoint = median(d2$y))
This will give you the following:
Hope you find it helpful.
The following code will do it (but horizontally):
library(scales) # for muted
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, group = type)) +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
If you want to make it vertically, you may flip the coordinates using coord_flip()
upside down.
ggplot(df22, aes(x = val, y = x)) +
geom_ribbon(aes(ymax = val, ymin = 0)) +
coord_flip() +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
Or, if you want it to be horizontal with a vertical gradient (as you requested), you might need to go around it by playing with your data and using the geom_segment()
instead of geom_ribbon()
, like the following:
vals <- lapply(df22$val, function(y) seq(0, y, by = 0.001))
y <- unlist(vals)
mid <- rep(df22$x, lengths(vals))
d2 <- data.frame(x = mid - 1, xend = mid + 1, y = y, yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 1) +
scale_color_gradient2(low = "blue", mid = muted("blue"), high = "red", midpoint = median(d2$y))
This will give you the following:
Hope you find it helpful.
edited Nov 20 '18 at 20:52
answered Nov 20 '18 at 16:53
TeeKeaTeeKea
3,22851731
3,22851731
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
add a comment |
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
thank you so much for the great answer. I made an upvote already, as we are very Close. I would require "vertically. So starting with blue at the bottom and red on top", is that possible as well?
– ThanksGuys
Nov 20 '18 at 17:02
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
I see your point. So why don't you flip the coordinates?
– TeeKea
Nov 20 '18 at 18:16
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
thank you for the Response. Unfortunately thats not possible. It will have to be vertically - blue on bottom, red on top.
– ThanksGuys
Nov 20 '18 at 18:28
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
I have updated my answer. Please have a look.
– TeeKea
Nov 20 '18 at 19:09
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
awesome, great work!
– ThanksGuys
Nov 20 '18 at 20:30
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%2f53397131%2fgradient-fill-in-ggplot2%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
2
There's not an easy way to do this.
scale_gradient
is made for mapping data to different colors. You don't have data corresponding to the different colors. There are hacky work-arounds (possible dupes), e.g. How to produce a gradient fill within a polygon? and How to make a gradient color filled timeseries plot?– Gregor
Nov 20 '18 at 16:24