ప్రధాన కంటెంట్‌కు వెళ్లండి
eLearner.app
మాడ్యూల్ 5 · 2లో పాఠం 1కోర్సులో 9/10~12 min
మాడ్యూల్ పాఠాలు (1/2)

వ్యాకరణం మరియు ప్రాథమిక జ్యామితి

Data visualization in R is dominated by ggplot2, a package based on the "Grammar of Graphics". This theory breaks down graphs into independent semantic components, such as data, aesthetic mappings, and geometric elements.


The Structure of ggplot2

Every plot created with ggplot2 starts with a call to the ggplot() function, where we supply a data frame and define how columns map to visual properties using the aes() (aesthetics) function.

Next, we add geometric layers using the + operator (note: we use + and not %>% in ggplot2).

The basic template is:

Code
ggplot(data = <DATA_FRAME>, mapping = aes(<AESTHETICS>)) +
  <GEOMETRY>()

Aesthetic Mappings (aes) and Geometries (geom_*)

The aes() function defines which variables in your data control the plot's visual attributes:

  • x: Position on the horizontal axis.
  • y: Position on the vertical axis.
  • color: Color of points or lines.
  • fill: Fill color of closed shapes (e.g. bars).
  • size: Size of elements.

Geometries (geom_) define the type of visual representation:

1. Scatter Plot (geom_point())

Ideal for displaying the relationship between two numeric variables.

Code
ggplot(df, aes(x = age, y = income)) +
  geom_point()

2. Line Plot (geom_line())

Commonly used for time series or trend lines.

Code
ggplot(df, aes(x = year, y = sales)) +
  geom_line()

3. Bar Plot (geom_bar() and geom_col())

By default, geom_bar() counts the frequency of categories, while geom_col() represents the actual values stored in a y column directly.

Code
# Plots the value of the 'sales' column for each 'category'
ggplot(df, aes(x = category, y = sales)) +
  geom_col()

Try it yourself

Exercise 1: Create a basic scatter plot

వ్యాయామం#r.m5.l1.e1
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Create a scatter plot using the df data frame. Map the height column to the x-axis and the weight column to the y-axis. Use geom_point().

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Assicurati di usare ggplot(df, aes(x = height, y = weight)) + geom_point()

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 2: Add color based on data

వ్యాయామం#r.m5.l1.e2
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Modify the previous plot by mapping the color property to the gender column inside aes() to color the points by gender.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Includi color = gender all'interno di aes(), ad esempio: aes(x = height, y = weight, color = gender).

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 3: Line plot

వ్యాయామం#r.m5.l1.e3
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Create a line plot using df. Map the year column to the x-axis and the sales column to the y-axis. Add the geom_line() geometric layer.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Usa ggplot(df, aes(x = year, y = sales)) + geom_line()

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 4: Bar plot with geom_col

వ్యాయామం#r.m5.l1.e4
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Create a bar plot to display values directly. Map the category column to the x-axis and the value column to the y-axis. Use the geom_col() geometry.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Usa ggplot(df, aes(x = category, y = value)) + geom_col()

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Exercise 5: Combine multiple geometries

వ్యాయామం#r.m5.l1.e5
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

In ggplot2 you can stack multiple geometric layers. Create a plot that combines both geom_point() and geom_smooth(), mapping x_val to the x-axis and y_val to the y-axis.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

Aggiungi sia geom_point() che geom_smooth() separati dal segno '+'.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది