ggplot(df_input) + geom_jitter(aes(!!sym(df_char1_name), !!sym(df_numeric1_name))) + geom_boxplot(aes(!!sym(df_char1_name), !!sym(df_numeric1_name)))
However, if we map our x and y values in the ggplot function we find that we generate the same graph
ggplot(
r dataframe_name, aes(x=
r df_char1_name, y=
r df_numeric1_name) +
geom_jitter() +
geom_boxplot()
ggplot(df_input, aes(!!sym(df_char1_name), !!sym(df_numeric1_name))) + geom_jitter() + geom_boxplot()
This is because when you set the aes mappings in the original ggplot
function you are setting the aes
globally.
This means all the functions afterwards will inherit that mapping. So in our example, this means that both the jitter and boxplot geoms know to graph the same information
You can also set aes values locally within the geom function. Doing so will only change the values in that geom
ggplot(
r dataframe_name, aes(x=
r df_char1_name, y=
r df_numeric1_name) +
geom_jitter() +
geom_boxplot(aes(color =
r df_char1_name))
ggplot(df_input, aes(!!sym(df_char1_name), !!sym(df_numeric1_name))) + geom_jitter() + geom_boxplot(aes(color = !!sym(df_char1_name)))
mean <- mean(df_numeric2_vec) sd <- sd(df_numeric2_vec)
Data can also be set locally or globally. For this example, let's filter our original data first using the dplyr::filter
function
df_filter <-
r dataframe_name%>% filter(
r df_numeric2_name>
r round(mean + 2*sd))
*this number is two standard deviations above the mean
value of r df_numeric2_name
Now, let's identify only the r dataframe_about
in our data that are outliers, more than 2SD above the mean, by setting data locally in a new geom
ggplot(
r dataframe_name, aes(x=
r df_char1_name, y=
r df_numeric1_name) +
geom_jitter() +
geom_boxplot(aes(color =
r df_char1_name)) +
geom_label(data=df_filter, aes(label=
r df_id_name))
df_filter <- df_input %>% filter(!!sym(df_numeric2_name) > round(mean + 2*sd)) ggplot(df_input, aes(!!sym(df_char1_name), !!sym(df_numeric1_name))) + geom_jitter() + geom_boxplot(aes(color = !!sym(df_char1_name))) + geom_label_repel(data = df_filter, aes(label = !!sym(df_id_name)))
You notice we have to indicate the new dataset, but because it has the same x and y values, we did not need to set those mappings
Go to code/
Open 06_global_v_local.Rmd
Complete the exercise to practice mapping locally and globally.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.