R构面的更改顺序 [英] R Changing Order of Facets

查看:61
本文介绍了R构面的更改顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将构面的顺序从BA,SLG更改为SLG,BA.我发现了与此类似的问题,但是我认为我的解决方案可能无法正常工作,因为我已经在excel中汇总了数据.因此,我的数据框可能会有所不同.无论如何,我尝试实施此操作无济于事:

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

推荐答案

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

这篇关于R构面的更改顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆