How to make a US map based on state-level data?Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)How to make a great R reproducible exampleHow can we make xkcd style graphs?how do I match color palettes in multiple ggplot2 graphs in R?how to make a bar plot for a list of dataframes?Why can't I bind these data frames in R?Plot longitude-latitude points on a map with different colors for varying levels?Create data frame of names of 50 states in Rggplot2 & map filling colors in states in R studio
Taking my Ph.D. advisor out for dinner after graduation
What is the maximum amount of diamond in one Minecraft game?
Do intermediate subdomains need to exist?
How do I check that users don't write down their passwords?
Sleepy tired vs physically tired
Bypass with wrong cvv of debit card and getting OTP
What do I need to see before Spider-Man: Far From Home?
Why no parachutes in the Orion AA2 abort test?
Motorcyle Chain needs to be cleaned every time you lube it?
Should I increase my 401(k) contributions, or increase my mortgage payments
What is the fundamental difference between catching whales and hunting other animals?
Way to see all encrypted fields in Salesforce?
What are some bad ways to subvert tropes?
Can a Time Lord survive with just one heart?
Boss furious on bad appraisal
Who is responsible for exterminating cockroaches in house - tenant or landlord?
When is one 'Ready' to make Original Contributions to Mathematics?
Can you take the Dodge action while prone?
Is it acceptable that I plot a time-series figure with years increasing from right to left?
Is conquering your neighbors to fight a greater enemy a valid strategy?
How can I effectively map a multi-level dungeon?
Is it possible that Curiosity measured its own methane or failed doing the spectrometry?
Why do most airliners have underwing engines, while business jets have rear-mounted engines?
Was I wrongfully denied boarding for having a Schengen visa issued from the second country on my itinerary?
How to make a US map based on state-level data?
Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)How to make a great R reproducible exampleHow can we make xkcd style graphs?how do I match color palettes in multiple ggplot2 graphs in R?how to make a bar plot for a list of dataframes?Why can't I bind these data frames in R?Plot longitude-latitude points on a map with different colors for varying levels?Create data frame of names of 50 states in Rggplot2 & map filling colors in states in R studio
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to use ggplot2 to make a US map based on some state level data, and color each state based on the value of one variable.
State loan
AL 25310770
AK 45310770
AZ 35310770
AR 25682770
...
Edit: Thanks to @Hector Haffenden, the dput(head(your_data))
gives:
structure(list(state = c("AL", "AK", "AZ", "AR", "IL", "MA"),
loan = c(25310770, 21230922, 15055436, 15212963, 12796921, 20311736),
row.names = c(NA, 6L), class = "data.frame")
Since I have a variable of state name, is it possible to automatically match each row to the map based on the state name abbreviations? Here is an example of my expected output:
https://i.imgur.com/0CD4fOx.png
r ggplot2
add a comment |
I am trying to use ggplot2 to make a US map based on some state level data, and color each state based on the value of one variable.
State loan
AL 25310770
AK 45310770
AZ 35310770
AR 25682770
...
Edit: Thanks to @Hector Haffenden, the dput(head(your_data))
gives:
structure(list(state = c("AL", "AK", "AZ", "AR", "IL", "MA"),
loan = c(25310770, 21230922, 15055436, 15212963, 12796921, 20311736),
row.names = c(NA, 6L), class = "data.frame")
Since I have a variable of state name, is it possible to automatically match each row to the map based on the state name abbreviations? Here is an example of my expected output:
https://i.imgur.com/0CD4fOx.png
r ggplot2
Hello "The R", your question was well written. A potential improvement could be adding the results fromdput(head(your_data))
to the question. This allows someone to copy paste a sample. Will get you a much faster and more accurate answer :) Thanks for the accept and welcome to stack.
– Hector Haffenden
Mar 25 at 19:37
@HectorHaffenden Thanks for your advice! I will do that!
– The R
Mar 25 at 19:46
add a comment |
I am trying to use ggplot2 to make a US map based on some state level data, and color each state based on the value of one variable.
State loan
AL 25310770
AK 45310770
AZ 35310770
AR 25682770
...
Edit: Thanks to @Hector Haffenden, the dput(head(your_data))
gives:
structure(list(state = c("AL", "AK", "AZ", "AR", "IL", "MA"),
loan = c(25310770, 21230922, 15055436, 15212963, 12796921, 20311736),
row.names = c(NA, 6L), class = "data.frame")
Since I have a variable of state name, is it possible to automatically match each row to the map based on the state name abbreviations? Here is an example of my expected output:
https://i.imgur.com/0CD4fOx.png
r ggplot2
I am trying to use ggplot2 to make a US map based on some state level data, and color each state based on the value of one variable.
State loan
AL 25310770
AK 45310770
AZ 35310770
AR 25682770
...
Edit: Thanks to @Hector Haffenden, the dput(head(your_data))
gives:
structure(list(state = c("AL", "AK", "AZ", "AR", "IL", "MA"),
loan = c(25310770, 21230922, 15055436, 15212963, 12796921, 20311736),
row.names = c(NA, 6L), class = "data.frame")
Since I have a variable of state name, is it possible to automatically match each row to the map based on the state name abbreviations? Here is an example of my expected output:
https://i.imgur.com/0CD4fOx.png
r ggplot2
r ggplot2
edited Mar 25 at 19:50
The R
asked Mar 25 at 19:18
The RThe R
155 bronze badges
155 bronze badges
Hello "The R", your question was well written. A potential improvement could be adding the results fromdput(head(your_data))
to the question. This allows someone to copy paste a sample. Will get you a much faster and more accurate answer :) Thanks for the accept and welcome to stack.
– Hector Haffenden
Mar 25 at 19:37
@HectorHaffenden Thanks for your advice! I will do that!
– The R
Mar 25 at 19:46
add a comment |
Hello "The R", your question was well written. A potential improvement could be adding the results fromdput(head(your_data))
to the question. This allows someone to copy paste a sample. Will get you a much faster and more accurate answer :) Thanks for the accept and welcome to stack.
– Hector Haffenden
Mar 25 at 19:37
@HectorHaffenden Thanks for your advice! I will do that!
– The R
Mar 25 at 19:46
Hello "The R", your question was well written. A potential improvement could be adding the results from
dput(head(your_data))
to the question. This allows someone to copy paste a sample. Will get you a much faster and more accurate answer :) Thanks for the accept and welcome to stack.– Hector Haffenden
Mar 25 at 19:37
Hello "The R", your question was well written. A potential improvement could be adding the results from
dput(head(your_data))
to the question. This allows someone to copy paste a sample. Will get you a much faster and more accurate answer :) Thanks for the accept and welcome to stack.– Hector Haffenden
Mar 25 at 19:37
@HectorHaffenden Thanks for your advice! I will do that!
– The R
Mar 25 at 19:46
@HectorHaffenden Thanks for your advice! I will do that!
– The R
Mar 25 at 19:46
add a comment |
1 Answer
1
active
oldest
votes
Try this, first define our data like this,
dat <- data.frame(state = c("AL", "AK", "AZ", "AR"), Loan = c(25310770, 45310770, 35310770, 25682770))
Import the packages usmap
and ggplot2
, then, with more complete data, it will fill the whole map, but using the sample provided, we see
library(usmap)
library(ggplot2)
plot_usmap(
data = dat, values = "Loan", lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Loan", label = scales::comma
) +
labs(title = "US States", subtitle = "States and loan data") +
theme(legend.position = "right")
Note some states are grey due to small sample of data provided.
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%2f55344991%2fhow-to-make-a-us-map-based-on-state-level-data%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
Try this, first define our data like this,
dat <- data.frame(state = c("AL", "AK", "AZ", "AR"), Loan = c(25310770, 45310770, 35310770, 25682770))
Import the packages usmap
and ggplot2
, then, with more complete data, it will fill the whole map, but using the sample provided, we see
library(usmap)
library(ggplot2)
plot_usmap(
data = dat, values = "Loan", lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Loan", label = scales::comma
) +
labs(title = "US States", subtitle = "States and loan data") +
theme(legend.position = "right")
Note some states are grey due to small sample of data provided.
add a comment |
Try this, first define our data like this,
dat <- data.frame(state = c("AL", "AK", "AZ", "AR"), Loan = c(25310770, 45310770, 35310770, 25682770))
Import the packages usmap
and ggplot2
, then, with more complete data, it will fill the whole map, but using the sample provided, we see
library(usmap)
library(ggplot2)
plot_usmap(
data = dat, values = "Loan", lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Loan", label = scales::comma
) +
labs(title = "US States", subtitle = "States and loan data") +
theme(legend.position = "right")
Note some states are grey due to small sample of data provided.
add a comment |
Try this, first define our data like this,
dat <- data.frame(state = c("AL", "AK", "AZ", "AR"), Loan = c(25310770, 45310770, 35310770, 25682770))
Import the packages usmap
and ggplot2
, then, with more complete data, it will fill the whole map, but using the sample provided, we see
library(usmap)
library(ggplot2)
plot_usmap(
data = dat, values = "Loan", lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Loan", label = scales::comma
) +
labs(title = "US States", subtitle = "States and loan data") +
theme(legend.position = "right")
Note some states are grey due to small sample of data provided.
Try this, first define our data like this,
dat <- data.frame(state = c("AL", "AK", "AZ", "AR"), Loan = c(25310770, 45310770, 35310770, 25682770))
Import the packages usmap
and ggplot2
, then, with more complete data, it will fill the whole map, but using the sample provided, we see
library(usmap)
library(ggplot2)
plot_usmap(
data = dat, values = "Loan", lines = "red"
) +
scale_fill_continuous(
low = "white", high = "red", name = "Loan", label = scales::comma
) +
labs(title = "US States", subtitle = "States and loan data") +
theme(legend.position = "right")
Note some states are grey due to small sample of data provided.
edited Mar 25 at 19:32
answered Mar 25 at 19:25
Hector HaffendenHector Haffenden
9244 silver badges16 bronze badges
9244 silver badges16 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55344991%2fhow-to-make-a-us-map-based-on-state-level-data%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
Hello "The R", your question was well written. A potential improvement could be adding the results from
dput(head(your_data))
to the question. This allows someone to copy paste a sample. Will get you a much faster and more accurate answer :) Thanks for the accept and welcome to stack.– Hector Haffenden
Mar 25 at 19:37
@HectorHaffenden Thanks for your advice! I will do that!
– The R
Mar 25 at 19:46