如何改变闪亮的条形颜色

时间:2015-02-22 10:23:38

标签: r shiny googlevis

我有一个包含当前项目的数据框:

感情,色彩,推文
 pos,green,58
 neg,red,3
 中性,蓝色,46

R 闪亮中我使用Cran包GoogleVis。但是当我绘制柱形图时  所有的酒吧都有相同的颜色。

服务器脚本

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


shinyServer(function(input, output) {


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

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

   )
}) 
})

UI脚本

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

我可以在GGPLOT2中实现它(参见下面的代码)。但 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"))

如何使用数据框中可用的颜色属性更改条形颜色。

非常感谢!

Erik

2 个答案:

答案 0 :(得分:2)

您可以在使用colour绘图之前重命名tweets.stylegvisColumnChart,并将该列作为yvar

传递

以下是一个示例,我使用了您在顶部提供的虚拟数据和ui.R(刚刚删除了选择输入):

<强> 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"))
 }) 
})

我得到这样的东西: enter image description here

您可以获得有关角色和自定义gvis图here

的更多信息

答案 1 :(得分:0)

如果您使用R,您可以简单地执行以下操作:例如:

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