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

అనుకూలీకరణ మరియు కోణాలు

Beyond defining geometries and aesthetics, ggplot2 allows you to completely customize the visual look of a plot by adding titles, modifying themes, and faceting (splitting plots into sub-panels).


Titles and Labels: labs()

To add a main title, subtitles, and customize axis labels or legend titles, we add the labs() layer:

Code
ggplot(df, aes(x = age, y = income)) +
  geom_point() +
  labs(
    title = "Income by Age",
    subtitle = "2026 census data",
    x = "Age (years)",
    y = "Annual Income (EUR)",
    color = "Gender"
  )

Visual Themes: theme_*()

ggplot2 includes several pre-packaged themes that change backgrounds, grid lines, and typography. Some of the most common ones are:

  • theme_gray() (the default theme with a light gray background).
  • theme_minimal() (a clean white background with thin grid lines).
  • theme_classic() (a simple classic style, with no background grid lines).
Code
ggplot(df, aes(x = age, y = income)) +
  geom_point() +
  theme_minimal()  # Applies a clean, modern style

Splitting Plots into Sub-panels: facet_wrap()

Faceting allows you to split a single plot into multiple subplots (panels) side-by-side based on the value of a categorical variable.

The primary function is facet_wrap() and uses R formula notation (~ variable_name):

Code
# Creates a separate subplot for each department
ggplot(df, aes(x = age, y = income)) +
  geom_point() +
  facet_wrap(~ department)

Try it yourself

Exercise 1: Add titles and axes

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

Given the scatter plot, add a labs() layer setting the title (title) to 'Title', the x-axis label to 'Age', and the y-axis label to 'Income'.

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

Use labs(title = 'Title', x = 'Age', y = 'Income') concatenating it with '+'.

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

Exercise 2: Apply a clean theme

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

Apply the theme_minimal() graphic theme to the scatter plot below to improve its visual aesthetics.

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

Use the '+' sign and add the theme_minimal() function.

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

Exercise 3: Split the plot with facet_wrap

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

Use the facet_wrap() function to split the scatter plot into different panels based on the department column.

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

Use facet_wrap(~ department) to split the plot.

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

Exercise 4: Flip coordinate axes

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

To make a column chart with many categories readable, you can flip the x and y axes. Add the coord_flip() layer to the bar chart.

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

Usa coord_flip() legandolo alla pipeline ggplot con '+'.

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

Exercise 5: Complete publication-ready plot

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

Create a complete plot on df: map age to x, income to y, and color to gender inside geom_point(). Split the plot using facet_wrap() by department, add labs() with title 'Salary by Age', and finally apply theme_minimal().

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

Combine all layers using '+': ggplot(...) + geom_point() + facet_wrap(~ department) + labs(title = 'Salary by Age') + theme_minimal()

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