2017-05-18 23 views
1

我想從第一個圖形中刪除一列而不更改兩個圖形上的條形圖的寬度或對齊方式。任何幫助是極大的讚賞。在不改變格式的情況下從條形圖中選擇性地刪除條形圖

theme_min <- 
    theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(), 
    panel.background = element_blank(), 
    panel.border = element_blank(), 
    axis.line = element_line(colour = "black") 
) 

# display 2 of 4 columns 
mtcars %>% 
    mutate(
    am = factor(am, labels = c("auto", "manual")), 
    vs = factor(vs, labels = c("V", "S")) 
) %>% 
    filter(
    am == "auto" 
) %>% 
    ggplot(aes(x = am, y = mpg, fill = vs)) + 
    geom_col(position = position_dodge()) + 
    scale_y_continuous(limits = c(0,35)) + 
    theme_min + 
    scale_x_discrete(drop = FALSE) 

2 of 4 bars

# display 1 of 4 columns 
mtcars %>% 
    mutate(
    am = factor(am, labels = c("auto", "manual")), 
    vs = factor(vs, labels = c("V", "S")) 
) %>% 
    filter(
    am == "auto", 
    vs == "V" 
) %>% 
    ggplot(aes(x = am, y = mpg, fill = vs)) + 
    geom_col(position = position_dodge()) + 
    scale_y_continuous(limits = c(0,35)) + 
    theme_min + 
    scale_x_discrete(drop = FALSE) + 
    scale_fill_discrete(drop = FALSE) 

1 of 4 bars displayed, but width is out of sync with image 1

+2

建議重複[總是躲閃的直方圖(http://stackoverflow.com/q/10149571/903061),[在GGPLOT2相同的條寬(http://stackoverflow.com/q/24304642/903061),[不要刪除零計數躲閃條形圖](http://stackoverflow.com/q/10326729/903061)... – Gregor

回答

1

我能夠所以它等於列(S)你想降爲零選擇性地通過突變的結果變量刪除列。

mtcars %>% 
    mutate(
    am = factor(am, labels = c("auto", "manual")), 
    vs = factor(vs, labels = c("V", "S")) 
) %>% 
    filter(am == "auto") %>% 
    # mutate to drop bar while maintaining bar formatting 
    mutate(mpg = ifelse(vs == "S", mpg == 0, mpg)) %>% 
    ggplot(aes(x = am, y = mpg, fill = vs)) + 
    geom_col(position = position_dodge()) + 
    scale_y_continuous(limits = c(0,35)) + 
    scale_x_discrete(drop = FALSE) 

one bar plot

相關問題