Here I am looking at the mtcars
dataset.
I create a density plot for the wt
variable using the ggplot2
package. I also the geom_vline()
layer to add a vertical line for the mean of wt
.
ggplot(mtcars, aes(x = wt)) +
geom_density() +
geom_vline(xintercept = mean(mtcars$wt))
I then switch the syntax a bit to start with the start with the dataframe and then move to ggplot
. I do this because I want to add a step where I create a new variable. In this example, I create wt2
which is wt ^ 2
.
mtcars %>%
mutate(wt2 = wt ^ 2) %>%
ggplot(aes(wt2)) +
geom_density()
I find that I am no longer able to add a geom_vline()
layer in the same way that I did before, using this new syntax. Is there something I am doing wrong?
mtcars %>%
mutate(wt2 = wt ^ 2) %>%
ggplot(aes(wt2)) +
geom_density() +
geom_vline(xintercept = mean(mtcars$wt2))
Now, the code below creates the graph I want, but only after creating a new table, which I want to avoid. I want to avoid this workflow because I'm working with a large dataset and it's creating memory issues to create new tables / I don't seem to have space in the environment I'm using.
mtcars_new_df <- mtcars %>% mutate(wt2 = wt ^ 2)
mtcars_new_df %>% ggplot(aes(wt2)) + geom_density() + geom_vline(xintercept = mean(mtcars$wt2))
The reason I want to avoid a workflow where I create a new dataframe is because of memory and time issues. (I'm using a dataset much larger than the mtcars
dataset.)