2016-07-01 24 views
2

我有一個變量Channel,它是A,B,C級別的因子。如果你運行我的亮起水平A是紅點B級綠點和C級藍點。 當我取消選中A級別時,它會使B和C發生變化,這就是我想要防止的情況。所以每個級別都應該有它的顏色。我試圖玩一些縮放手動等,但沒有成功。將顏色與ggplot2中的因子配對

########### UI.R 
library(shiny) 

shinyUI(fluidPage(
    titlePanel("Hello Shiny!"), 
    sidebarLayout(
     sidebarPanel(
      checkboxGroupInput(inputId="check1_1", label="Select channel", choices=c("a","b","c"), 
           selected=c("a","b","c")) 
     ), 

     mainPanel(
      plotOutput("myplot") 
     ) 
    ) 
)) 

########## Server.R 
shinyServer(function(input, output) { 

df<-data.frame(Channel=sample(letters[1:3],100,replace = TRUE), 
         x=runif(100), 
         y=runif(100)) 

df1<-reactive({ 

    y<-as.factor(input$check1_1) 
    df1<-df[which(df$Channel %in% y), ] 
}) 
output$myplot<-renderPlot({ 
    ggplot(df1(),aes(x = x,y = y,colour=Channel))+geom_point(size=6) 
}) 


}) 
+1

一些方法來做到這一點,顯示[這裏](http://stackoverflow.com/questions/21322819/generating-a-consistent-dynamic-color-palette-in-ggplot-within-a-loop)和[here](http://stackoverflow.com/questions/19068432/ggplot2-how-to-use-same-colors-in-different-plots-for-same-factor#) – aosmith

+1

我還沒有測試過它與您的代碼,但我認爲你可以在你的ggplot代碼中添加'+ scale_color_discrete(drop = FALSE)'。無論從數據中刪除哪些行,只要該因素的基本級別沒有變化,這都可以工作。 – eipi10

+0

eipi謝謝大大的工作 –

回答

2

您可以將+ scale_color_discrete(drop=FALSE)添加到您的ggplot代碼中。無論從數據中刪除哪些行,只要該因素的基本級別沒有變化,這都可以工作。

相關問題