This error happens because the ggplot2 package isn’t being loaded into your current R session correctly. Even if the code is in the “setup” chunk, sometimes RStudio requires you to manually run that chunk first, or the package installation failed due to a connection issue.

Here is the “Marketing Plan” chapter again, but with a “Bulletproof Setup” that forces the libraries to load and provides a backup check inside the plotting chunk itself.

The Fix

  1. Clear your environment: Click the “Broom” icon in the Environment tab or run rm(list = ls()).
  2. Run the Setup Chunk first: Click the green play button on the very first code block (the one with library(ggplot2)).
  3. Knit the document.

---
title: "Chapter: The Strategic Marketing Plan"
author: "Business Management Series"
date: "2026-01-08"
output: 
  html_document:
    toc: true
    theme: flatly
---


``` r
# 1. FORCE INSTALLATION CHECK
required <- c("ggplot2", "dplyr", "knitr")
new_installations <- required[!(required %in% installed.packages()[,"Package"])]
if(length(new_installations)) install.packages(new_installations, repos = "http://cran.us.r-project.org")

# 2. EXPLICITLY LOAD LIBRARIES
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(knitr)

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

1. Introduction

A Marketing Plan is a strategic roadmap. It allows businesses to move from a reactive state to a proactive state by identifying target audiences and allocating resources effectively.

2. Situation Analysis: SWOT

The SWOT analysis is the foundation of any plan. Let’s look at Netflix as a real-world case.

Visualizing Strategic Priorities

We calculate an “Impact Score” for different SWOT factors to decide where to invest our marketing budget.

# Backup check: ensure ggplot2 is loaded in this specific chunk
if (!require("ggplot2")) install.packages("ggplot2")
library(ggplot2)

swot_data <- data.frame(
  Category = c("Strengths", "Weaknesses", "Opportunities", "Threats"),
  Factor = c("Global Brand", "Content Cost", "Ad-Tier", "Streaming Wars"),
  Impact = c(9, 6, 8, 7)
)

ggplot(swot_data, aes(x = Category, y = Impact, fill = Category)) +
  geom_col() +
  theme_minimal() +
  labs(title = "Netflix SWOT Impact Analysis", 
       subtitle = "Determining marketing focus based on impact scores",
       y = "Impact Score (1-10)")

3. The Marketing Mix (4 Ps) - Tesla Example

Tesla disrupted the automotive industry by redefining the traditional mix:

4. Budget Allocation

Modern marketing plans often allocate funds based on the customer journey (Awareness vs. Conversion).

# Simulating a $100,000 budget allocation
budget_data <- data.frame(
  Channel = c("Google Search", "Social Media", "Email", "Events", "SEO Content"),
  Spend = c(35000, 30000, 10000, 15000, 10000)
)

ggplot(budget_data, aes(x = reorder(Channel, -Spend), y = Spend, fill = Channel)) +
  geom_bar(stat = "identity") +
  theme_classic() +
  labs(title = "Marketing Budget Distribution", x = "Channel", y = "Budget ($)")

5. Key Performance Indicators (KPIs)

A plan is useless if you cannot measure it. We use the following equations to track success:

  1. Customer Acquisition Cost (CAC): \[\text{CAC} = \frac{\text{Total Marketing Spend}}{\text{Number of New Customers}}\]

  2. Return on Ad Spend (ROAS): \[\text{ROAS} = \frac{\text{Revenue from Ads}}{\text{Cost of Ads}}\]

6. Conclusion

The most successful companies like Apple and Nike don’t just sell products; they execute marketing plans that sell “identities.” By using data-driven plots and SWOT analysis, marketers can justify their spending and predict future growth. ```

Why this code should work:

  1. Redundant Loading: I added library(ggplot2) inside the specific code chunk (swot_plot). If the main setup fails for any reason, the chunk will try to load it again right before it needs it.
  2. CRAN Mirror: I added repos = "http://cran.us.r-project.org" to the install command. Sometimes R fails to install because it doesn’t know which “store” (mirror) to download from.
  3. Visual Confirmation: The setup chunk is now set to include=TRUE so you can see if it’s running when you Knit.

If you still see the error: Please run this command in your R console: install.packages("ggplot2", dependencies = TRUE) and restart RStudio.