[R] question about error message: "Aesthetics must be either length 1 or the same as the data (226): y and colour"

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Dec 30 20:06:18 CET 2021


1. Please read and follow the Posting Guide linked below. Among other
things, it says:

"For questions about functions in standard packages distributed with R
(see the FAQ Add-on packages in R), ask questions on R-help."
[The link is:
https://cran.r-project.org/doc/FAQ/R-FAQ.html#Add-on-packages-in-R
This gives the list of current _standard_ packages]

"If the question relates to a contributed package [ggplot is such a
package] , e.g., one downloaded from CRAN, try contacting the package
maintainer first. You can also use find("functionname") and
packageDescription("packagename") to find this information. Only send
such questions to R-help or R-devel if you get no reply or need
further assistance. This applies to both requests for help and to bug
reports."

Note that RStudio maintains its own help resources at:
https://community.rstudio.com/
This is where questions about the TidyVerse, ggplot, etc. should be posted.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Thu, Dec 30, 2021 at 10:43 AM Kai Yang via R-help
<r-help using r-project.org> wrote:
>
> Hi R team,
> I can create a plot using the code below:
> library(ggplot2)
> library(dplyr)
> mpg %>%
>   filter(hwy <35) %>%
>   ggplot(aes(x = displ, y = hwy, color = cyl)) +
>   geom_point()
> ggsave("c:/temp/hwy_cyl.jpg",width = 9, height = 6, dpi = 1200, units = "in")
>
>
> I want to do the exactly same work using function. Below is the function I created:
> plot1 <- function(y, c, f){
>           mpg %>%
>             filter(hwy <35) %>%
>             ggplot(aes(x = displ, y = mpg[[y]], color = mpg[[c]] )) +
>             geom_point()
>           ggsave(paste0("c:/temp/", f ,".jpg"), width = 9, height = 6, dpi = 1200, units = "in")
> }
> plot1("hwy","cyl","hwy_cyl_f")
>
> But I got error message when I run the code: "Aesthetics must be either length 1 or the same as the data (226): y and colour" . I checked online about the message. My understanding is: I need to add "fill" in geom_point() statement. My questions are:
> 1. is it possible to make the code work without add 'fill' in geom_point() statement, but keep the color as same as the first code?
> 2. if I must add 'fill' option in geom_point() statement, how to add them in? Should I add 266 colors name after 'fill'?
> 3. this is my first function in R, I'm sure there are many problems in the code. please point out my error in the code.
>
>
> Thank you,
> Kai
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list