R Choropleth U.S. Map Over Time: Getting Boundaries for Missing States
I'm fairly new to R and ggplot. I'm trying to plot a facet_wrap
of a series of United States choropleth maps over the period 1999 - 2017.
I attempted to follow code from this post (How to map all the states of US using R with the number of crimes occurred in each state?) but am having trouble getting boundaries and filling in states with no data for a given year.
I have a data frame called by_year
like such with data. The issue is for any one year, the majority of the states are not in the df (there was no value reported that year).
state Year Value
new jersey 1999 13.8
oklahoma 1999 16.5
The code I have so far is this:
us.states <- map_data("state")
ggplot(data = by_year, aes(map_id = state)) +
geom_map(aes(fill = Value), color= "black", map = us.states) +
expand_limits(x = us.states$long, y = us.states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") + theme(legend.position = "bottom",
panel.background = element_blank())+
facet_wrap(~Year, ncol = 5)
This produces the following output:
As you can see, for any given year, many of the states do not have data. How to do I get outlines of the missing states for each year and fill them in in light gray? I was also hoping to change the color scale to shades of red (with darker shades being for higher values).
Thank you so much for any help.
r ggplot2 choropleth
add a comment |
I'm fairly new to R and ggplot. I'm trying to plot a facet_wrap
of a series of United States choropleth maps over the period 1999 - 2017.
I attempted to follow code from this post (How to map all the states of US using R with the number of crimes occurred in each state?) but am having trouble getting boundaries and filling in states with no data for a given year.
I have a data frame called by_year
like such with data. The issue is for any one year, the majority of the states are not in the df (there was no value reported that year).
state Year Value
new jersey 1999 13.8
oklahoma 1999 16.5
The code I have so far is this:
us.states <- map_data("state")
ggplot(data = by_year, aes(map_id = state)) +
geom_map(aes(fill = Value), color= "black", map = us.states) +
expand_limits(x = us.states$long, y = us.states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") + theme(legend.position = "bottom",
panel.background = element_blank())+
facet_wrap(~Year, ncol = 5)
This produces the following output:
As you can see, for any given year, many of the states do not have data. How to do I get outlines of the missing states for each year and fill them in in light gray? I was also hoping to change the color scale to shades of red (with darker shades being for higher values).
Thank you so much for any help.
r ggplot2 choropleth
3
you need data for each state even it it'sNA
. This is a well-traveled topic on SO. Which other questions besides the single one you linked to failed to provide assistance?
– hrbrmstr
Nov 15 '18 at 20:47
add a comment |
I'm fairly new to R and ggplot. I'm trying to plot a facet_wrap
of a series of United States choropleth maps over the period 1999 - 2017.
I attempted to follow code from this post (How to map all the states of US using R with the number of crimes occurred in each state?) but am having trouble getting boundaries and filling in states with no data for a given year.
I have a data frame called by_year
like such with data. The issue is for any one year, the majority of the states are not in the df (there was no value reported that year).
state Year Value
new jersey 1999 13.8
oklahoma 1999 16.5
The code I have so far is this:
us.states <- map_data("state")
ggplot(data = by_year, aes(map_id = state)) +
geom_map(aes(fill = Value), color= "black", map = us.states) +
expand_limits(x = us.states$long, y = us.states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") + theme(legend.position = "bottom",
panel.background = element_blank())+
facet_wrap(~Year, ncol = 5)
This produces the following output:
As you can see, for any given year, many of the states do not have data. How to do I get outlines of the missing states for each year and fill them in in light gray? I was also hoping to change the color scale to shades of red (with darker shades being for higher values).
Thank you so much for any help.
r ggplot2 choropleth
I'm fairly new to R and ggplot. I'm trying to plot a facet_wrap
of a series of United States choropleth maps over the period 1999 - 2017.
I attempted to follow code from this post (How to map all the states of US using R with the number of crimes occurred in each state?) but am having trouble getting boundaries and filling in states with no data for a given year.
I have a data frame called by_year
like such with data. The issue is for any one year, the majority of the states are not in the df (there was no value reported that year).
state Year Value
new jersey 1999 13.8
oklahoma 1999 16.5
The code I have so far is this:
us.states <- map_data("state")
ggplot(data = by_year, aes(map_id = state)) +
geom_map(aes(fill = Value), color= "black", map = us.states) +
expand_limits(x = us.states$long, y = us.states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") + theme(legend.position = "bottom",
panel.background = element_blank())+
facet_wrap(~Year, ncol = 5)
This produces the following output:
As you can see, for any given year, many of the states do not have data. How to do I get outlines of the missing states for each year and fill them in in light gray? I was also hoping to change the color scale to shades of red (with darker shades being for higher values).
Thank you so much for any help.
r ggplot2 choropleth
r ggplot2 choropleth
edited Nov 15 '18 at 20:25
jp334
asked Nov 15 '18 at 20:14
jp334jp334
556
556
3
you need data for each state even it it'sNA
. This is a well-traveled topic on SO. Which other questions besides the single one you linked to failed to provide assistance?
– hrbrmstr
Nov 15 '18 at 20:47
add a comment |
3
you need data for each state even it it'sNA
. This is a well-traveled topic on SO. Which other questions besides the single one you linked to failed to provide assistance?
– hrbrmstr
Nov 15 '18 at 20:47
3
3
you need data for each state even it it's
NA
. This is a well-traveled topic on SO. Which other questions besides the single one you linked to failed to provide assistance?– hrbrmstr
Nov 15 '18 at 20:47
you need data for each state even it it's
NA
. This is a well-traveled topic on SO. Which other questions besides the single one you linked to failed to provide assistance?– hrbrmstr
Nov 15 '18 at 20:47
add a comment |
0
active
oldest
votes
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%2f53327238%2fr-choropleth-u-s-map-over-time-getting-boundaries-for-missing-states%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53327238%2fr-choropleth-u-s-map-over-time-getting-boundaries-for-missing-states%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
3
you need data for each state even it it's
NA
. This is a well-traveled topic on SO. Which other questions besides the single one you linked to failed to provide assistance?– hrbrmstr
Nov 15 '18 at 20:47