drawing lines that have the same latitude and longitude on a map (ggplot, geom_curve)
I'd like to get some help from you regarding drawing lines on map using ggplot in R.
Suppose there are two observations, A and B, and their origin and destination longitude and latitude are the same with each other.
I'd like to draw two lines that are not overlapped on the map.
I tried with geom_curve() but it seems to draw two lines on the same trajectory.
Is there a smart way to resolve this issue?
I would say this is sort of similar to geom_repel() but not for texts or labels but for curves.
Thanks.
r ggplot2
add a comment |
I'd like to get some help from you regarding drawing lines on map using ggplot in R.
Suppose there are two observations, A and B, and their origin and destination longitude and latitude are the same with each other.
I'd like to draw two lines that are not overlapped on the map.
I tried with geom_curve() but it seems to draw two lines on the same trajectory.
Is there a smart way to resolve this issue?
I would say this is sort of similar to geom_repel() but not for texts or labels but for curves.
Thanks.
r ggplot2
Hopefully my answer also gives you an idea of how to mock up some sample data for next time as this question may get closed fairly quickly.
– hrbrmstr
Nov 17 '18 at 19:10
add a comment |
I'd like to get some help from you regarding drawing lines on map using ggplot in R.
Suppose there are two observations, A and B, and their origin and destination longitude and latitude are the same with each other.
I'd like to draw two lines that are not overlapped on the map.
I tried with geom_curve() but it seems to draw two lines on the same trajectory.
Is there a smart way to resolve this issue?
I would say this is sort of similar to geom_repel() but not for texts or labels but for curves.
Thanks.
r ggplot2
I'd like to get some help from you regarding drawing lines on map using ggplot in R.
Suppose there are two observations, A and B, and their origin and destination longitude and latitude are the same with each other.
I'd like to draw two lines that are not overlapped on the map.
I tried with geom_curve() but it seems to draw two lines on the same trajectory.
Is there a smart way to resolve this issue?
I would say this is sort of similar to geom_repel() but not for texts or labels but for curves.
Thanks.
r ggplot2
r ggplot2
asked Nov 17 '18 at 18:38
joonjoon
467
467
Hopefully my answer also gives you an idea of how to mock up some sample data for next time as this question may get closed fairly quickly.
– hrbrmstr
Nov 17 '18 at 19:10
add a comment |
Hopefully my answer also gives you an idea of how to mock up some sample data for next time as this question may get closed fairly quickly.
– hrbrmstr
Nov 17 '18 at 19:10
Hopefully my answer also gives you an idea of how to mock up some sample data for next time as this question may get closed fairly quickly.
– hrbrmstr
Nov 17 '18 at 19:10
Hopefully my answer also gives you an idea of how to mock up some sample data for next time as this question may get closed fairly quickly.
– hrbrmstr
Nov 17 '18 at 19:10
add a comment |
1 Answer
1
active
oldest
votes
This is a base R solution (a tidyverse one wld look a bit less "code-y" but do essentially the same thing.
If these are, indeed, X & Y pairs then all you have to do is add a mid-point and jitter said mid-point. I have not fully "solutioned" this for you since you really do need to do some work (like filtering the data frame for only those lines with same start/end points before doing this jitter hack):
library(ggplot2)
lines_df <- as.data.frame(state.center, stringsAsFactors = FALSE)[c(4,20,4,20),]
lines_df$grp <- c("a", "a", "b", "b")
lines_df$where <- rep("end", 4)
do.call(
rbind.data.frame,
lapply(
split(lines_df, lines_df$grp),
function(.df) {
rbind.data.frame(
.df,
data.frame(
x = sum(.df$x)/2, y = sum(.df$y)/2,
grp = .df$grp[1], where = "mid",
stringsAsFactors = FALSE
)
)
}
)
) -> lines_df
lines_df[lines_df$where == "mid", "y"] <- jitter(lines_df[lines_df$where == "mid", "y"])
ggplot(lines_df, aes(x, y, group=grp)) +
geom_line(aes(color=grp))

NOTE: this does not take into account the reality of the earth being curved and naive division by 2 over a sufficiently long line (esp depending on the projection) is less than ideal, but this gives you one way to attack the problem.
tidyverse 4eva 🤘🏻 folks who also think this is the most straightforward approach are encouraged to post a tidyverse solution with the same logic.
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
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%2f53354312%2fdrawing-lines-that-have-the-same-latitude-and-longitude-on-a-map-ggplot-geom-c%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
This is a base R solution (a tidyverse one wld look a bit less "code-y" but do essentially the same thing.
If these are, indeed, X & Y pairs then all you have to do is add a mid-point and jitter said mid-point. I have not fully "solutioned" this for you since you really do need to do some work (like filtering the data frame for only those lines with same start/end points before doing this jitter hack):
library(ggplot2)
lines_df <- as.data.frame(state.center, stringsAsFactors = FALSE)[c(4,20,4,20),]
lines_df$grp <- c("a", "a", "b", "b")
lines_df$where <- rep("end", 4)
do.call(
rbind.data.frame,
lapply(
split(lines_df, lines_df$grp),
function(.df) {
rbind.data.frame(
.df,
data.frame(
x = sum(.df$x)/2, y = sum(.df$y)/2,
grp = .df$grp[1], where = "mid",
stringsAsFactors = FALSE
)
)
}
)
) -> lines_df
lines_df[lines_df$where == "mid", "y"] <- jitter(lines_df[lines_df$where == "mid", "y"])
ggplot(lines_df, aes(x, y, group=grp)) +
geom_line(aes(color=grp))

NOTE: this does not take into account the reality of the earth being curved and naive division by 2 over a sufficiently long line (esp depending on the projection) is less than ideal, but this gives you one way to attack the problem.
tidyverse 4eva 🤘🏻 folks who also think this is the most straightforward approach are encouraged to post a tidyverse solution with the same logic.
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
add a comment |
This is a base R solution (a tidyverse one wld look a bit less "code-y" but do essentially the same thing.
If these are, indeed, X & Y pairs then all you have to do is add a mid-point and jitter said mid-point. I have not fully "solutioned" this for you since you really do need to do some work (like filtering the data frame for only those lines with same start/end points before doing this jitter hack):
library(ggplot2)
lines_df <- as.data.frame(state.center, stringsAsFactors = FALSE)[c(4,20,4,20),]
lines_df$grp <- c("a", "a", "b", "b")
lines_df$where <- rep("end", 4)
do.call(
rbind.data.frame,
lapply(
split(lines_df, lines_df$grp),
function(.df) {
rbind.data.frame(
.df,
data.frame(
x = sum(.df$x)/2, y = sum(.df$y)/2,
grp = .df$grp[1], where = "mid",
stringsAsFactors = FALSE
)
)
}
)
) -> lines_df
lines_df[lines_df$where == "mid", "y"] <- jitter(lines_df[lines_df$where == "mid", "y"])
ggplot(lines_df, aes(x, y, group=grp)) +
geom_line(aes(color=grp))

NOTE: this does not take into account the reality of the earth being curved and naive division by 2 over a sufficiently long line (esp depending on the projection) is less than ideal, but this gives you one way to attack the problem.
tidyverse 4eva 🤘🏻 folks who also think this is the most straightforward approach are encouraged to post a tidyverse solution with the same logic.
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
add a comment |
This is a base R solution (a tidyverse one wld look a bit less "code-y" but do essentially the same thing.
If these are, indeed, X & Y pairs then all you have to do is add a mid-point and jitter said mid-point. I have not fully "solutioned" this for you since you really do need to do some work (like filtering the data frame for only those lines with same start/end points before doing this jitter hack):
library(ggplot2)
lines_df <- as.data.frame(state.center, stringsAsFactors = FALSE)[c(4,20,4,20),]
lines_df$grp <- c("a", "a", "b", "b")
lines_df$where <- rep("end", 4)
do.call(
rbind.data.frame,
lapply(
split(lines_df, lines_df$grp),
function(.df) {
rbind.data.frame(
.df,
data.frame(
x = sum(.df$x)/2, y = sum(.df$y)/2,
grp = .df$grp[1], where = "mid",
stringsAsFactors = FALSE
)
)
}
)
) -> lines_df
lines_df[lines_df$where == "mid", "y"] <- jitter(lines_df[lines_df$where == "mid", "y"])
ggplot(lines_df, aes(x, y, group=grp)) +
geom_line(aes(color=grp))

NOTE: this does not take into account the reality of the earth being curved and naive division by 2 over a sufficiently long line (esp depending on the projection) is less than ideal, but this gives you one way to attack the problem.
tidyverse 4eva 🤘🏻 folks who also think this is the most straightforward approach are encouraged to post a tidyverse solution with the same logic.
This is a base R solution (a tidyverse one wld look a bit less "code-y" but do essentially the same thing.
If these are, indeed, X & Y pairs then all you have to do is add a mid-point and jitter said mid-point. I have not fully "solutioned" this for you since you really do need to do some work (like filtering the data frame for only those lines with same start/end points before doing this jitter hack):
library(ggplot2)
lines_df <- as.data.frame(state.center, stringsAsFactors = FALSE)[c(4,20,4,20),]
lines_df$grp <- c("a", "a", "b", "b")
lines_df$where <- rep("end", 4)
do.call(
rbind.data.frame,
lapply(
split(lines_df, lines_df$grp),
function(.df) {
rbind.data.frame(
.df,
data.frame(
x = sum(.df$x)/2, y = sum(.df$y)/2,
grp = .df$grp[1], where = "mid",
stringsAsFactors = FALSE
)
)
}
)
) -> lines_df
lines_df[lines_df$where == "mid", "y"] <- jitter(lines_df[lines_df$where == "mid", "y"])
ggplot(lines_df, aes(x, y, group=grp)) +
geom_line(aes(color=grp))

NOTE: this does not take into account the reality of the earth being curved and naive division by 2 over a sufficiently long line (esp depending on the projection) is less than ideal, but this gives you one way to attack the problem.
tidyverse 4eva 🤘🏻 folks who also think this is the most straightforward approach are encouraged to post a tidyverse solution with the same logic.
answered Nov 17 '18 at 19:07
hrbrmstrhrbrmstr
60.7k688150
60.7k688150
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
add a comment |
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
Thank you @hrbrmstr. This is extremely helpful.
– joon
Nov 19 '18 at 14:44
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%2f53354312%2fdrawing-lines-that-have-the-same-latitude-and-longitude-on-a-map-ggplot-geom-c%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
Hopefully my answer also gives you an idea of how to mock up some sample data for next time as this question may get closed fairly quickly.
– hrbrmstr
Nov 17 '18 at 19:10