library(ggplot2)
data(“swiss”)
summary(swiss) #providing a summary of each variable in the dataset head(swiss) #view the first rows of the dataset
mean(swiss\(Fertility) sd(swiss\)Fertility)
summary(swiss) #providing a summary of each variable in the dataset head(swiss) #view the first rows of the dataset
mean(swiss\(Fertility) sd(swiss\)Fertility)
mean(swiss\(Education) sd(swiss\)Education) mean(swiss\(Fertility) sd(swiss\)Fertility)
#plot the correlation between fertility and education cor(swiss\(Fertility, swiss\)Education) plot(swiss\(Fertility, swiss\)Education)
ggplot(data = swiss, aes(x = Fertility, y = Agriculture)) + geom_point()
ggplot(data = swiss, aes(x = Fertility, y = Agriculture)) + geom_point(color = “grey”) + theme_minimal() + labs(title = “Fertility and Agriculture in Switzerland”)
ggplot(data = swiss, aes(x = Fertility, y = Agriculture)) + geom_point(color = “blue”) + theme_minimal() + labs(title = “Fertility and Agriculture in Switzerland”)
ggplot(data = swiss, aes(x = Fertility, y = Agriculture)) + geom_point(color = “blue”) + theme_minimal() + labs(title = “Fertility and Agriculture in Switzerland”) + geom_smooth(method = “lm”, se = FALSE)
ggplot(data = swiss, aes(x = Fertility, y = Agriculture)) + geom_point(color = “blue”) + theme_minimal() + labs(title = “Fertility and Agriculture in Switzerland”) + geom_smooth(method = “lm”, se = FALSE, linetype = “dashed”)
#create a density plot of the variable fertility ggplot(swiss, aes(x = Fertility)) + geom_bar(stat = “density”, fill = “skyblue”) + labs(title = “Fertility Rates in Swiss Provinces”, y = “Density”, x = “Fertility Rate”) + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotate x-axis labels for better readability
ggplot(swiss, aes(x = Fertility)) + geom_bar(stat = “density”, fill = “skyblue”) + labs(title = “Fertility Rates in Swiss Provinces”, y = “Density”, x = “Fertility Rate”) + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + # Rotate x-axis labels for better readability geom_vline(aes(xintercept = mean(Fertility)), color = “red”, linetype = “dashed”, size = 1) + geom_text(aes(x = 80, y = 0.02, label = “Mean Fertility”), color = “red”, size = 4)
swiss$Cantones <- rownames(swiss) head(swiss)
data(“swiss”) library(tibble) swiss <- as_tibble(swiss, rownames = “Cantons”)
#i want to remove all objects from my environment rm(list = ls())
#load dplyr package library(dplyr) library(ggplot2)
#load swiss data data(swiss)
#I want to change the column Catholic to a factor of 1 if the value is above 50 and 0 if the value is below 50 swiss\(Catholic <- ifelse(swiss\)Catholic > 50, 1, 0) swiss
#Explore the mean education level of the Catholic and non-Catholic cantons swiss %>% group_by(Catholic) %>% summarise(mean(Education))
#Filter the cantons with education level above 10 high_education <- swiss %>% filter(Education > 10)
#Explore the correlation between high education cantons and fertility cor_test_result <- cor.test(high_education\(Fertility, high_education\)Education) cor_test_result
#Visualize the correlation between high education cantons and fertility ggplot(high_education, aes(x = Fertility, y = Education)) + geom_point() + geom_smooth(method = “lm”, se = FALSE) + labs(title = “Correlation between high education cantons and fertility”, x = “Fertility”, y = “Education”)