2016-12-20 232 views
0

鑑於下面的數據和圖表,如何從圖例中刪除顯示爲「NA」的類別?我只想讓傳說展示類別ABC的顏色。從圖例中刪除特定標籤

library(tibble) 
library(ggplot2) 

mydata <- tibble(Time_dim = rep(1:10, 10), 
       Category = c(rep(NA, 10), rep(c(rep("A", 10), rep("B", 10), rep("C", 10)), 3)), 
       Attribute = c(rep("alpha", 10), rep("beta", 30), rep("omega", 30), rep("theta", 30)), 
       Data = runif(100)) 
mydata$Category <- factor(mydata$Category) 
mydata$Attribute <- factor(mydata$Attribute) 

ggplot(mydata, aes(x = Time_dim, y = Data, colour = Category)) + 
geom_line() + facet_wrap(~ Attribute, ncol = 1, scales = "free") 

enter image description here

+0

在其他線程提供沒有與我的工作數據的答案,但@aosmith提供的答案完美的作品。 – Phil

回答

2

您可以在color規模設置breaks爲僅要顯示的類別。

在你的情況下,你可以使用你的色彩因子levels

scale_color_discrete(breaks = levels(mydata$Category))

0

它添加到ggplot

+ scale_colour_manual(breaks=c("A", "B", "C"), values=c("red", "green", "blue"))