ggplot facet_grid with different y axis scales: reverse axis for a facet panel
I have got four plots with all the same x axis (Time) but different y axis. So I used
library(ggplot2)
Gio.m <- melt(Gio, id="AGE")
ggplot(Gio.m[!is.na(Gio.m$value),], aes(x=AGE, y=value, group=1))+
geom_line(aes(color=variable)) +
facet_grid(variable ~ ., scales="free_y") +
theme(legend.position="none")
to make a grid with four scatterplots.
The result looks like this:
The first question would be how to avoid that the output shows all the y-values.
The second question is, if there is a possibility of turning the axis of only one plot within the grid (which should afterwards have a reversed y-axis).
Thanks a lot for your help, and if I should provide more infos on the data pls let me know.
r ggplot2 facet-grid
add a comment |
I have got four plots with all the same x axis (Time) but different y axis. So I used
library(ggplot2)
Gio.m <- melt(Gio, id="AGE")
ggplot(Gio.m[!is.na(Gio.m$value),], aes(x=AGE, y=value, group=1))+
geom_line(aes(color=variable)) +
facet_grid(variable ~ ., scales="free_y") +
theme(legend.position="none")
to make a grid with four scatterplots.
The result looks like this:
The first question would be how to avoid that the output shows all the y-values.
The second question is, if there is a possibility of turning the axis of only one plot within the grid (which should afterwards have a reversed y-axis).
Thanks a lot for your help, and if I should provide more infos on the data pls let me know.
r ggplot2 facet-grid
3
Can you dump your data viadput(head(df))
? 1. Your Y data seems to be categorical (char, factor, ...). 2. Try either grid.arrange or patchwork
– Roman
Nov 24 '18 at 11:39
take a look atscale_y_continuous(breaks=("Whatever you want the y-vals to be"))
– RAB
Nov 24 '18 at 13:40
add a comment |
I have got four plots with all the same x axis (Time) but different y axis. So I used
library(ggplot2)
Gio.m <- melt(Gio, id="AGE")
ggplot(Gio.m[!is.na(Gio.m$value),], aes(x=AGE, y=value, group=1))+
geom_line(aes(color=variable)) +
facet_grid(variable ~ ., scales="free_y") +
theme(legend.position="none")
to make a grid with four scatterplots.
The result looks like this:
The first question would be how to avoid that the output shows all the y-values.
The second question is, if there is a possibility of turning the axis of only one plot within the grid (which should afterwards have a reversed y-axis).
Thanks a lot for your help, and if I should provide more infos on the data pls let me know.
r ggplot2 facet-grid
I have got four plots with all the same x axis (Time) but different y axis. So I used
library(ggplot2)
Gio.m <- melt(Gio, id="AGE")
ggplot(Gio.m[!is.na(Gio.m$value),], aes(x=AGE, y=value, group=1))+
geom_line(aes(color=variable)) +
facet_grid(variable ~ ., scales="free_y") +
theme(legend.position="none")
to make a grid with four scatterplots.
The result looks like this:
The first question would be how to avoid that the output shows all the y-values.
The second question is, if there is a possibility of turning the axis of only one plot within the grid (which should afterwards have a reversed y-axis).
Thanks a lot for your help, and if I should provide more infos on the data pls let me know.
r ggplot2 facet-grid
r ggplot2 facet-grid
edited Nov 25 '18 at 14:59
Valentin
2,0421231
2,0421231
asked Nov 24 '18 at 11:33
laesaslaesas
84
84
3
Can you dump your data viadput(head(df))
? 1. Your Y data seems to be categorical (char, factor, ...). 2. Try either grid.arrange or patchwork
– Roman
Nov 24 '18 at 11:39
take a look atscale_y_continuous(breaks=("Whatever you want the y-vals to be"))
– RAB
Nov 24 '18 at 13:40
add a comment |
3
Can you dump your data viadput(head(df))
? 1. Your Y data seems to be categorical (char, factor, ...). 2. Try either grid.arrange or patchwork
– Roman
Nov 24 '18 at 11:39
take a look atscale_y_continuous(breaks=("Whatever you want the y-vals to be"))
– RAB
Nov 24 '18 at 13:40
3
3
Can you dump your data via
dput(head(df))
? 1. Your Y data seems to be categorical (char, factor, ...). 2. Try either grid.arrange or patchwork– Roman
Nov 24 '18 at 11:39
Can you dump your data via
dput(head(df))
? 1. Your Y data seems to be categorical (char, factor, ...). 2. Try either grid.arrange or patchwork– Roman
Nov 24 '18 at 11:39
take a look at
scale_y_continuous(breaks=("Whatever you want the y-vals to be"))
– RAB
Nov 24 '18 at 13:40
take a look at
scale_y_continuous(breaks=("Whatever you want the y-vals to be"))
– RAB
Nov 24 '18 at 13:40
add a comment |
1 Answer
1
active
oldest
votes
For your first question, as already mentioned by @Roman, you most probably have categorical data in the column value
after you melt Gio
table. To fix that, transform it back to numeric:
- if
value
is character, then runGio.m$value <- as.numeric(Gio.m$value)
- if
value
is factor, then runGio.m$value <- as.numeric(levels(Gio.m$value))[Gio.m$value]
as pointed out here
For the second question - not sure if I understand correctly, but one solution could be this:
1) Generate a plot example and its version with reversed OY axis:
library(ggplot2)
library(grid)
# Plot 1
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(drv ~ cyl)
# Plot 2 = plot 1 with OY reversed
p2 <- p1 + scale_y_reverse()
2) Get the grid layout and identify grobs:
# Generate the ggplot2 plot grob for each case
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
# Draw a diagram of a Grid layout; Is helpful to identifies grobs
grid.show.layout(gtable:::gtable_layout(g1))
# or reduce the font if more practical
grid.show.layout(gtable:::gtable_layout(g1), vp = viewport(gp = gpar(cex=0.7)))
# Check also the layout
g1$layout
Checking and visualizing the layout structure as above can help with identifying the wanted grobs. Here, I want to identify the names of the top panel grobs, so that I replace them with the ones from the graph with reversed OY.
3) Replace the grobs. Will replace the top 3 panels of plot 1 (p1) with the ones from p2 having the OY reversed. Also need to replace the axis.
# Replace the panels from g1 with the ones from g2
panels <- c('panel-1-1', 'panel-4-1', 'panel-3-2', 'panel-2-3')
for (p in panels){
g1$grobs[grep(p, g1$layout$name)] <- g2$grobs[grep(p, g2$layout$name)]
}
# Also replace the axis corresponding to those panels
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Check the results
p1 # the original plot
grid.newpage(); grid.draw(g1) # the edited plot with top panels having OY reversed
Just realized that you do not facet by two variables, but only by one, in this case, is a bit less complex:
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(cyl ~ ., scales="free_y")
p2 <- p1 + scale_y_reverse()
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g1$grobs[grep("panel-1-1", g1$layout$name)] <- g2$grobs[grep("panel-1-1", g2$layout$name)]
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
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%2f53457698%2fggplot-facet-grid-with-different-y-axis-scales-reverse-axis-for-a-facet-panel%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
For your first question, as already mentioned by @Roman, you most probably have categorical data in the column value
after you melt Gio
table. To fix that, transform it back to numeric:
- if
value
is character, then runGio.m$value <- as.numeric(Gio.m$value)
- if
value
is factor, then runGio.m$value <- as.numeric(levels(Gio.m$value))[Gio.m$value]
as pointed out here
For the second question - not sure if I understand correctly, but one solution could be this:
1) Generate a plot example and its version with reversed OY axis:
library(ggplot2)
library(grid)
# Plot 1
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(drv ~ cyl)
# Plot 2 = plot 1 with OY reversed
p2 <- p1 + scale_y_reverse()
2) Get the grid layout and identify grobs:
# Generate the ggplot2 plot grob for each case
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
# Draw a diagram of a Grid layout; Is helpful to identifies grobs
grid.show.layout(gtable:::gtable_layout(g1))
# or reduce the font if more practical
grid.show.layout(gtable:::gtable_layout(g1), vp = viewport(gp = gpar(cex=0.7)))
# Check also the layout
g1$layout
Checking and visualizing the layout structure as above can help with identifying the wanted grobs. Here, I want to identify the names of the top panel grobs, so that I replace them with the ones from the graph with reversed OY.
3) Replace the grobs. Will replace the top 3 panels of plot 1 (p1) with the ones from p2 having the OY reversed. Also need to replace the axis.
# Replace the panels from g1 with the ones from g2
panels <- c('panel-1-1', 'panel-4-1', 'panel-3-2', 'panel-2-3')
for (p in panels){
g1$grobs[grep(p, g1$layout$name)] <- g2$grobs[grep(p, g2$layout$name)]
}
# Also replace the axis corresponding to those panels
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Check the results
p1 # the original plot
grid.newpage(); grid.draw(g1) # the edited plot with top panels having OY reversed
Just realized that you do not facet by two variables, but only by one, in this case, is a bit less complex:
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(cyl ~ ., scales="free_y")
p2 <- p1 + scale_y_reverse()
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g1$grobs[grep("panel-1-1", g1$layout$name)] <- g2$grobs[grep("panel-1-1", g2$layout$name)]
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
add a comment |
For your first question, as already mentioned by @Roman, you most probably have categorical data in the column value
after you melt Gio
table. To fix that, transform it back to numeric:
- if
value
is character, then runGio.m$value <- as.numeric(Gio.m$value)
- if
value
is factor, then runGio.m$value <- as.numeric(levels(Gio.m$value))[Gio.m$value]
as pointed out here
For the second question - not sure if I understand correctly, but one solution could be this:
1) Generate a plot example and its version with reversed OY axis:
library(ggplot2)
library(grid)
# Plot 1
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(drv ~ cyl)
# Plot 2 = plot 1 with OY reversed
p2 <- p1 + scale_y_reverse()
2) Get the grid layout and identify grobs:
# Generate the ggplot2 plot grob for each case
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
# Draw a diagram of a Grid layout; Is helpful to identifies grobs
grid.show.layout(gtable:::gtable_layout(g1))
# or reduce the font if more practical
grid.show.layout(gtable:::gtable_layout(g1), vp = viewport(gp = gpar(cex=0.7)))
# Check also the layout
g1$layout
Checking and visualizing the layout structure as above can help with identifying the wanted grobs. Here, I want to identify the names of the top panel grobs, so that I replace them with the ones from the graph with reversed OY.
3) Replace the grobs. Will replace the top 3 panels of plot 1 (p1) with the ones from p2 having the OY reversed. Also need to replace the axis.
# Replace the panels from g1 with the ones from g2
panels <- c('panel-1-1', 'panel-4-1', 'panel-3-2', 'panel-2-3')
for (p in panels){
g1$grobs[grep(p, g1$layout$name)] <- g2$grobs[grep(p, g2$layout$name)]
}
# Also replace the axis corresponding to those panels
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Check the results
p1 # the original plot
grid.newpage(); grid.draw(g1) # the edited plot with top panels having OY reversed
Just realized that you do not facet by two variables, but only by one, in this case, is a bit less complex:
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(cyl ~ ., scales="free_y")
p2 <- p1 + scale_y_reverse()
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g1$grobs[grep("panel-1-1", g1$layout$name)] <- g2$grobs[grep("panel-1-1", g2$layout$name)]
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
add a comment |
For your first question, as already mentioned by @Roman, you most probably have categorical data in the column value
after you melt Gio
table. To fix that, transform it back to numeric:
- if
value
is character, then runGio.m$value <- as.numeric(Gio.m$value)
- if
value
is factor, then runGio.m$value <- as.numeric(levels(Gio.m$value))[Gio.m$value]
as pointed out here
For the second question - not sure if I understand correctly, but one solution could be this:
1) Generate a plot example and its version with reversed OY axis:
library(ggplot2)
library(grid)
# Plot 1
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(drv ~ cyl)
# Plot 2 = plot 1 with OY reversed
p2 <- p1 + scale_y_reverse()
2) Get the grid layout and identify grobs:
# Generate the ggplot2 plot grob for each case
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
# Draw a diagram of a Grid layout; Is helpful to identifies grobs
grid.show.layout(gtable:::gtable_layout(g1))
# or reduce the font if more practical
grid.show.layout(gtable:::gtable_layout(g1), vp = viewport(gp = gpar(cex=0.7)))
# Check also the layout
g1$layout
Checking and visualizing the layout structure as above can help with identifying the wanted grobs. Here, I want to identify the names of the top panel grobs, so that I replace them with the ones from the graph with reversed OY.
3) Replace the grobs. Will replace the top 3 panels of plot 1 (p1) with the ones from p2 having the OY reversed. Also need to replace the axis.
# Replace the panels from g1 with the ones from g2
panels <- c('panel-1-1', 'panel-4-1', 'panel-3-2', 'panel-2-3')
for (p in panels){
g1$grobs[grep(p, g1$layout$name)] <- g2$grobs[grep(p, g2$layout$name)]
}
# Also replace the axis corresponding to those panels
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Check the results
p1 # the original plot
grid.newpage(); grid.draw(g1) # the edited plot with top panels having OY reversed
Just realized that you do not facet by two variables, but only by one, in this case, is a bit less complex:
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(cyl ~ ., scales="free_y")
p2 <- p1 + scale_y_reverse()
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g1$grobs[grep("panel-1-1", g1$layout$name)] <- g2$grobs[grep("panel-1-1", g2$layout$name)]
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
For your first question, as already mentioned by @Roman, you most probably have categorical data in the column value
after you melt Gio
table. To fix that, transform it back to numeric:
- if
value
is character, then runGio.m$value <- as.numeric(Gio.m$value)
- if
value
is factor, then runGio.m$value <- as.numeric(levels(Gio.m$value))[Gio.m$value]
as pointed out here
For the second question - not sure if I understand correctly, but one solution could be this:
1) Generate a plot example and its version with reversed OY axis:
library(ggplot2)
library(grid)
# Plot 1
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(drv ~ cyl)
# Plot 2 = plot 1 with OY reversed
p2 <- p1 + scale_y_reverse()
2) Get the grid layout and identify grobs:
# Generate the ggplot2 plot grob for each case
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
# Draw a diagram of a Grid layout; Is helpful to identifies grobs
grid.show.layout(gtable:::gtable_layout(g1))
# or reduce the font if more practical
grid.show.layout(gtable:::gtable_layout(g1), vp = viewport(gp = gpar(cex=0.7)))
# Check also the layout
g1$layout
Checking and visualizing the layout structure as above can help with identifying the wanted grobs. Here, I want to identify the names of the top panel grobs, so that I replace them with the ones from the graph with reversed OY.
3) Replace the grobs. Will replace the top 3 panels of plot 1 (p1) with the ones from p2 having the OY reversed. Also need to replace the axis.
# Replace the panels from g1 with the ones from g2
panels <- c('panel-1-1', 'panel-4-1', 'panel-3-2', 'panel-2-3')
for (p in panels){
g1$grobs[grep(p, g1$layout$name)] <- g2$grobs[grep(p, g2$layout$name)]
}
# Also replace the axis corresponding to those panels
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
Check the results
p1 # the original plot
grid.newpage(); grid.draw(g1) # the edited plot with top panels having OY reversed
Just realized that you do not facet by two variables, but only by one, in this case, is a bit less complex:
p1 <- ggplot(mpg, aes(cty, displ)) + geom_point() + facet_grid(cyl ~ ., scales="free_y")
p2 <- p1 + scale_y_reverse()
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g1$grobs[grep("panel-1-1", g1$layout$name)] <- g2$grobs[grep("panel-1-1", g2$layout$name)]
g1$grobs[grep('axis-l-1', g1$layout$name)] <- g2$grobs[grep('axis-l-1', g2$layout$name)]
edited Nov 24 '18 at 14:01
answered Nov 24 '18 at 13:47
ValentinValentin
2,0421231
2,0421231
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
add a comment |
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
Thanks this works perfectly fine now and looks as i want it to look!
– laesas
Nov 24 '18 at 16:19
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%2f53457698%2fggplot-facet-grid-with-different-y-axis-scales-reverse-axis-for-a-facet-panel%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
Can you dump your data via
dput(head(df))
? 1. Your Y data seems to be categorical (char, factor, ...). 2. Try either grid.arrange or patchwork– Roman
Nov 24 '18 at 11:39
take a look at
scale_y_continuous(breaks=("Whatever you want the y-vals to be"))
– RAB
Nov 24 '18 at 13:40