[R] question about error message: "Aesthetics must be either length 1 or the same as the data (226): y and colour"
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Dec 30 20:29:12 CET 2021
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 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