Open rpruim opened 6 years ago
Comment from @AmeliaMN regarding ggformula
and ggpplot2
:
I would love to see a document translating between ggplot2 and ggformula. I haven't spent much time pushing on it, but my initial impression is that ggformula didn't have the full power of ggplot2. I'd love to be proven wrong!
Let's create a list of things that would be good to include in a side-by-side comparison.
This is more of a "challenge" plot than anything, but I often use this code as an example when I teach ggplot2
to show how complicated plots can be built up from many layers. It comes from my COST paper.
colors1 <- brewer.pal(5, "Spectral")
colorsB <- colors1[c(2,3,4,5,1)]
baseplot <- ggplot(mapping = aes(x=citystate, y=Freq, fill = Response, order=Response)) +
facet_wrap(~year, nrow=3) +
geom_bar(data = trial2$neg, stat = "identity") +
scale_fill_manual(
breaks=c("Not at all satisfied", "2", "3", "4", "Extremely satisfied"),
values=colorsB,
name="Response"
) +
geom_bar(data = trial2$pos, stat = "identity") +
coord_flip() +
ggtitle("Community satisfaction") +
xlab("") +
ylab("") +
scale_y_continuous(
limits=c(-0.5, 1),
breaks=seq(from=-0.5, to=0.75, by=0.25),
labels=c("50%", "25%", "0", "25%", "50%", "75%")
) +
theme(
legend.text=element_text(size=14),
legend.title=element_text(size=16),
axis.text=element_text(size=14),
strip.text=element_text(size=14))
baseplot
Don't ask me about the data structure, I can't remember why I chose to use a list... If you want to try to reproduce this exact figure, I've saved the data as an .Rdata file.
Challenge accepted.
As you see, most things can be translated very easily. A few notes:
gf_col()
and geom_col()
are preferred to geom_bar()
with stat = "identity"
order = Response
do anything? I don't see it documented in geom_bar()
and it emits a warning from gf_col()
. require(RColorBrewer)
require(ggformula)
load("~/Downloads/likertdata.Rdata")
colors1 <- brewer.pal(5, "Spectral")
colorsB <- colors1[c(2 ,3, 4, 5, 1)]
gf_col(Freq ~ citystate, fill = ~ Response, order = ~ Response, data = trial2$neg) %>%
gf_col(data = trial2$pos) %>%
gf_facet_wrap( ~ year, nrow = 3) %>%
gf_refine(
scale_fill_manual(
breaks = c("Not at all satisfied", "2", "3", "4", "Extremely satisfied"),
values = colorsB,
name = "Response"),
scale_y_continuous(
limits=c(-0.5, 1),
breaks=seq(from = -0.5, to = 0.75, by = 0.25),
labels=c("50%", "25%", "0", "25%", "50%", "75%")
),
coord_flip()
) %>%
gf_labs(
title = "Community satisfaction", x = "", y = "") %>%
gf_theme(
legend.text=element_text(size=14),
legend.title=element_text(size=16),
axis.text=element_text(size=14),
strip.text=element_text(size=14))
#> Warning: Ignoring unknown aesthetics: order
Looking at this more carefully, I'm guessing that this is what you really wanted.
require(RColorBrewer)
require(ggformula)
load("~/Downloads/likertdata.Rdata")
colors1 <- brewer.pal(5, "Spectral")
colorsB <- colors1[c(2,3,4,5,1)]
responses1 <- c("Not at all satisfied", "2", "3", "4", "Extremely satisfied")
responses2 <- responses1[c(1,5,2,4,3)]
gf_col(Freq ~ citystate, fill = ~ factor(Response, levels = responses2), data = trial2$neg) %>%
gf_col(data = trial2$pos, fill = ~ factor(Response, levels = responses2)) %>%
gf_facet_wrap( ~ year, nrow = 3) %>%
gf_refine(
scale_fill_manual(
breaks = responses1,
values = colorsB,
name = "Response"),
scale_y_continuous(
limits=c(-0.5, 1),
breaks=seq(from=-0.5, to=0.75, by=0.25),
labels=c("50%", "25%", "0", "25%", "50%", "75%")
),
coord_flip()
) %>%
gf_labs(
title = "Community satisfaction", x = "", y = "") %>%
gf_theme(
legend.text=element_text(size=10),
legend.title=element_text(size=12),
axis.text=element_text(size=6, angle = 30),
strip.text=element_text(size=11))
Okay, I'm convinced! You're right about the colors/ordering, I must not have actually grabbed all the relevant code.
As per the discussion in https://github.com/ProjectMOSAIC/mosaic/issues/686, it might be nice to have a document doing a side-by-side comparison of
ggformula
andggplot2
. Uses:ggplot2
is available viaggformula
ggformula
toggplot2
, if desiredggplot2
see how things can be done inggformula
.