Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()
I am doing some analysis in ggplot2 at the moment for a project and by chance I stumbled across some (for me) weird behavior that I cannot explain. When I write aes(x = cyl, ...)
the plot looks different to what it does if I pass the same variable using aes(x = mtcars$cyl, ...)
. When I remove facet_grid(am ~ .)
both graphs are the same again. The code below is modeled after the code in my project that generates the same behavior:
library(dplyr)
library(ggplot2)
data = mtcars
test.data = data %>%
select(-hp)
ggplot(test.data, aes(x = test.data$cyl, y = mpg)) +
geom_point() +
facet_grid(am ~ .) +
labs(title="graph 1 - dollar sign notation")
ggplot(test.data, aes(x = cyl, y = mpg)) +
geom_point()+
facet_grid(am ~ .) +
labs(title="graph 2 - no dollar sign notation")
Here is the picture of graph 1:
Here is the picture of graph 2:
I found that I can work around this problem using aes_string
instead of aes
and passing the variable names as strings, but I would like to understand why ggplot is behaving that way. The problem also occurs in similar attempts with facet_wrap
.
Thx a lot for any help in advance! I feel very uncomfortable if I do not understand that properly...
r ggplot2 r-faq
|
show 3 more comments
I am doing some analysis in ggplot2 at the moment for a project and by chance I stumbled across some (for me) weird behavior that I cannot explain. When I write aes(x = cyl, ...)
the plot looks different to what it does if I pass the same variable using aes(x = mtcars$cyl, ...)
. When I remove facet_grid(am ~ .)
both graphs are the same again. The code below is modeled after the code in my project that generates the same behavior:
library(dplyr)
library(ggplot2)
data = mtcars
test.data = data %>%
select(-hp)
ggplot(test.data, aes(x = test.data$cyl, y = mpg)) +
geom_point() +
facet_grid(am ~ .) +
labs(title="graph 1 - dollar sign notation")
ggplot(test.data, aes(x = cyl, y = mpg)) +
geom_point()+
facet_grid(am ~ .) +
labs(title="graph 2 - no dollar sign notation")
Here is the picture of graph 1:
Here is the picture of graph 2:
I found that I can work around this problem using aes_string
instead of aes
and passing the variable names as strings, but I would like to understand why ggplot is behaving that way. The problem also occurs in similar attempts with facet_wrap
.
Thx a lot for any help in advance! I feel very uncomfortable if I do not understand that properly...
r ggplot2 r-faq
11
the short answer is: never use$
inaes()
– baptiste
Sep 12 '15 at 20:22
^_^ after the shock I got today when my graph suddenly looked all weird, I won't do it again. Still I would like to understand what is happening, because I never encountered this problem/behavior before.
– Christoph
Sep 12 '15 at 20:34
1
when ggplot builds the plot, if splits the dataset(s) for each layer into groups, defined by the aesthetics and facetting. For this grouping to be reliable, you need the variables to originate from a single data.frame, otherwise ggplot may end up using a different order for the facetting factor and the rest of the mapping.
– baptiste
Sep 12 '15 at 20:39
hmm, but isn't the variable in the same data.frame in this example irrespective of whether I write aes(x = cyl, ...) or aes(x = test.data$cyl,...)? test.data is the data.frame I pass to ggplot and it contains all variables. Where am I going wrong? Thx a lot for your quick reply though!
– Christoph
Sep 12 '15 at 20:45
3
The point here is that because ggplot is using nonstandard evaluation techniques, and R's environment and scoping systems can be complicated, when you use $ here you are providing potentially confusing information that will cause ggplot to respond unpredictably. The manner in which things may go wrong are varied, complex, and usually unintuitive.
– joran
Sep 12 '15 at 20:54
|
show 3 more comments
I am doing some analysis in ggplot2 at the moment for a project and by chance I stumbled across some (for me) weird behavior that I cannot explain. When I write aes(x = cyl, ...)
the plot looks different to what it does if I pass the same variable using aes(x = mtcars$cyl, ...)
. When I remove facet_grid(am ~ .)
both graphs are the same again. The code below is modeled after the code in my project that generates the same behavior:
library(dplyr)
library(ggplot2)
data = mtcars
test.data = data %>%
select(-hp)
ggplot(test.data, aes(x = test.data$cyl, y = mpg)) +
geom_point() +
facet_grid(am ~ .) +
labs(title="graph 1 - dollar sign notation")
ggplot(test.data, aes(x = cyl, y = mpg)) +
geom_point()+
facet_grid(am ~ .) +
labs(title="graph 2 - no dollar sign notation")
Here is the picture of graph 1:
Here is the picture of graph 2:
I found that I can work around this problem using aes_string
instead of aes
and passing the variable names as strings, but I would like to understand why ggplot is behaving that way. The problem also occurs in similar attempts with facet_wrap
.
Thx a lot for any help in advance! I feel very uncomfortable if I do not understand that properly...
r ggplot2 r-faq
I am doing some analysis in ggplot2 at the moment for a project and by chance I stumbled across some (for me) weird behavior that I cannot explain. When I write aes(x = cyl, ...)
the plot looks different to what it does if I pass the same variable using aes(x = mtcars$cyl, ...)
. When I remove facet_grid(am ~ .)
both graphs are the same again. The code below is modeled after the code in my project that generates the same behavior:
library(dplyr)
library(ggplot2)
data = mtcars
test.data = data %>%
select(-hp)
ggplot(test.data, aes(x = test.data$cyl, y = mpg)) +
geom_point() +
facet_grid(am ~ .) +
labs(title="graph 1 - dollar sign notation")
ggplot(test.data, aes(x = cyl, y = mpg)) +
geom_point()+
facet_grid(am ~ .) +
labs(title="graph 2 - no dollar sign notation")
Here is the picture of graph 1:
Here is the picture of graph 2:
I found that I can work around this problem using aes_string
instead of aes
and passing the variable names as strings, but I would like to understand why ggplot is behaving that way. The problem also occurs in similar attempts with facet_wrap
.
Thx a lot for any help in advance! I feel very uncomfortable if I do not understand that properly...
r ggplot2 r-faq
r ggplot2 r-faq
edited Jun 14 '18 at 2:53
Tung
10k22944
10k22944
asked Sep 12 '15 at 20:21
ChristophChristoph
19511
19511
11
the short answer is: never use$
inaes()
– baptiste
Sep 12 '15 at 20:22
^_^ after the shock I got today when my graph suddenly looked all weird, I won't do it again. Still I would like to understand what is happening, because I never encountered this problem/behavior before.
– Christoph
Sep 12 '15 at 20:34
1
when ggplot builds the plot, if splits the dataset(s) for each layer into groups, defined by the aesthetics and facetting. For this grouping to be reliable, you need the variables to originate from a single data.frame, otherwise ggplot may end up using a different order for the facetting factor and the rest of the mapping.
– baptiste
Sep 12 '15 at 20:39
hmm, but isn't the variable in the same data.frame in this example irrespective of whether I write aes(x = cyl, ...) or aes(x = test.data$cyl,...)? test.data is the data.frame I pass to ggplot and it contains all variables. Where am I going wrong? Thx a lot for your quick reply though!
– Christoph
Sep 12 '15 at 20:45
3
The point here is that because ggplot is using nonstandard evaluation techniques, and R's environment and scoping systems can be complicated, when you use $ here you are providing potentially confusing information that will cause ggplot to respond unpredictably. The manner in which things may go wrong are varied, complex, and usually unintuitive.
– joran
Sep 12 '15 at 20:54
|
show 3 more comments
11
the short answer is: never use$
inaes()
– baptiste
Sep 12 '15 at 20:22
^_^ after the shock I got today when my graph suddenly looked all weird, I won't do it again. Still I would like to understand what is happening, because I never encountered this problem/behavior before.
– Christoph
Sep 12 '15 at 20:34
1
when ggplot builds the plot, if splits the dataset(s) for each layer into groups, defined by the aesthetics and facetting. For this grouping to be reliable, you need the variables to originate from a single data.frame, otherwise ggplot may end up using a different order for the facetting factor and the rest of the mapping.
– baptiste
Sep 12 '15 at 20:39
hmm, but isn't the variable in the same data.frame in this example irrespective of whether I write aes(x = cyl, ...) or aes(x = test.data$cyl,...)? test.data is the data.frame I pass to ggplot and it contains all variables. Where am I going wrong? Thx a lot for your quick reply though!
– Christoph
Sep 12 '15 at 20:45
3
The point here is that because ggplot is using nonstandard evaluation techniques, and R's environment and scoping systems can be complicated, when you use $ here you are providing potentially confusing information that will cause ggplot to respond unpredictably. The manner in which things may go wrong are varied, complex, and usually unintuitive.
– joran
Sep 12 '15 at 20:54
11
11
the short answer is: never use
$
in aes()
– baptiste
Sep 12 '15 at 20:22
the short answer is: never use
$
in aes()
– baptiste
Sep 12 '15 at 20:22
^_^ after the shock I got today when my graph suddenly looked all weird, I won't do it again. Still I would like to understand what is happening, because I never encountered this problem/behavior before.
– Christoph
Sep 12 '15 at 20:34
^_^ after the shock I got today when my graph suddenly looked all weird, I won't do it again. Still I would like to understand what is happening, because I never encountered this problem/behavior before.
– Christoph
Sep 12 '15 at 20:34
1
1
when ggplot builds the plot, if splits the dataset(s) for each layer into groups, defined by the aesthetics and facetting. For this grouping to be reliable, you need the variables to originate from a single data.frame, otherwise ggplot may end up using a different order for the facetting factor and the rest of the mapping.
– baptiste
Sep 12 '15 at 20:39
when ggplot builds the plot, if splits the dataset(s) for each layer into groups, defined by the aesthetics and facetting. For this grouping to be reliable, you need the variables to originate from a single data.frame, otherwise ggplot may end up using a different order for the facetting factor and the rest of the mapping.
– baptiste
Sep 12 '15 at 20:39
hmm, but isn't the variable in the same data.frame in this example irrespective of whether I write aes(x = cyl, ...) or aes(x = test.data$cyl,...)? test.data is the data.frame I pass to ggplot and it contains all variables. Where am I going wrong? Thx a lot for your quick reply though!
– Christoph
Sep 12 '15 at 20:45
hmm, but isn't the variable in the same data.frame in this example irrespective of whether I write aes(x = cyl, ...) or aes(x = test.data$cyl,...)? test.data is the data.frame I pass to ggplot and it contains all variables. Where am I going wrong? Thx a lot for your quick reply though!
– Christoph
Sep 12 '15 at 20:45
3
3
The point here is that because ggplot is using nonstandard evaluation techniques, and R's environment and scoping systems can be complicated, when you use $ here you are providing potentially confusing information that will cause ggplot to respond unpredictably. The manner in which things may go wrong are varied, complex, and usually unintuitive.
– joran
Sep 12 '15 at 20:54
The point here is that because ggplot is using nonstandard evaluation techniques, and R's environment and scoping systems can be complicated, when you use $ here you are providing potentially confusing information that will cause ggplot to respond unpredictably. The manner in which things may go wrong are varied, complex, and usually unintuitive.
– joran
Sep 12 '15 at 20:54
|
show 3 more comments
1 Answer
1
active
oldest
votes
tl;dr
Never use [
or $
inside aes()
.
Consider this illustrative example where the facetting variable f
is purposely in a non-obvious order with respect to x
d <- data.frame(x=1:10, f=rev(letters[gl(2,5)]))
Now contrast what happens with these two plots,
p1 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(x, y=0, label=x, colour=f)) +
ggtitle("good mapping")
p2 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(d$x, y=0, label=x, colour=f)) +
ggtitle("$ corruption")
We can get a better idea of what's happening by looking at the data.frame created internally by ggplot2 for each panel,
ggplot_build(p1)[["data"]][[1]][,c("x","PANEL")]
x PANEL
1 6 1
2 7 1
3 8 1
4 9 1
5 10 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
ggplot_build(p2)[["data"]][[1]][,c("x", "PANEL")]
x PANEL
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2
The second plot has the wrong mapping, because when ggplot creates a data.frame for each panel, it picks x values in the "wrong" order.
This occurs because the use of $
breaks the link between the various variables to be mapped (ggplot must assume it's an independent variable, which for all it knows could come from an arbitrary, disconnected source). Since the data.frame in this example is not ordered according to the factor f
, the subset data.frames used internally for each panel assume the wrong order.
1
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
1
This problem has been fixed inggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
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%2f32543340%2fissue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio%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
tl;dr
Never use [
or $
inside aes()
.
Consider this illustrative example where the facetting variable f
is purposely in a non-obvious order with respect to x
d <- data.frame(x=1:10, f=rev(letters[gl(2,5)]))
Now contrast what happens with these two plots,
p1 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(x, y=0, label=x, colour=f)) +
ggtitle("good mapping")
p2 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(d$x, y=0, label=x, colour=f)) +
ggtitle("$ corruption")
We can get a better idea of what's happening by looking at the data.frame created internally by ggplot2 for each panel,
ggplot_build(p1)[["data"]][[1]][,c("x","PANEL")]
x PANEL
1 6 1
2 7 1
3 8 1
4 9 1
5 10 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
ggplot_build(p2)[["data"]][[1]][,c("x", "PANEL")]
x PANEL
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2
The second plot has the wrong mapping, because when ggplot creates a data.frame for each panel, it picks x values in the "wrong" order.
This occurs because the use of $
breaks the link between the various variables to be mapped (ggplot must assume it's an independent variable, which for all it knows could come from an arbitrary, disconnected source). Since the data.frame in this example is not ordered according to the factor f
, the subset data.frames used internally for each panel assume the wrong order.
1
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
1
This problem has been fixed inggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
add a comment |
tl;dr
Never use [
or $
inside aes()
.
Consider this illustrative example where the facetting variable f
is purposely in a non-obvious order with respect to x
d <- data.frame(x=1:10, f=rev(letters[gl(2,5)]))
Now contrast what happens with these two plots,
p1 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(x, y=0, label=x, colour=f)) +
ggtitle("good mapping")
p2 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(d$x, y=0, label=x, colour=f)) +
ggtitle("$ corruption")
We can get a better idea of what's happening by looking at the data.frame created internally by ggplot2 for each panel,
ggplot_build(p1)[["data"]][[1]][,c("x","PANEL")]
x PANEL
1 6 1
2 7 1
3 8 1
4 9 1
5 10 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
ggplot_build(p2)[["data"]][[1]][,c("x", "PANEL")]
x PANEL
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2
The second plot has the wrong mapping, because when ggplot creates a data.frame for each panel, it picks x values in the "wrong" order.
This occurs because the use of $
breaks the link between the various variables to be mapped (ggplot must assume it's an independent variable, which for all it knows could come from an arbitrary, disconnected source). Since the data.frame in this example is not ordered according to the factor f
, the subset data.frames used internally for each panel assume the wrong order.
1
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
1
This problem has been fixed inggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
add a comment |
tl;dr
Never use [
or $
inside aes()
.
Consider this illustrative example where the facetting variable f
is purposely in a non-obvious order with respect to x
d <- data.frame(x=1:10, f=rev(letters[gl(2,5)]))
Now contrast what happens with these two plots,
p1 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(x, y=0, label=x, colour=f)) +
ggtitle("good mapping")
p2 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(d$x, y=0, label=x, colour=f)) +
ggtitle("$ corruption")
We can get a better idea of what's happening by looking at the data.frame created internally by ggplot2 for each panel,
ggplot_build(p1)[["data"]][[1]][,c("x","PANEL")]
x PANEL
1 6 1
2 7 1
3 8 1
4 9 1
5 10 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
ggplot_build(p2)[["data"]][[1]][,c("x", "PANEL")]
x PANEL
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2
The second plot has the wrong mapping, because when ggplot creates a data.frame for each panel, it picks x values in the "wrong" order.
This occurs because the use of $
breaks the link between the various variables to be mapped (ggplot must assume it's an independent variable, which for all it knows could come from an arbitrary, disconnected source). Since the data.frame in this example is not ordered according to the factor f
, the subset data.frames used internally for each panel assume the wrong order.
tl;dr
Never use [
or $
inside aes()
.
Consider this illustrative example where the facetting variable f
is purposely in a non-obvious order with respect to x
d <- data.frame(x=1:10, f=rev(letters[gl(2,5)]))
Now contrast what happens with these two plots,
p1 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(x, y=0, label=x, colour=f)) +
ggtitle("good mapping")
p2 <- ggplot(d) +
facet_grid(.~f, labeller = label_both) +
geom_text(aes(d$x, y=0, label=x, colour=f)) +
ggtitle("$ corruption")
We can get a better idea of what's happening by looking at the data.frame created internally by ggplot2 for each panel,
ggplot_build(p1)[["data"]][[1]][,c("x","PANEL")]
x PANEL
1 6 1
2 7 1
3 8 1
4 9 1
5 10 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
ggplot_build(p2)[["data"]][[1]][,c("x", "PANEL")]
x PANEL
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 2
7 7 2
8 8 2
9 9 2
10 10 2
The second plot has the wrong mapping, because when ggplot creates a data.frame for each panel, it picks x values in the "wrong" order.
This occurs because the use of $
breaks the link between the various variables to be mapped (ggplot must assume it's an independent variable, which for all it knows could come from an arbitrary, disconnected source). Since the data.frame in this example is not ordered according to the factor f
, the subset data.frames used internally for each panel assume the wrong order.
edited Jun 24 '16 at 23:02
answered Sep 12 '15 at 21:09
baptistebaptiste
59k9146230
59k9146230
1
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
1
This problem has been fixed inggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
add a comment |
1
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
1
This problem has been fixed inggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
1
1
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
Thanks a lot for your help! Your example is awesome!
– Christoph
Sep 12 '15 at 21:45
1
1
This problem has been fixed in
ggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
This problem has been fixed in
ggplot2 v3.0.0.9000
– Tung
Jul 14 '18 at 17:41
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%2f32543340%2fissue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio%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
11
the short answer is: never use
$
inaes()
– baptiste
Sep 12 '15 at 20:22
^_^ after the shock I got today when my graph suddenly looked all weird, I won't do it again. Still I would like to understand what is happening, because I never encountered this problem/behavior before.
– Christoph
Sep 12 '15 at 20:34
1
when ggplot builds the plot, if splits the dataset(s) for each layer into groups, defined by the aesthetics and facetting. For this grouping to be reliable, you need the variables to originate from a single data.frame, otherwise ggplot may end up using a different order for the facetting factor and the rest of the mapping.
– baptiste
Sep 12 '15 at 20:39
hmm, but isn't the variable in the same data.frame in this example irrespective of whether I write aes(x = cyl, ...) or aes(x = test.data$cyl,...)? test.data is the data.frame I pass to ggplot and it contains all variables. Where am I going wrong? Thx a lot for your quick reply though!
– Christoph
Sep 12 '15 at 20:45
3
The point here is that because ggplot is using nonstandard evaluation techniques, and R's environment and scoping systems can be complicated, when you use $ here you are providing potentially confusing information that will cause ggplot to respond unpredictably. The manner in which things may go wrong are varied, complex, and usually unintuitive.
– joran
Sep 12 '15 at 20:54