Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Changing Order of Facets

Tags:

r

ggplot2

facet

I am trying to change the order of the facets from BA, SLG to SLG, BA. I have found questions similar to this but I think my solution might not be working because I have summarized the data in excel; Therefore, my data frame might be different. Anyways, I tried implementing this to no avail:

df2 <- factor(df, levels = c("SLG","BA"))

Any help fixing this issue would be much appreciated.

df <- read.table(textConnection(
  'POS SLG BA
  2B    0.4632 .23
  3B    0.468652174 .24
  SS    0.4146 .22
  1B    0.472368421 .25
  RF    0.462684211 .245
  CF    0.4435 .225
  LF    0.4474 .226
  C 0.440875  .228
  DH    0.508714286 .28'), header = TRUE,stringsAsFactors = FALSE)


library(micromapST)
library(ggplot2)
library(tidyr)
library(dplyr)
df$POS <- reorder(as.factor(df$POS), df$SLG)
dfx <- gather(df, group, data, SLG, BA)
row.names(df) <- NULL

theme_set(theme_grey() +
            theme(plot.title = element_text(hjust=0.5,face='bold'),
                  axis.title.y = element_text(angle = 0, vjust = 0.5,face='bold'),
                  axis.title.x=element_text(face='bold'),
                  panel.background = element_rect(fill = "gray"),
                  axis.ticks=element_blank()))


plot <- ggplot(dfx, aes(x = data, y = POS, group = group, fill = POS))+
  labs(title = "Position vs Slugging Percentage", x = "SLG", y = "Position") + 
  geom_point(shape = 21, size = 3) + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        plot.caption = element_text(hjust = -0.5),
        legend.position = "",
        strip.text.y = element_blank(),
        strip.background = element_rect(fill = rgb(.9,.95,1),
                                        colour = gray(.5), size=.2),
        panel.border = element_rect(fill = FALSE, colour=gray(.75)),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.spacing.x = unit(0.07,"cm"),
        panel.spacing.y = unit(0.07,"cm"),
        axis.ticks = element_blank(),
        axis.text = element_text(colour = "black"),
        axis.text.y = element_text(size = rel(.78), face = "bold",
                                   margin = margin(0,0,0,3)),
        axis.text.x = element_text(margin = margin(-1,0,3,0))) +
  facet_grid(~group, scale = "free")

plot

Image

like image 860
Remy M Avatar asked Apr 08 '17 05:04

Remy M


People also ask

How do you change the order of facets?

You can use the following basic syntax to specify the order of facets in ggplot2: p + facet_grid(~factor(my_variable, levels=c('val1', 'val2', 'val3', ...))) The following example shows how to use this syntax in practice.

How do I change a facet label in R?

Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

What is the difference between Facet_grid and Facet_wrap?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

What does the facet wrap function do in R?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.


2 Answers

dfx$group <- factor(dfx$group, levels = c("SLG","BA"))
like image 136
baptiste Avatar answered Oct 21 '22 09:10

baptiste


As you only have two factor levels you can use forcats::fct_rev() within your ggplot code (no need to load forcats if you have tidyverse already loaded).

library(forcats)
# replace last line of code with
facet_grid( ~ fct_rev(group), scale = "free")
like image 32
Joe Avatar answered Oct 21 '22 11:10

Joe