Im trying to make a plot with six boxplots in it,
Im not sure exactly how to start with this but using ggplot
Im hoping to get a series of 6 boxplots in a row:
- 3 boxplots for the three variables under
A5
(1,0, and NA) - 3 boxplots for the variables under
A9
(1, 0, and NA). - Then populate the boxplot with the respective value from
maf
. I've been trying to plot them but I get either just the maf column plotted or a grey slab. Any help would be amazing thank you.
A5 A9 maf
1 1 NA 9.33843e-01
2 1 1 2.07735e-01
3 1 1 9.28438e-01
4 1 1 7.17963e-01
5 0 NA 1.02935e-01
6 1 1 9.28281e-01
7 1 1 9.28100e-01
8 1 1 2.85170e-01
9 1 1 2.67085e-01
10 1 1 9.28339e-01
11 1 1 5.68334e-02
12 1 1 4.87848e-01
13 1 1 2.22414e-01
14 1 1 3.37689e-01
15 1 1 1.09452e-01
16 1 1 4.71361e-02
17 NA NA 6.87674e-01
18 0 0 5.03323e-01
19 1 1 8.93953e-01
20 1 1 9.13662e-01
EDIT---------------------------------------------------------- When running the code on the original data with e-9 exponents:
df %>%
pivot_longer(
-maf
) %>%
ggplot(aes(name, maf, fill=factor(value)))
geom_boxplot()
I get this image:
Edit 2-----------------------------------------------------------
structure(list(A5 = c(1L, 1L, 1L, 1L, 0L, 1L), A9 = c(NA, 1L,
1L, 1L, NA, 1L), maf = c("9.33843e-01", "2.07735e-01", "9.28438e-01",
"7.17963e-01", "1.02935e-01", "9.28281e-01")), row.names = c(NA,
6L), class = "data.frame")
CodePudding user response:
Update with new data:
df %>%
pivot_longer(
-maf
) %>%
ggplot(aes(factor(name), as.numeric(maf), fill=factor(value)))
geom_boxplot()
scale_y_continuous(labels = function(x) format(x, scientific = FALSE))
First answer:
Bring your data in long format then use geom_boxplot()
library(tidyverse)
df %>%
pivot_longer(
-maf
) %>%
ggplot(aes(name, maf, fill=factor(value)))
geom_boxplot()
data:
df <- structure(list(A5 = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, NA, 0L, 1L, 1L), A9 = c(NA, 1L, 1L, 1L,
NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 0L, 1L, 1L
), maf = c(0.933843, 0.207735, 0.928438, 0.717963, 0.102935,
0.928281, 0.9281, 0.28517, 0.267085, 0.928339, 0.0568334, 0.487848,
0.222414, 0.337689, 0.109452, 0.0471361, 0.687674, 0.503323,
0.893953, 0.913662)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))