2017-04-08 1 views
1

패싯 순서를 BA, SLG에서 SLG, BA로 변경하려고합니다. 나는 이것과 비슷한 질문을 찾았지만 데이터를 요약 한 덕분에 솔루션이 작동하지 않을 수도 있다고 생각합니다. 따라서 내 데이터 프레임이 다를 수 있습니다. 어쨌든, 나는 아무 소용이 구현하는 시도 :이 문제를 해결R 패싯 순서 변경

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

+0

'DFX <- (DF, 그룹, 데이터, SLG, BA)'할'DFX의 $ 그룹 = 계수 (DFX의 $ 그룹, 레벨 = C를 ("SLG를 수집 ","BA "))'. – eipi10

+0

알았어, 내가 본 예제에서 그룹은 변수 이름 (SLG)이 될 것이지만, 요약 통계가 이미있는 경우 그룹을 사용한다고 가정합니다. –

+1

요인 수준의 순서를 설정하려는 변수의 이름을 사용하십시오. 이 경우에는'group'이됩니다. – eipi10

답변

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