Raster-like timeseries graph in ggplot2
I'm trying to recreate a graph like the one here using ggplot2
.
I can get pretty close if I mess around with the size and shape of points using coord_equal, but...
Example data and code
library(ggplot2)
df <- data.frame()
Years <- 1990:2020
for(i in 1:length(Years)) {
Year <- Years[i]
week <-1:52
value <- sort(round(rnorm(52, 50, 30), 0))
df.small <- data.frame(Year = Year, week = week, value = value)
df <- bind_rows(df, df.small)
}
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10)) +
coord_equal()
The problem is,
with my real data I want to "stretch" the graph so I can see it more clearly (my timeseries is shorter) and when I don't use coord_equal, squares don't fill the graphing area:
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10))
r ggplot2 time-series raster
add a comment |
I'm trying to recreate a graph like the one here using ggplot2
.
I can get pretty close if I mess around with the size and shape of points using coord_equal, but...
Example data and code
library(ggplot2)
df <- data.frame()
Years <- 1990:2020
for(i in 1:length(Years)) {
Year <- Years[i]
week <-1:52
value <- sort(round(rnorm(52, 50, 30), 0))
df.small <- data.frame(Year = Year, week = week, value = value)
df <- bind_rows(df, df.small)
}
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10)) +
coord_equal()
The problem is,
with my real data I want to "stretch" the graph so I can see it more clearly (my timeseries is shorter) and when I don't use coord_equal, squares don't fill the graphing area:
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10))
r ggplot2 time-series raster
2
Side note: please scrap rainbow colour scales: nature.com/articles/519291d
– Tung
Nov 19 '18 at 18:27
2
You can find better colormaps than rainbow in this thread stackoverflow.com/q/37482977/786542
– Tung
Nov 19 '18 at 18:28
1
This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :)
– Nova
Nov 19 '18 at 21:16
that's great to hear. The world needs more thoughtful scientists like you :)
– Tung
Nov 19 '18 at 22:33
add a comment |
I'm trying to recreate a graph like the one here using ggplot2
.
I can get pretty close if I mess around with the size and shape of points using coord_equal, but...
Example data and code
library(ggplot2)
df <- data.frame()
Years <- 1990:2020
for(i in 1:length(Years)) {
Year <- Years[i]
week <-1:52
value <- sort(round(rnorm(52, 50, 30), 0))
df.small <- data.frame(Year = Year, week = week, value = value)
df <- bind_rows(df, df.small)
}
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10)) +
coord_equal()
The problem is,
with my real data I want to "stretch" the graph so I can see it more clearly (my timeseries is shorter) and when I don't use coord_equal, squares don't fill the graphing area:
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10))
r ggplot2 time-series raster
I'm trying to recreate a graph like the one here using ggplot2
.
I can get pretty close if I mess around with the size and shape of points using coord_equal, but...
Example data and code
library(ggplot2)
df <- data.frame()
Years <- 1990:2020
for(i in 1:length(Years)) {
Year <- Years[i]
week <-1:52
value <- sort(round(rnorm(52, 50, 30), 0))
df.small <- data.frame(Year = Year, week = week, value = value)
df <- bind_rows(df, df.small)
}
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10)) +
coord_equal()
The problem is,
with my real data I want to "stretch" the graph so I can see it more clearly (my timeseries is shorter) and when I don't use coord_equal, squares don't fill the graphing area:
ggplot(df, aes(week, Year, color = value)) +
geom_point(shape = 15, size = 2.7) +
scale_color_gradientn(colours = rainbow(10))
r ggplot2 time-series raster
r ggplot2 time-series raster
edited Nov 19 '18 at 18:21
Nova
asked Nov 19 '18 at 18:11
NovaNova
1,3491229
1,3491229
2
Side note: please scrap rainbow colour scales: nature.com/articles/519291d
– Tung
Nov 19 '18 at 18:27
2
You can find better colormaps than rainbow in this thread stackoverflow.com/q/37482977/786542
– Tung
Nov 19 '18 at 18:28
1
This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :)
– Nova
Nov 19 '18 at 21:16
that's great to hear. The world needs more thoughtful scientists like you :)
– Tung
Nov 19 '18 at 22:33
add a comment |
2
Side note: please scrap rainbow colour scales: nature.com/articles/519291d
– Tung
Nov 19 '18 at 18:27
2
You can find better colormaps than rainbow in this thread stackoverflow.com/q/37482977/786542
– Tung
Nov 19 '18 at 18:28
1
This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :)
– Nova
Nov 19 '18 at 21:16
that's great to hear. The world needs more thoughtful scientists like you :)
– Tung
Nov 19 '18 at 22:33
2
2
Side note: please scrap rainbow colour scales: nature.com/articles/519291d
– Tung
Nov 19 '18 at 18:27
Side note: please scrap rainbow colour scales: nature.com/articles/519291d
– Tung
Nov 19 '18 at 18:27
2
2
You can find better colormaps than rainbow in this thread stackoverflow.com/q/37482977/786542
– Tung
Nov 19 '18 at 18:28
You can find better colormaps than rainbow in this thread stackoverflow.com/q/37482977/786542
– Tung
Nov 19 '18 at 18:28
1
1
This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :)
– Nova
Nov 19 '18 at 21:16
This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :)
– Nova
Nov 19 '18 at 21:16
that's great to hear. The world needs more thoughtful scientists like you :)
– Tung
Nov 19 '18 at 22:33
that's great to hear. The world needs more thoughtful scientists like you :)
– Tung
Nov 19 '18 at 22:33
add a comment |
1 Answer
1
active
oldest
votes
Is this as simple as using the geom_raster
geom?
ggplot(df, aes(week, Year)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours = rainbow(10)) +
coord_equal()
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
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%2f53380410%2fraster-like-timeseries-graph-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
Is this as simple as using the geom_raster
geom?
ggplot(df, aes(week, Year)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours = rainbow(10)) +
coord_equal()
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
add a comment |
Is this as simple as using the geom_raster
geom?
ggplot(df, aes(week, Year)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours = rainbow(10)) +
coord_equal()
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
add a comment |
Is this as simple as using the geom_raster
geom?
ggplot(df, aes(week, Year)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours = rainbow(10)) +
coord_equal()
Is this as simple as using the geom_raster
geom?
ggplot(df, aes(week, Year)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours = rainbow(10)) +
coord_equal()
answered Nov 19 '18 at 18:23
Rui BarradasRui Barradas
16.6k51730
16.6k51730
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
add a comment |
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!!
– Nova
Nov 19 '18 at 21:15
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%2f53380410%2fraster-like-timeseries-graph-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
Side note: please scrap rainbow colour scales: nature.com/articles/519291d
– Tung
Nov 19 '18 at 18:27
2
You can find better colormaps than rainbow in this thread stackoverflow.com/q/37482977/786542
– Tung
Nov 19 '18 at 18:28
1
This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :)
– Nova
Nov 19 '18 at 21:16
that's great to hear. The world needs more thoughtful scientists like you :)
– Tung
Nov 19 '18 at 22:33