Reputation: 11
I have a question about plotting in ggplot, or r in general.
If I have a data frame that looks something like this:
df <- data_frame(Patients = c(1:10),
test1 = sample(c(0,1), 10, replace = TRUE),
test2 = sample(c(0,1), 10, replace = TRUE),
test3 = sample(c(0,1), 10, replace = TRUE),
test4 = sample(c(0,1), 10, replace = TRUE))
Then the output looks like this.
# A tibble: 10 x 5
Patients test1 test2 test3 test4
<int> <dbl> <dbl> <dbl> <dbl>
1 1 0 1 1 0
2 2 1 0 1 1
3 3 1 0 0 1
4 4 1 1 0 1
5 5 1 0 1 1
6 6 1 0 0 1
7 7 1 1 1 0
8 8 1 0 0 1
9 9 1 0 1 1
10 10 0 1 0 1
How can I make a graph where the patients are on the x-axis as discrete entries, and the y axis has four discrete entries, one for each test?
For example:
I would also like to order the x axis by the number of matching tiles, like this.
Thanks very much for you time.
Upvotes: 1
Views: 274
Reputation: 93761
We reshape the data into long format, calculate the number of tests by patient and use that to convert Patients
to a factor with the desired ordering.
library(tidyverse)
library(forcats)
library(stringr)
theme_set(theme_minimal())
set.seed(2)
df <- data_frame(Patients = c(1:10),
test1 = sample(c(0,1), 10, replace = TRUE),
test2 = sample(c(0,1), 10, replace = TRUE),
test3 = sample(c(0,1), 10, replace = TRUE),
test4 = sample(c(0,1), 10, replace = TRUE))
ggplot(gather(df, Test, value, -Patients) %>%
group_by(Patients) %>%
mutate(Test = str_to_title(gsub("(.*)([0-9])", "\\1 \\2", Test)),
numTests = sum(value)) %>%
ungroup %>%
arrange(numTests, Patients) %>%
mutate(Patients = fct_inorder(factor(Patients))),
aes(Patients, Test, fill=factor(value))) +
geom_tile(colour="grey60", lwd=0.8, show.legend=FALSE) +
scale_fill_manual(values=c("white","black")) +
labs(x="Patient", y="")
Upvotes: 0