Install Package & Libraries

#install.packages('tidyverse')
#install.packages('ggplot2')
#install.packages('palmerpenguins')
library('tidyverse')
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library('ggplot2')
library('palmerpenguins')

View data frame

data(penguins)
view(penguins)
head(penguins)
## # A tibble: 6 × 8
##   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
##   <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
## 1 Adelie  Torgersen           39.1          18.7               181        3750
## 2 Adelie  Torgersen           39.5          17.4               186        3800
## 3 Adelie  Torgersen           40.3          18                 195        3250
## 4 Adelie  Torgersen           NA            NA                  NA          NA
## 5 Adelie  Torgersen           36.7          19.3               193        3450
## 6 Adelie  Torgersen           39.3          20.6               190        3650
## # ℹ 2 more variables: sex <fct>, year <int>

Creating a plot chart

The plot shows a positive relationship between the two variables. In other words, the larger the penguin, the longer the flipper.

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

Similar code:

 ggplot(data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g)) +  geom_point()
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

with color

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species))
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##shapes

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species, shape=species))
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##size

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species, shape=species, size=species))
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##alpha (transparency)

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color=species, shape=species, alpha=species))
## Warning: Using alpha for a discrete variable is not advised.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##color= purple

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color="purple", shape=species, alpha=species))
## Warning: Using alpha for a discrete variable is not advised.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##Types of Plots - Geom functions

geom_point

ggplot(data = penguins) + 
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##geom_smoooth

ggplot(data = penguins) + 
  geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).

##geom_smoooth + geom_point

ggplot(data = penguins) + 
  geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g))+
  geom_point (mapping=aes(x=flipper_length_mm, y= body_mass_g))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##geom_smooth (with linetype)

ggplot(data = penguins) + 
  geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g, linetype=species))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).

##geom_jitter

ggplot(data = penguins) + 
  geom_jitter(mapping = aes(x = flipper_length_mm, y = body_mass_g))
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

##Geom_bar- Data Diamond

ggplot(data=diamonds)+
  geom_bar(mapping = aes(x = cut))

##geom_bar (color)

ggplot(data=diamonds)+
  geom_bar(mapping = aes(x = cut, color=cut))

##geom_bar (fill)

ggplot(data=diamonds)+
  geom_bar(mapping = aes(x = cut, fill=cut))

##geom_bar (fill= clarity)

ggplot(data=diamonds)+
  geom_bar(mapping = aes(x = cut, fill=clarity))

##Facets - simple example ## Facet_wraps

ggplot(data=penguins)+
  geom_point(mapping = aes(x = flipper_length_mm, y=body_mass_g,color=species))+
  facet_wrap(~species)
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

other example

ggplot(data=diamonds)+
  geom_bar(mapping = aes(x = color, fill=cut))+
  facet_wrap(~cut)

## Facet_grid()

ggplot(data=penguins)+
  geom_point(mapping = aes(x = flipper_length_mm, y=body_mass_g,color=species))+
  facet_grid(sex~species)
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).