Skip to content

Content in aes is warned But the proposed treatment is problematic (Apologize for the title) #6534

Closed
@Breeze-Hu

Description

@Breeze-Hu

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()

Image

This is able to draw the image, but a warning pops up:

Warning messages:
1: Use of data[[1]] is discouraged.
ℹ Use .data[[1]] instead.
2: Use of data[[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.
Run rlang::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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions