ProjectMOSAIC / ggformula

Provides a formula interface to 'ggplot2' graphics.
Other
39 stars 9 forks source link

Create ggformula/ggplot2 Rosetta Stone #67

Open rpruim opened 6 years ago

rpruim commented 6 years ago

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 and ggplot2. Uses:

rpruim commented 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.

AmeliaMN commented 6 years ago

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.

rpruim commented 6 years ago

Challenge accepted.

As you see, most things can be translated very easily. A few notes:

    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

rpruim commented 6 years ago

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))

AmeliaMN commented 6 years ago

Okay, I'm convinced! You're right about the colors/ordering, I must not have actually grabbed all the relevant code. screen shot 2018-02-20 at 1 24 42 pm