How to flip x and y axes in ggraph R
The first image is a hand drawn (using MS word) image of a graph. The second image is an attempt to generate the same graph using ggraph.
Following is the code I used to automatically draw the graph when given the node-edge connections (the code was adopted from this thread). I want to move the x and y axis of the ggraph as shown in the hand drawn graph (image 1). Reverse the x axis numbering from top to bottom and move the y axis to the top. How do I go about it?
library(igraph)
library(tidyverse)
library(ggraph)
V <- read.table(text = "x y
2 1
4 2
4 4
2 5
6 4
3 7
8 6",
header = T) %>%
rownames_to_column("name")
E <- matrix(c(0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0), nrow = 7, byrow = T) %>%
data.frame() %>%
rename_all(list(function(x) 1:7)) %>%
rownames_to_column(var = "from") %>%
gather(to, val, 2:6) %>%
filter(val == 1) %>%
select(from, to)
g <- graph_from_data_frame(E, vertices = V, directed = F)
png("C:\Users\Yasoda\Downloads\rplot.png", width = 450, height = 450)
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
theme_minimal()
dev.off()
I tried using scale_x_reverse() but it distorts the layout and gives the warning "Scale for 'x' is already present.Adding another scale for 'x', which will replace the existing scale.". Also I tried the position = "top" option in the scale_y_continuous and it doesn't make a difference either.
r ggplot2 ggraph
add a comment |
The first image is a hand drawn (using MS word) image of a graph. The second image is an attempt to generate the same graph using ggraph.
Following is the code I used to automatically draw the graph when given the node-edge connections (the code was adopted from this thread). I want to move the x and y axis of the ggraph as shown in the hand drawn graph (image 1). Reverse the x axis numbering from top to bottom and move the y axis to the top. How do I go about it?
library(igraph)
library(tidyverse)
library(ggraph)
V <- read.table(text = "x y
2 1
4 2
4 4
2 5
6 4
3 7
8 6",
header = T) %>%
rownames_to_column("name")
E <- matrix(c(0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0), nrow = 7, byrow = T) %>%
data.frame() %>%
rename_all(list(function(x) 1:7)) %>%
rownames_to_column(var = "from") %>%
gather(to, val, 2:6) %>%
filter(val == 1) %>%
select(from, to)
g <- graph_from_data_frame(E, vertices = V, directed = F)
png("C:\Users\Yasoda\Downloads\rplot.png", width = 450, height = 450)
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
theme_minimal()
dev.off()
I tried using scale_x_reverse() but it distorts the layout and gives the warning "Scale for 'x' is already present.Adding another scale for 'x', which will replace the existing scale.". Also I tried the position = "top" option in the scale_y_continuous and it doesn't make a difference either.
r ggplot2 ggraph
2
The example seems to need some fixing:g <- graph_from_data_frame(E, vertices = V, directed = FALSE)
throws an error.
– Julius Vainora
Jan 20 at 14:16
@JuliusVainora apologies. I have fixed it now. It's the addition of the ID column to the data frame V. I removed that part. Now it should work.
– SriniShine
Jan 20 at 18:37
add a comment |
The first image is a hand drawn (using MS word) image of a graph. The second image is an attempt to generate the same graph using ggraph.
Following is the code I used to automatically draw the graph when given the node-edge connections (the code was adopted from this thread). I want to move the x and y axis of the ggraph as shown in the hand drawn graph (image 1). Reverse the x axis numbering from top to bottom and move the y axis to the top. How do I go about it?
library(igraph)
library(tidyverse)
library(ggraph)
V <- read.table(text = "x y
2 1
4 2
4 4
2 5
6 4
3 7
8 6",
header = T) %>%
rownames_to_column("name")
E <- matrix(c(0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0), nrow = 7, byrow = T) %>%
data.frame() %>%
rename_all(list(function(x) 1:7)) %>%
rownames_to_column(var = "from") %>%
gather(to, val, 2:6) %>%
filter(val == 1) %>%
select(from, to)
g <- graph_from_data_frame(E, vertices = V, directed = F)
png("C:\Users\Yasoda\Downloads\rplot.png", width = 450, height = 450)
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
theme_minimal()
dev.off()
I tried using scale_x_reverse() but it distorts the layout and gives the warning "Scale for 'x' is already present.Adding another scale for 'x', which will replace the existing scale.". Also I tried the position = "top" option in the scale_y_continuous and it doesn't make a difference either.
r ggplot2 ggraph
The first image is a hand drawn (using MS word) image of a graph. The second image is an attempt to generate the same graph using ggraph.
Following is the code I used to automatically draw the graph when given the node-edge connections (the code was adopted from this thread). I want to move the x and y axis of the ggraph as shown in the hand drawn graph (image 1). Reverse the x axis numbering from top to bottom and move the y axis to the top. How do I go about it?
library(igraph)
library(tidyverse)
library(ggraph)
V <- read.table(text = "x y
2 1
4 2
4 4
2 5
6 4
3 7
8 6",
header = T) %>%
rownames_to_column("name")
E <- matrix(c(0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0), nrow = 7, byrow = T) %>%
data.frame() %>%
rename_all(list(function(x) 1:7)) %>%
rownames_to_column(var = "from") %>%
gather(to, val, 2:6) %>%
filter(val == 1) %>%
select(from, to)
g <- graph_from_data_frame(E, vertices = V, directed = F)
png("C:\Users\Yasoda\Downloads\rplot.png", width = 450, height = 450)
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
theme_minimal()
dev.off()
I tried using scale_x_reverse() but it distorts the layout and gives the warning "Scale for 'x' is already present.Adding another scale for 'x', which will replace the existing scale.". Also I tried the position = "top" option in the scale_y_continuous and it doesn't make a difference either.
r ggplot2 ggraph
r ggplot2 ggraph
edited Jan 20 at 18:43
SriniShine
asked Jan 20 at 13:52
SriniShineSriniShine
43721429
43721429
2
The example seems to need some fixing:g <- graph_from_data_frame(E, vertices = V, directed = FALSE)
throws an error.
– Julius Vainora
Jan 20 at 14:16
@JuliusVainora apologies. I have fixed it now. It's the addition of the ID column to the data frame V. I removed that part. Now it should work.
– SriniShine
Jan 20 at 18:37
add a comment |
2
The example seems to need some fixing:g <- graph_from_data_frame(E, vertices = V, directed = FALSE)
throws an error.
– Julius Vainora
Jan 20 at 14:16
@JuliusVainora apologies. I have fixed it now. It's the addition of the ID column to the data frame V. I removed that part. Now it should work.
– SriniShine
Jan 20 at 18:37
2
2
The example seems to need some fixing:
g <- graph_from_data_frame(E, vertices = V, directed = FALSE)
throws an error.– Julius Vainora
Jan 20 at 14:16
The example seems to need some fixing:
g <- graph_from_data_frame(E, vertices = V, directed = FALSE)
throws an error.– Julius Vainora
Jan 20 at 14:16
@JuliusVainora apologies. I have fixed it now. It's the addition of the ID column to the data frame V. I removed that part. Now it should work.
– SriniShine
Jan 20 at 18:37
@JuliusVainora apologies. I have fixed it now. It's the addition of the ID column to the data frame V. I removed that part. Now it should work.
– SriniShine
Jan 20 at 18:37
add a comment |
1 Answer
1
active
oldest
votes
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
# Using scale_x_reverse and swapping the limits
scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) +
# switching y position to "right" (pre-flip)
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
theme_minimal()
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
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%2f54277115%2fhow-to-flip-x-and-y-axes-in-ggraph-r%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
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
# Using scale_x_reverse and swapping the limits
scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) +
# switching y position to "right" (pre-flip)
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
theme_minimal()
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
add a comment |
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
# Using scale_x_reverse and swapping the limits
scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) +
# switching y position to "right" (pre-flip)
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
theme_minimal()
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
add a comment |
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
# Using scale_x_reverse and swapping the limits
scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) +
# switching y position to "right" (pre-flip)
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
theme_minimal()
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
# Using scale_x_reverse and swapping the limits
scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) +
# switching y position to "right" (pre-flip)
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
theme_minimal()
answered Jan 20 at 21:12
Jon SpringJon Spring
6,1581726
6,1581726
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
add a comment |
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
thank you very much Jon Spring!
– SriniShine
Jan 20 at 22:12
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%2f54277115%2fhow-to-flip-x-and-y-axes-in-ggraph-r%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
The example seems to need some fixing:
g <- graph_from_data_frame(E, vertices = V, directed = FALSE)
throws an error.– Julius Vainora
Jan 20 at 14:16
@JuliusVainora apologies. I have fixed it now. It's the addition of the ID column to the data frame V. I removed that part. Now it should work.
– SriniShine
Jan 20 at 18:37