R:改变面板顺序

15

我想改变BA、SLG的外观顺序,变成SLG、BA。我发现有类似的问题,但是我的解决方案可能行不通,因为我已经在Excel中汇总了数据。因此,我的数据框架可能不同。无论如何,我尝试实施这个方法都没有成功:

df2 <- factor(df, levels = c("SLG","BA"))
任何帮助解决这个问题都将不胜感激。
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

图片


1
dfx <- gather(df, group, data, SLG, BA) 之后,执行 dfx$group = factor(dfx$group, levels=c("SLG","BA")) - eipi10
好的,在我看到的例子中,group在它们的情况下将是变量名(SLG),但我想在我已经有摘要统计数据的情况下,我只需使用group。 - Remy M
1
使用您想要设置因子水平顺序的变量名称。在这种情况下,它恰好是 group - eipi10
3个回答

20
dfx$group <- factor(dfx$group, levels = c("SLG","BA"))

11

由于您只有两个因子水平,您可以在ggplot代码中使用forcats :: fct_rev()(如果已经加载了tidyverse,则无需加载forcats)。

library(forcats)
# replace last line of code with
facet_grid( ~ fct_rev(group), scale = "free")

2

这个页面上有很多优秀的解决方案。

这个建议在之前的基础上加了一点微调。根据(1)你是否想更改底层数据(通常情况下是可以的,而且在输入/建模等方面也更容易),以及(2)个人喜好,你也可以在ggplot facet_grid()内轻松处理它。

# Factorize variable prior to plotting (from baptiste)
dx$group <- factor(dfx$group, levels = c("SLG", "BA"))

# Within the ggplot
# Update this code
facet_grid(~group, scale = "free")
# With this code
facet_grid(~factor(group, levels = c("SLG", "BA"), scale = "free")

当我不想改变底层数据时,我通常会采用这种方法处理。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接