0

I am making an app using shiny package for R data analysis.

I am having trouble in giving the values for color attribute for barplot function. I am taking two columns from the user and I am applying the function nvalues that does the trick for default colors.. but what If I want to have my own array of custom colors and use only some of them as per column's requirement.

here is my shiny file

shinyServer(function(input, output) {
  output$myplot<-renderPlot({          
    c1<-input$cname1
    c2<-input$cname2
    clm1<-smpl[,c1]
    clm2<-smpl[,c2]
    counts<-table(clm1,clm2)
    View(counts)
    n<-nlevels(clm1)
    barplot(counts,beside=TRUE,col=c(1:n),legend = rownames(counts))                    
  })
})
2
  • It's not really clear what you're asking here. Are you asking for a function to choose among a list of colors?
    – Thomas
    Apr 2, 2014 at 11:14
  • I am asking that if counts has n levels then n colors should be selected from a vector of color that I define and not all the colors in that vector.
    – apTNow
    Apr 2, 2014 at 13:18

1 Answer 1

0

As with all base plotting functions, assign a vector of colors as you desire.

barplot(some_data,col=c('red','orange','yellow','green','blue'))   

or

barplot(some_data,col=rainbow(10))

and so on. The col vector recycles as necessary.

2
  • what if the levels in the column are fewer than the vector. It uses it for the next subgroup in a barplot. thats not good
    – apTNow
    Apr 2, 2014 at 13:20
  • @apps92 then specify the index, e.g. col=rainbow(10)[1:n] Apr 2, 2014 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.