Erik hoeven
Erik hoeven

Reputation: 1610

How to change the colour of the bar in shiny

I have a dataframe with the current items :

sentiment, colour, tweets
pos , green ,58
neg , red ,3
neutral , blue ,46

In R shiny i use the Cran Package GoogleVis. But when i plot the Column Chart all the bars get the same colour.

Server Script

# server.R
library(googleVis)
library(shiny)


shinyServer(function(input, output) {


datasetInput <- reactive({
   getFilteredBarFrame(input$dataset)
   }) 

output$view <- renderGvis({
   gvisColumnChart(datasetInput()

   )
}) 
})

UI Script

library(shiny)
source("data.R")

lstCategorie <-     
   getlstCategorie(getGroupedData(getTweetTraining(openDBSentiment())))


shinyUI(pageWithSidebar(
    headerPanel("Tweet Sentiment"),
    sidebarPanel(
    selectInput("dataset", "Selecteer bank:"
                         ,  choices = lstCategorie
                         , selected = "Rabobank"
    )
 ),
mainPanel(
     htmlOutput("view")
   )
) 

)

data.r

library(RMongo)
library(data.table)
library(ggplot2)
library(googleVis)
library(plyr)

#Make connection with MongoDB
openDBSentiment <- function (){
dbSentiment <- mongoDbConnect('Sentiment')  
return (dbSentiment)  
}

#Get Trainingset of Tweets
getTweetTraining <- function (dbSentiment){
   getTrainingData <- dbGetQuery(dbSentiment,'TweetTraining','{}')
   TrainingData <- data.frame(getTrainingData) 
return (TrainingData)  
}

#Aggegrate dataset
getGroupedData <-function(TrainingData){
  grouped_data <- aggregate(TrainingData, by=list(TrainingData$colour, 
  TrainingData$tag, TrainingData$categorie), FUN=length)

  #Remove empty rows
  grouped_data <- subset(grouped_data,select=c(Group.1,Group.2,Group.3,   
  colour), subset =(Group.2 != "" ) )

  #Rename columns
  grouped_data <-  


    rename(grouped_data,c("Group.1"="kleur","Group.2"="sentiment","Group.3"="categorie","colour"="aantaltweets"))

  return (grouped_data)
}

#Get list of categories
getlstCategorie <- function (grouped_data){
  lstCategorie <- unique(grouped_data$categorie)
  return (lstCategorie)
}

#Get dataframe for barplot
getFilteredBarFrame <- function (input) {
 #Build dataframe from other functions
  dfBarFrame  <-  getGroupedData(getTweetTraining(openDBSentiment()))
  #Filter the data.frame based on the input parameter
  dfBarFrame <-  dfBarFrame[dfBarFrame$categorie ==  input , ]
  #select only 2 fields of the data.frame
  dfBarFrame <- dfBarFrame[c("sentiment","aantaltweets","kleur")]
  #return the result tot the function
return (dfBarFrame)
}

GoogleVis Plot Result

enter image description here

I can realize it in GGPLOT2 (see code below). But not with GoogleVis!

    ggplot(dfAGG, aes(x=categorie, y=aantaltweets, fill=sentiment)) +
  geom_bar(position="dodge" , colour = "black" ,stat="identity") +
  scale_fill_manual(values=c("red","orange","green"))

How can i change the bar colour with the colour property which is available in the dataframe.

Many thanks!

Erik

Upvotes: 2

Views: 2756

Answers (2)

NicE
NicE

Reputation: 21425

You can rename the colour column tweets.style before plotting with gvisColumnChart, and pass that column as an yvar

Here is an example, I used the dummy data you gave at the top and your ui.R (just removed the select input):

server.R

library(googleVis)
library(shiny)

shinyServer(function(input, output) {
        data=data.frame(sentiment=c("pos","neg","neutral"),colour=c("green","red","blue"),tweets=c(58,3,46))
        names(data)<-c("sentiment","tweets.style","tweets")
        output$view <- renderGvis({
                gvisColumnChart(data,xvar="sentiment",yvar=c("tweets","tweets.style"))
 }) 
})

I get something like this: enter image description here

You can get more info about roles and customizing gvis plots here

Upvotes: 2

user1267127
user1267127

Reputation:

if you are using R, you can simply do as follows e.g.:

counts <- table(mtcars$gear)
color <-  c("blue", "orange", "gold", "indianred", "skyblue4","light blue")
barplot(counts, main="Car Distribution", xlab="Number of Gears",col=color)

Upvotes: 0

Related Questions