I have a technical question regarding this example dataset (using RStudio) :
So I created a function that allows me to conduct descriptive analysis visualisation (it still needs some work) but for now it would look like this (with the use of boxplots as an example) :
library(ggplot2)
library(dplyr)
data("Salaries", package = "carData")
f <- function(x) {
lapply(X = Salaries %>% select_if(is.numeric), FUN = function(X) {
ggplot(Salaries, aes(x, y = X, fill = x, color = x))
geom_boxplot(col = "black")
})
}
lapply(Salaries %>% select_if(is.factor), FUN = function(X) f(X))
So now I am able to visualise boxplots of all possible categorical and continuous variables.
However, I am not able to find a way to make sure that I have different fill colours for each bloxplot. (I would appreciate to know how to apply fill colours automatically and manually).
Thanks.
CodePudding user response:
I am surprised that you get the described problem with the boxplot colors as, when I run your code, the boxplots I obtain are generated with different fill colors, for example:
However, one problem with your code is that you don't see what is being plotted in the graph (you see x
and X
in the axis labels for all plots), and this is a problem with lapply()
because it doesn't give you access to the names of the analyzed variables.
I then suggest the following improvement to your code, so that each plot shows the name of the analyzed variables on the axis labels. The solution was inspired by the first comment by Akrun on