I have a data frame with many columns. The first column contains categories such as "System 1", "System 2", and the second column has numbers that represent the 0's and 1's.
For example:
SYSTEM | Q1 | Q2 |
---|---|---|
S1 | 0 | 1 |
S1 | 1 | 0 |
S2 | 1 | 1 |
S2 | 0 | 0 |
S2 | 1 | 1 |
How to write R code to produce a violin plot for all the questions(Q1,Q2,Q3,..Q10) at the same time.
p <- ggplot(mydata, aes(x =system, y = q1, fill=system, color=system)) +
ggplot(mydata, aes(x =system, y = q2, fill=system, color=system)) +
ggplot(mydata, aes(x =system, y = q3, fill=system, color=system)) +
ggplot(mydata, aes(x =system, y = q100, fill=system, color=system)) +
geom_violin(show.legend=FALSE) +
stat_summary(fun.y=median, geom="point", size=2, color="red")+
coord_flip()
save pdf file
filename <- fs::path(knitr::fig_path(), "file.pdf")
ggsave(filename)
plot(p)
invisible(dev.off())
knitr::include_graphics(filename)
I'm new to R and I spent a very long time but it doesn't work at all.
I apply the code suggested by @Rui Barradas then I got all the plots compare to each other. Actually, what I want is to get one figure that include 2 plots for each system such system 1 then system 2 side by side and the title is Q1.
Pleas I asked for how to save pdf files for each figure by using for-loop to save 100 pdf files in a folder.
Thank you again.