'What's the difference between placing the aes() function inside the ggplot() function or inside the geom_point/geom_bar/geom_line () functions?
I am new to programming and coding, trying to learn R in the Google course.
They have given several examples for visuals using the ggplot functions, but they have used the aes() in two ways.
First:
ggplot(data=palmerpenguins) + geom_point(mapping = aes(x = bill_length_mm,y = body_mass_g)) The aes() function is inside the geom_point() function.
Then they show: ggplot(data, aes(x=distance, y= dep_delay, color=carrier, size=air_time, shape = carrier)) + geom_point()
Now the aes() function is in the ggplot() function, where they specify the dataset.
What is the reason for the switch? It seems like aes() can go in either place. Is this true? For something that is so specific like coding, it's confusing why you could do it either way. Any explanation would be appreciated. Thanks
Solution 1:[1]
If you define the mapping (use aes()) inside the ggplot call, you create a set of default mapping values for all the attached geoms.
If you want to apply different mappings for each geom you add, you can define them in an aes() call within the geom itself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jay Achar |
