How to set color of bar in googleVis?

10 views
Skip to first unread message

Song

unread,
Jan 2, 2018, 2:01:14 PM1/2/18
to Shiny - Web Framework for R
Hi guys.

The following plot is what I want to get. But for some reasons, I can not get it by ggplot(Run the following code). The order of bars is not sorted.


da <- read.csv("/Users/song/Downloads/da.csv", header = TRUE, sep = ";")
da
<- dput(da)
#> structure(list(data_source = structure(c(3L, 6L, 1L, 5L, 4L,
#> 7L, 2L, 8L, 9L), .Label = c("arXiv", "engineering.com", "EPO",
#> "Foerderkatalog", "Fraunhofer Publica", "newatlas.com", "opcconnect.opcfoundation.org",
#> "phys.org", "theengineer.co.uk"), class = "factor"), data_type = structure(c(3L,
#> 2L, 4L, 4L, 1L, 2L, 2L, 2L, 2L), .Label = c("FUNDING_PROJECT",
#> "NEWS", "PATENT", "SCIENT_PUBLICATION"), class = "factor"), count = c(1253L,
#> 656L, 297L, 26L, 24L, 3L, 2L, 1L, 1L)), .Names = c("data_source",
#> "data_type", "count"), class = "data.frame", row.names = c(NA,
#> -9L))
plotData
<- da

library(ggplot2)
   
# Make cVal column a factor with specified levels
plotData
[[
"data_type"]] <- factor(plotData[["data_type"]],
                                  levels
= c(
                                           
"FUNDING_PROJECT",
                                           
"SCIENT_PUBLICATION",
                                           
"PATENT",
                                           
"NEWS"))

# Initialise ggplot
p
<- ggplot(plotData)

# Plot bar chart
p
<- p + geom_bar(aes_q(x = as.name(
"data_source"),
                        y
= as.name(
"count"),
                        fill
= as.name(
"data_type")),
                  stat
=
"identity",
                  position
= position_dodge())

# Add values as labels on bars
plotData
<- cbind(plotData, yLabel = paste0(
"  ", plotData[,"count"], "  "))
p
<- p + geom_text(data = plotData,
                   aes_q
(x = as.name(
"data_source"),
                         y
= as.name(
"count"),
                         label
= as.name(
"yLabel"),
                         hjust
=
"inward"),
                   size
= (
5/14)*12 # conversion from mm to px
)



# Assign colours to data types
p
<- p + scale_fill_manual(name =
"",
                           values
= c(
                               
"FUNDING_PROJECT"    = "red",
                               
"SCIENT_PUBLICATION" = "blue",  
                               
"PATENT"             = "green",
                               
"NEWS"               = "black"),
                           labels
= c(
                               
"FUNDING_PROJECT"    = "Funded projects",
                               
"SCIENT_PUBLICATION" = "Scientific publications",
                               
"PATENT"             = "Patents",  
                               
"NEWS"               = "News"))


# Flip coordinates
p
<- p + coord_flip()


plot
(p)


I also try to use googleVis to get the plot. But I do not know how to set the color. And I think the legend will be hard to set too.

da <- read.csv("/Users/song/Downloads/da.csv", header = TRUE, sep = ";")
da
<- dput(da)
#> structure(list(data_source = structure(c(3L, 6L, 1L, 5L, 4L,
#> 7L, 2L, 8L, 9L), .Label = c("arXiv", "engineering.com", "EPO",
#> "Foerderkatalog", "Fraunhofer Publica", "newatlas.com", "opcconnect.opcfoundation.org",
#> "phys.org", "theengineer.co.uk"), class = "factor"), data_type = structure(c(3L,
#> 2L, 4L, 4L, 1L, 2L, 2L, 2L, 2L), .Label = c("FUNDING_PROJECT",
#> "NEWS", "PATENT", "SCIENT_PUBLICATION"), class = "factor"), count = c(1253L,
#> 656L, 297L, 26L, 24L, 3L, 2L, 1L, 1L)), .Names = c("data_source",
#> "data_type", "count"), class = "data.frame", row.names = c(NA,
#> -9L))
library(googleVis)
#> Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
#>
#> Welcome to googleVis version 0.6.2
#>
#> Please read Google's Terms of Use
#> before you start using the package:
#> https://developers.google.com/terms/
#>
#> Note, the plot method of googleVis will by default use
#> the standard browser to display its output.
#>
#> See the googleVis package vignettes for more details,
#> or visit http://github.com/mages/googleVis.
#>
#> To suppress this message use:
#> suppressPackageStartupMessages(library(googleVis))
p
<- gvisBarChart(da, xvar =
"data_source", yvar = "count" )
plot
(p)
#> starting httpd help server ...
#>  done


Best
Song
Reply all
Reply to author
Forward
0 new messages