R语言中基于openintro包teacher数据框的绘图及统计问题求助
Hey there! Let's work through your problem with the teacher dataset from the openintro package. I'll help you fix the plotting issues, complete the descriptive statistics (both numerical and visual), and analyze how base salary relates to each of your target variables.
1. First: Load Data & Clean Up Your Average Salary Calculation
First, let's simplify your existing code for calculating average base salaries by degree—using dplyr makes this cleaner and more scalable:
# Load required packages library(openintro) library(dplyr) library(ggplot2) # For easier, more customizable plotting # Load the dataset dat <- openintro::teacher # Calculate average base salary by degree (cleaner approach) degree_salary_means <- dat %>% group_by(degree) %>% summarize(mean_base_salary = mean(base, na.rm = TRUE)) print(degree_salary_means)
2. Descriptive Statistics (Numerical + Visual)
Let's cover each of your target variables:
Numerical Descriptions
# For categorical variables (degree, fte, retirement) cat_stats <- list( degree_table = table(dat$degree), fte_table = table(dat$fte), retirement_table = table(dat$retirement) ) # For continuous variables (years, base salary) cont_stats <- dat %>% select(years, base) %>% summarize( across(everything(), list( mean = ~mean(., na.rm = TRUE), median = ~median(., na.rm = TRUE), sd = ~sd(., na.rm = TRUE), min = ~min(., na.rm = TRUE), max = ~max(., na.rm = TRUE) )) ) # Print results print("Categorical Variable Counts:") print(cat_stats) print("\nContinuous Variable Summary:") print(cont_stats)
Visual Descriptions
Categorical Variables (Bar Plots)
# Degree distribution ggplot(dat, aes(x = degree)) + geom_bar(fill = "steelblue") + labs(title = "Distribution of Teacher Degrees", x = "Degree Type", y = "Count") + theme_minimal() # FTE (Full-Time Equivalent) distribution ggplot(dat, aes(x = fte)) + geom_bar(fill = "coral") + labs(title = "Distribution of FTE Status", x = "FTE Category", y = "Count") + theme_minimal() # Retirement eligibility distribution ggplot(dat, aes(x = retirement)) + geom_bar(fill = "forestgreen") + labs(title = "Retirement Eligibility", x = "Eligible for Retirement?", y = "Count") + theme_minimal()
Continuous Variables (Histograms + Box Plots)
# Years of experience histogram ggplot(dat, aes(x = years)) + geom_histogram(binwidth = 5, fill = "purple", alpha = 0.7) + labs(title = "Distribution of Teacher Experience", x = "Years of Service", y = "Count") + theme_minimal() # Base salary box plot (to spot outliers) ggplot(dat, aes(y = base)) + geom_boxplot(fill = "orange", alpha = 0.7) + labs(title = "Distribution of Base Salaries", y = "Base Salary ($)") + theme_minimal()
3. Analyzing Base Salary vs. Target Variables
Now let's visualize how base salary relates to each variable:
Base Salary vs. Degree
This expands on your average calculation with a box plot to show salary spread:
ggplot(dat, aes(x = degree, y = base)) + geom_boxplot(fill = "steelblue", alpha = 0.7) + geom_text(data = degree_salary_means, aes(label = round(mean_base_salary, 2), y = mean_base_salary + 1000)) + labs(title = "Base Salary by Degree Type", x = "Degree", y = "Base Salary ($)") + theme_minimal()
Base Salary vs. FTE
ggplot(dat, aes(x = fte, y = base)) + geom_boxplot(fill = "coral", alpha = 0.7) + labs(title = "Base Salary by FTE Status", x = "FTE Category", y = "Base Salary ($)") + theme_minimal()
Base Salary vs. Years of Experience
A scatter plot with a trend line to show the relationship:
ggplot(dat, aes(x = years, y = base)) + geom_point(alpha = 0.5, color = "purple") + geom_smooth(method = "lm", se = FALSE, color = "black") + # Linear trend line labs(title = "Base Salary vs. Years of Experience", x = "Years of Service", y = "Base Salary ($)") + theme_minimal()
Base Salary vs. Retirement Eligibility
ggplot(dat, aes(x = retirement, y = base)) + geom_boxplot(fill = "forestgreen", alpha = 0.7) + labs(title = "Base Salary by Retirement Eligibility", x = "Eligible for Retirement?", y = "Base Salary ($)") + theme_minimal()
Troubleshooting Your Original Plotting Issue
If you were using base R plotting before, common issues include:
- Forgetting to handle
NAvalues (addna.rm = TRUEin calculations/plotting) - Trying to plot categorical vs. continuous variables with incompatible functions (e.g.,
plot(dat$degree, dat$base)works but is less polished than ggplot) - Not loading required packages for custom plots
The ggplot2 code above avoids most of these pitfalls and produces publication-ready visuals.
内容的提问来源于stack exchange,提问作者John John

