Description
Hi, Teun!
I'm encountering the following when putting ggplot into a custom function:
Simply put, since the column names for x and y in aes change each time the structures aes(x = data[[1]],y = data[[2]])
are used, ggplot
gives a warning. But when dealing with it based on ggplot's warnings, an error occurs straight away.
library(ggplot2)
set.seed(123)
data <- data.frame(a1 = rnorm(100),b1 = rnorm(100))
# Again there will be various combinations of scenarios: c(c2,d2), c(a3,e3) ...
ggplot(data = data, aes(x = data[[1]], y = data[[2]]))+
geom_point()
This is able to draw the image, but a warning pops up:
Warning messages:
1: Use ofdata[[1]]
is discouraged.
ℹ Use.data[[1]]
instead.
2: Use ofdata[[2]]
is discouraged.
ℹ Use.data[[2]]
instead.
Doing as suggested in the warning message, using .data[[1]], etc., unfortunately runs the error.
ggplot(data = data, aes(x =.data[[1]], y = .data[[2]]))+
geom_point()
Error in
geom_point()
:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in.data[[1]]
:
! Must subset the data pronoun with a string, not the number 1.
Runrlang::last_trace()
to see where the error occurred.
My question is: should the first behavior be warned (banned in a potential sense)? Or is there a chance that the latter could be realized by making code changes?
Best wishes,
Hu