I have a dataset looks like this:
01/02/2013 02/02/2013 03/02/2013 04/02/2013
1 2 3 3
2 1 6 7
3 3 4 2
4 1 1 8
I want to make a graph with n boxplots according to the number of the columns in my dataset, where each boxplot only contains one variable which is its corresponding column. So in this case, there would be 4 boxplots.
I used boxplot()
function and it worked for my data, however I want to use geom_jitter()
from ggplot2
to beautify my plots. And ggplot2
requires both x and y axes where I don't really have with my dataset.
This is what I want for my plot:
CodePudding user response:
Bring your data in long format with
pivot_longer
fromtidyr
package (is intidyverse
)use
ggplot
fromggplot2
package (is also intidyverse
)geom_boxplot
andgeom_jitter
if needed.
library(tidyverse)
df %>%
mutate(id = row_number()) %>%
pivot_longer(
cols = starts_with("X"),
names_to = "names",
values_to = "values"
) %>%
ggplot(aes(x=names, y=values, fill=names))
geom_boxplot()
geom_jitter(aes(y=values))