【问题标题】:Pairing colors to factors in ggplot2将颜色与 ggplot2 中的因子配对
【发布时间】: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)
})  


})

【问题讨论】:

  • 几种方法,如herehere所示
  • 我没有用你的代码测试过,但我认为你可以在你的ggplot代码中添加+ scale_color_discrete(drop=FALSE)。无论从数据中删除哪些行,只要因子的基础水平不变,这将起作用。
  • eipi 非常感谢

标签: r ggplot2 shiny


【解决方案1】:

您可以将 + scale_color_discrete(drop=FALSE) 添加到您的 ggplot 代码中。无论从数据中删除哪些行,只要因子的基础水平不变,这将起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多