2016-08-30 142 views
5

我只想改變最後一組的條形圖的順序,以突出顯示它。我用scale_fill_manual(),但它沒有多大幫助。更改ggplot中其中一個擋光條的填充顏色

這裏是我的代碼:

x<-c(rep(c("Type1", "Type2"),4)) 
    y<-c(4,5,6,7,3,4,5,2) 
    time<-c(2010,2010,2011,2011,2012,2012,2013,2013) 
    z<-data.frame(type = x, val=y, Time = time) 

    ggplot(data = z, aes(x=Time,y=val)) + 
     geom_bar(stat = "identity", position = "dodge", aes(fill=type))+ 
     scale_fill_manual(values = c(rep(c("white", "gray51"),3),"white","red")) 

下面是輸出:

enter image description here

我希望圖形看起來像: enter image description here

有什麼辦法,我可以做到這一點?我將不勝感激任何幫助。我看着change color of only one bar in ggplot,但它似乎不是關於分組數據。

+0

你是否特別關注沒有任何紅色的傳說?這將是一個棘手的部分... – Gregor

+0

@gregor我認爲這將是更好的,如果傳說沒有任何紅色。不過,我是R和'ggplot2'的初學者。所以,如果你能指導我如何在沒有傳說的情況下做到這一點,我將不勝感激。非常感謝你的幫助。對此,我真的非常感激。 – watchtower

+0

在我的答案結尾處查看編輯以保持圖例中的紅色。 – Gregor

回答

7

我的一般口頭禪是ggplot非常擅長繪製你給它的數據。如果你想要繪製不同的東西,最簡單的方法通常是修改你的數據。

z$type2 = as.character(z$type) 
z$type2[z$type == "Type2" & z$Time == 2013] = "Type2 " 

"Type2 "加入偷偷摸摸的額外空間,要突出顯示該行。這將是一個獨特的因素水平,並獲得自己的顏色(甚至被迫使用按字母順序排列的默認順序)。但它會在圖例標籤中顯示相同。

ggplot(data = z, aes(x=Time,y=val)) + 
    geom_bar(stat = "identity", position = "dodge", aes(fill=type2))+ 
    scale_fill_manual(values = c("white", "gray50", "red")) 

enter image description here

我認爲,從傳說中省略紅色將是困難的,但this answer向我展示了所有需要被添加breaks = c("Type1", "Type2")作爲參數scale_fill_manual

6

突出顯示帶邊框的酒吧怎麼樣?例如:

z$hi = with(z, ifelse(type=="Type2" & Time==2013, "Y","N")) 

ggplot(data = z, aes(x=Time,y=val)) + 
    geom_bar(stat = "identity", position = "dodge", 
      aes(fill=type, colour=hi), size=1) + 
    scale_fill_manual(values=c("gray51","white")) + 
    scale_colour_manual(values=c(NA,"red")) + 
    guides(colour=FALSE) 

enter image description here

UPDATE:在回答您的評論:我覺得一個線圖可以更容易地看到趨勢和各type之間的關係。例如:

ggplot(data = z, aes(x=Time,y=val,colour=type)) + 
    geom_line() + 
    geom_point() + 
    geom_point(data=z[z$hi=="Y",], aes(x=Time, y=val), size=4, pch=1, 
      colour=hcl(195,100,40), stroke=1) + 
    scale_y_continuous(limits=c(0,max(z$val))) + 
    theme_bw() 

enter image description here

+0

非常好的主意! – Gregor

+0

謝謝..格雷戈! – eipi10

+0

通話內的顏色比我下面的兩步解決方案要好得多。 –

1

易與傳說這樣做,雖然你可能需要謹慎對待扔用戶關閉與顏色的突然變化。只需在您的x變量中添加一個額外的類別,以指示您想要突出顯示的位置。

x<- xHigh <- c(rep(c("Type1", "Type2"),4)) 
xHigh[length(xHigh)] <- "Type2_highlight" 

myHighlight <- rep("No",length(x)) 
myHighlight[length(myHighlight)] <- "Yes" 
y<-c(4,5,6,7,3,4,5,2) 
time<-c(2010,2010,2011,2011,2012,2012,2013,2013) 
z<-data.frame(type = x, xHigh = xHigh, val=y, Time = time, myHighlight = myHighlight) 

ggplot(data = z, aes(x=Time,y=val)) + 
    geom_bar(stat = "identity", position = "dodge", aes(fill=xHigh))+ 
    scale_fill_manual(values = c(Type1 = "white", Type2 = "gray51", Type2_highlight = "red")) 

enter image description here

突出顯示特定的條另一個潛在選擇是畫一個框周圍,像這樣:

ggplot(data = z, aes(x=Time,y=val)) + 
    geom_bar(stat = "identity", position = "dodge", aes(fill=type))+ 
    scale_fill_manual(values = c(Type1 = "white", Type2 = "gray51")) + 
    geom_bar(aes(linetype = xHigh) 
      , fill = NA 
      , stat = "identity", position = "dodge" 
      , col = "red" 
      , show.legend = FALSE) + 
    scale_linetype_manual(values = c(Type1 = 0 
            , Type2 = 0 
            , Type2_highlight = 1)) 

enter image description here

希望有所幫助。