Ggplot using group_by
Let's say I measured some values ββin two experiments, an example of a toy in R:
set.seed(9)
df <- data.frame(
exp=c(rep(1,10), rep(2,10)),
value=runif(20,0,3))
Then I assign a categorical variable level
based on value
:
require("dplyr")
df <- df %>% mutate(level = ifelse(value<1, "low", ifelse(value>2, "high", "intermediate")))
I can display geom_point()
geom ( ggplot2
package) value
for two groups exp
:
require("ggplot2")
ggplot(df, aes(x=factor(exp), y=value))+geom_point()
My problem: How can I break the display using facet_wrap()
on level
to get a 3x3 plot geom_point()
with nine combinations level
for two exp
(for example, "high", "medium" and "low" vertically for experiment 1 and horizontally for experiment 2. In other words, the top left graph represents those value
that are "high" in experiments 1 and 2, the first column of the first row is those that are "high" in experiment 1 and "intermediate" in experiment 2, etc.). Can I somehow use group_by()
the call ggplot2, or do I need to do variable level.exp1
and level.exp2
for the face?
UPDATE: I don't seem to explain this properly, so to clarify what I need: I would like to facet_wrap()
instead repeat this code 9 times with different conditions filter()
(i.e. For all 9 combinations level
in experiments 1 and 2)
df %>% filter((exp==1 & level=="high") | (exp==2 & level=="high")) %>%
ggplot(aes(x=factor(exp), y=value))+geom_point()
Handmade makeup and unrelated to the df
above and fake legends can give an idea of ββwhat I want:
There must be a gentle solution - I couldn't find a solution to a dcast()
.
source to share
No one has answered this question yet
Check out similar questions: