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

2021-12-30 Thread Kai Yang via R-help
 Thank you all of your help. I'm new in R. I'll follow Bert's and your 
suggestions to post the question in another area in future. 
Happy New Year
Kai
On Thursday, December 30, 2021, 02:05:12 PM PST, CALUM POLWART 
 wrote:  
 
 You will get shot down in flames for posting this here. This list is for 
base-R and your query is tidyverse related so belongs on somewhere tidy 
specific...
But...
mpg[[y]]
Is returning the column mpg[[y]] unfiltered but displ is filtered.
It would be possible to use mpg[[y]][ mpg$hwy<35]
But I suspect there is a better solution involving . As a data source or 
perhaps select to pick only the data you want...

On 30 Dec 2021 18:42, Kai Yang via R-help  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@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.



  
[[alternative HTML version deleted]]

__
R-help@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.


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

2021-12-30 Thread Rui Barradas

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@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.



__
R-help@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.


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

2021-12-30 Thread Bert Gunter
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
 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@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.

__
R-help@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.


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

2021-12-30 Thread Kai Yang via R-help
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@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.