Untitled Flashcards Set
ggplot() - Initializes a ggplot object; typically followed by
aes()andgeom_*()layers.Example:
ggplot(data = df, aes(x = var1, y = var2)) + geom_point()
aes() - Defines aesthetic mappings, such as x and y variables, color, shape, size, etc.
Example:
aes(x = var1, y = var2, color = category)
geom_boxplot() - Creates a boxplot to visualize distribution and outliers of numerical data.
Example:
ggplot(df, aes(x = factor, y = value)) + geom_boxplot()
geom_point() - Creates a scatterplot for visualizing relationships between two numerical variables.
Example:
ggplot(df, aes(x = var1, y = var2)) + geom_point()
geom_histogram() - Creates a histogram to show the distribution of a single numerical variable.
Example:
ggplot(df, aes(x = var1)) + geom_histogram(bins = 30, fill = "blue")
geom_smooth() - Adds a smoothed conditional mean line (such as a LOESS or linear regression curve).
Example:
ggplot(df, aes(x = var1, y = var2)) + geom_smooth(method = "lm")
geom_line() - Creates a line chart, typically used for time series data.
Example:
ggplot(df, aes(x = date, y = value)) + geom_line()
labs() - Adds labels for title, axis labels, and legend.
Example:
+ labs(title = "Plot Title", x = "X-Axis", y = "Y-Axis", color = "Legend")
theme_minimal() - Applies a clean, minimalistic theme to the plot.
Example:
ggplot(df, aes(x, y)) + geom_point() + theme_minimal()
theme() - Customizes various aspects of the plot, such as text size, legend position, and axis appearance.
Example:
+ theme(legend.position = "bottom", text = element_text(size = 12))
scale_x_continuous() - Adjusts the x-axis scale, setting breaks, labels, or limits.
Example:
+ scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, 10))
scale_color_manual() - Manually sets colors for different groups in a plot.
Example:
+ scale_color_manual(values = c("group1" = "red", "group2" = "blue"))
Data Inspection Functions (dplyr/tidyverse)
glimpse() - Provides a compact summary of a dataframe’s structure.
Example:
glimpse(df)
nrow() - Returns the number of rows in a dataframe.
Example:
nrow(df)
ncol() - Returns the number of columns in a dataframe.
Example:
ncol(df)
dim() - Returns both the number of rows and columns in a dataframe.
Example:
dim(df)
Data Manipulation Functions (dplyr/tidyr)
slice_head() - Returns the first n rows of a dataframe.
Example:
slice_head(df, n = 10)
filter() - Filters rows based on a condition.
Example:
filter(df, var1 > 50 & category == "A")
arrange() - Sorts rows by one or more columns.
Example:
arrange(df, desc(var1))
relocate() - Moves selected columns to a new position in the dataframe.
Example:
relocate(df, new_col, .before = first_col)
if_else() - A vectorized conditional function returning values based on a condition.
Example:
df <- df %>% mutate(new_var = if_else(var1 > 50, "High", "Low"))
case_when() - A more flexible conditional function for multiple conditions.
Example:
df <- df %>% mutate(category = case_when(var1 > 75 ~ "High", var1 > 50 ~ "Medium", TRUE ~ "Low"))
count() - Counts occurrences of values in a column.
Example:
count(df, category)
group_by() - Groups data for grouped operations.
Example:
df %>% group_by(category) %>% summarize(avg = mean(var1))
ungroup() - Removes grouping from a dataframe.
Example:
df <- df %>% ungroup()
read_csv() - Reads a CSV file into a dataframe.
Example:
df <- read_csv("data.csv")
separate() - Splits a column into multiple columns based on a delimiter.
Example:
df <- df %>% separate(full_name, into = c("first", "last"), sep = " ")
mutate() - Creates or modifies columns.
Example:
df <- df %>% mutate(new_col = var1 * 2)
summarize() - Aggregates data based on a function (e.g., mean, sum, count).
Example:
df %>% summarize(mean_value = mean(var1, na.rm = TRUE))
pivot_longer() - Converts multiple columns into two columns: one for column names and one for values.
Example:
df <- df %>% pivot_longer(cols = starts_with("Q"), names_to = "Quarter", values_to = "Sales")
pivot_wider() - Converts key-value pairs into multiple columns.
Example:
df <- df %>% pivot_wider(names_from = category, values_from = value)
left_join() - Merges two dataframes, keeping all rows from the left dataframe and matching rows from the right.
Example:
df <- left_join(df1, df2, by = "id")
inner_join() - Merges two dataframes, keeping only rows that appear in both.
Example:
df <- inner_join(df1, df2, by = "id")
right_join() - Merges two dataframes, keeping all rows from the right dataframe.
Example:
df <- right_join(df1, df2, by = "id")
full_join() - Merges two dataframes, keeping all rows from both.
Example:
df <- full_join(df1, df2, by = "id")