【发布时间】:2016-07-01 17:56:09
【问题描述】:
我有一个变量 Channel,它是 A、B、C 级别的因子。如果你运行我的闪亮等级 A 是红色点 B 级绿色点和 C 级蓝色点。 当我取消选中 A 级别以使其为 B 和 C 更改而消失时,这就是我想要阻止的。所以每个级别都应该有它的颜色。我试着用比例手册等玩了一下,但没有成功。
########### 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)
})
})
【问题讨论】:
-
我没有用你的代码测试过,但我认为你可以在你的ggplot代码中添加
+ scale_color_discrete(drop=FALSE)。无论从数据中删除哪些行,只要因子的基础水平不变,这将起作用。 -
eipi 非常感谢