Kai Yang
2021-Dec-30 18:42 UTC
[R] question about error message: "Aesthetics must be either length 1 or the same as the data (226): y and colour"
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]]
Bert Gunter
2021-Dec-30 19:06 UTC
[R] question about error message: "Aesthetics must be either length 1 or the same as the data (226): y and colour"
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 at 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 at 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.
Rui Barradas
2021-Dec-30 19:29 UTC
[R] question about error message: "Aesthetics must be either length 1 or the same as the data (226): y and colour"
Hello, This seems like a repetition of a question you already asked and it has nothing to do with the fill aesthetic. The error message is caused by the maps y = mpg[[y]] color = mpg[[c]] First you filter the data keeping only a subset of the rows but now you are using the full data. This causes the error, the sizes of the filtered data and of the full data are not the same. mpg %>% filter(hwy < 35) %>% dim() #[1] 226 11 y <- "hwy" length(mpg[[y]]) #[1] 234 If you pass a string as variable, you must ?get the variable value, like in the first function below, plot1. plot1 <- function(y, c, f){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ, y = get(y), color = get(c))) + geom_point() ggsave(paste0("~/tmp/", f ,".jpg"), width = 9, height = 6, dpi = 1200, units = "in") } plot1("hwy","cyl","hwy_cyl_f") Or you can use aes_string. But since the x axis is fixed, not variable, pay attention to not mix a variable (displ) with variables' names (hwy and cyl). So in the next function I use aes() in the initial call to ggplot and aes_string in the geom_point layer. plot2 <- function(y, c, f){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ)) + geom_point(aes_string(y = y, color = c)) ggsave(paste0("~/tmp/", f ,".jpg"), width = 9, height = 6, dpi = 1200, units = "in") } plot2("hwy","cyl","hwy_cyl_f2") Hope this helps, Rui Barradas ?s 18:42 de 30/12/21, Kai Yang via R-help escreveu:> 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 at 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. >