├── DESCRIPTION ├── NAMESPACE ├── R └── partycolor.R ├── README.md ├── data └── partycolorsdataset.rda ├── man └── partycolor.Rd └── partycoloR.Rproj /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: partycoloR 2 | Title: Easily access the party colors of thousands of parties around the world 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person("Michael", "Imre", , "michael.imre@uni-mannheim.de", role = c("aut", "cre"), 6 | comment = c(ORCID = "0000-0002-1831-2083")) 7 | Description: This package provides easy access to the official party colors of thousands of parties around the world in hex triplet, RGB or CMYK format. IDs from the Party Facts project (https://partyfacts.herokuapp.com) are used to identify the parties, data was collected on Wikidata, Wikipedia, and supplemented by own research. 8 | License: use_mit_license() 9 | Encoding: UTF-8 10 | Roxygen: list(markdown = TRUE) 11 | RoxygenNote: 7.1.2 12 | Depends: 13 | R (>= 2.10) 14 | LazyData: true 15 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(partycolor) 4 | -------------------------------------------------------------------------------- /R/partycolor.R: -------------------------------------------------------------------------------- 1 | #' Get colors of parties 2 | #' 3 | #' Returns the colors of parties 4 | #' @param ids A single id or vector of ids from the Party Facts data set. 5 | #' @param type The color model the colors should be in, can be hex, rgb, cmyk, or all (if all three types should be returned). 6 | #' Default is hex. 7 | #' @param websafe logical: should web safe colors be returned instead? Old browsers were not able to display 8 | #' other colors than these, with current browsers this option should not be necessary, default is FALSE. 9 | #' @param include_ids logical: should party facts ids be included in the output? If the colors get added to an existing data 10 | #' frame that already has the ids this is usually not helpful, default is FALSE. 11 | #' @param include_description logical: should the name of the color (e.g. "Dark blue") be included in the output? 12 | #' Default is FALSE. 13 | #' @param include_source logical: should the data source of the color be included in the output? Can be wikidata or 14 | #' wikipedia, default is FALSE. 15 | #' @param include_multiple logical: in some cases parties have multiple party colors, in these cases only one of them 16 | #' is included in this data set. Should information on whether the party has multiple colors according to the source, 17 | #' default is FALSE. 18 | #' @keywords parties colors party colors 19 | #' @export 20 | #' @examples 21 | #' austrianparties <- data.frame(names=c("OEVP","SPOE","FPOE","Gruene","Neos"), partyfacts_id=c(1329,1384,463,1659,1970)) 22 | #' ## this returns a simple vector of all hex triplets 23 | #' austrianparties$color <- partycolor(austrianparties$partyfacts_id) 24 | #' ## this returns a data frame consisting of colors in all three color models, party facts ids, 25 | #' color descriptions and color source. 26 | #' austriancolors <- partycolor(austrianparties$partyfacts_id, 27 | #' type='all', include_ids = TRUE, 28 | #' include_description = TRUE, include_source = TRUE) 29 | #' 30 | partycolor <- function(ids, 31 | type = c("hex","rgb","cmyk","all"), 32 | websafe=FALSE, 33 | include_ids=FALSE, 34 | include_description=FALSE, 35 | include_source=FALSE, 36 | include_multiple=FALSE) { 37 | if(missing(type)) { 38 | type <- "hex" 39 | } 40 | # check if correct argument was used for type- option 41 | type <- match.arg(type,several.ok=T) 42 | 43 | if(type=="all") { 44 | type <- c("hex","rgb","cmyk") 45 | } 46 | if(include_ids==TRUE) { 47 | type <- c("partyfacts_id",type) 48 | } 49 | if(include_description==TRUE) { 50 | type <- c(type,"description") 51 | } 52 | if(websafe==TRUE) { 53 | type <- paste0("ws",type) 54 | } 55 | if(include_source==TRUE) { 56 | type <- c(type,"source") 57 | } 58 | if(include_multiple==TRUE) { 59 | type <- c(type,"multiple") 60 | } 61 | partyfacts_id <- data.frame(as.vector(ids)) 62 | names(partyfacts_id)[1] <- "partyfacts_id" 63 | partyfacts_id$mergeorder <- as.integer(rownames(partyfacts_id)) 64 | merged <- merge(partyfacts_id,partycolorsdataset,all.x=T) 65 | merged <- merged[order(merged$mergeorder),] 66 | merged$mergeorder <- NULL 67 | requestedvars <- merged[,type] 68 | 69 | return(requestedvars) 70 | } 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | partycoloR 2 | ================ 3 | 4 | 5 | 6 | 7 | About the package 8 | ----------------- 9 | **This is a quick draft and very much work in progress, if you find mistakes or the code doesn't work I'd be very grateful if you contacted me!** 10 | 11 | This package provides easy access to the official party colors of thousands of parties around the world in hex triplet, RGB or CMYK format. 12 | IDs from the [Party Facts](https://partyfacts.herokuapp.com) project are used to identify the parties. Color data were collected on Wikidata and Wikipedia. If available, Wikidata (providing the precise hex triplets of the colors (e.g. #FF0000)) was used, if no data regarding color were available there, Wikipedia (providing color names (e.g. red)) was used as a data source. [Colorhexa](https://www.colorhexa.com) was used to add hex triplets (for colors from wikipedia), color names (for colors from wikidata) as well as RGB and CMYK codes as well as data on websafe versions of all colors. 13 | 14 | 15 | How to install 16 | -------------- 17 | 18 | ``` eval 19 | library(devtools) 20 | install_github("imrem/partycoloR") 21 | library(partycoloR) 22 | ``` 23 | 24 | 25 | How to use 26 | -------------- 27 | 28 | This package is only useful if you are working with IDs from the party facts project, which can be used to merge many different data sets 29 | 30 | ### Looking up color data of a single party 31 | ``` R 32 | partycolor(432) 33 | ``` 34 | In this case, the output is a single number, the hex code for the party color of the US Democratic Party. 35 | 36 | ``` R 37 | partycolor(809, type="all",include_ids=T,include_description = T) 38 | ``` 39 | This results in a more detailed output: color codes in three different formats, as well as the partyfacts ID color name of the party color of the US Republican Party. 40 | 41 | 42 | ### Looking up color data of multiple parties 43 | Probably the most useful usecase of this package is quickly merging the colors of multiple parties to a data frame (e.g. for displaying parties using their official colors in graphs). 44 | 45 | ``` R 46 | gerparties <- data.frame(name=c("AfD","CDU","CSU","FDP","Gruene","Linke","SPD"), 47 | partyfacts_id=c(1976,1375,1731,573,1816,1545,383), 48 | lrecon=c(7,5.90,6.38,7.90,3.81,1.29,3.71), 49 | galtan=c(9.52,5.86,7.29,3.43,1.10,2.81,3.38)) 50 | gerparties$hex <- partycolor(gerparties$partyfacts_id) 51 | ``` 52 | The code chunk above just adds the hex codes to all parties in the data frame. To add multiple color variables to a data set it's easiest to create a seperate object and merge it to the existing data frame using the Party Facts ID 53 | ``` R 54 | colors <- partycolor(gerparties$partyfacts_id, type='all', include_ids = TRUE, include_description = TRUE, include_source = TRUE) 55 | gerparties <- merge(gerparties,colors, all.x=T) 56 | ``` 57 | 58 | ### Create a graph using the party colors 59 | Now that the colors are added, one could easily create a graph using the colors that were just added to the data, for example: 60 | ``` R 61 | library(ggplot2) 62 | ggplot(gerparties,aes(x=lrecon,y=galtan, label=name)) + 63 | theme_classic() + labs(title="", x="LRECON", y="GALTAN") + 64 | scale_x_continuous(breaks=seq(0,10,1)) + 65 | scale_y_continuous(breaks=seq(0,10,1)) + 66 | geom_point(size = 6, color=gerparties$hex) 67 | ``` 68 | 69 | ### Access all party color data 70 | If you just want the data set, you can do so by assigning the object 'partycolorsdataset' to any object: 71 | ``` R 72 | partycolors <- partycolorsdataset 73 | ``` 74 | 75 | Accessibility 76 | -------------- 77 | As it heavily depends on the use case which colors will be displayed in a graph together I, unfortunately, have no way of making this colorblind-friendly. To make more accessible graphics, you might want to consider checking out a package like [RColorBrewer](https://cran.r-project.org/web/packages/RColorBrewer/index.html) or [Viridis](https://cran.r-project.org/web/packages/viridis/index.html), which provide options to make graphs better visible to color blind people, instead of using the real party colors. 78 | 79 | -------------------------------------------------------------------------------- /data/partycolorsdataset.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imrem/partycoloR/107b9ac5dc35979d4fad613d6eea4ad9bd069135/data/partycolorsdataset.rda -------------------------------------------------------------------------------- /man/partycolor.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/partycolor.R 3 | \name{partycolor} 4 | \alias{partycolor} 5 | \title{Get colors of parties} 6 | \usage{ 7 | partycolor( 8 | ids, 9 | type = c("hex", "rgb", "cmyk", "all"), 10 | websafe = FALSE, 11 | include_ids = FALSE, 12 | include_description = FALSE, 13 | include_source = FALSE, 14 | include_multiple = FALSE 15 | ) 16 | } 17 | \arguments{ 18 | \item{ids}{A single id or vector of ids from the Party Facts data set.} 19 | 20 | \item{type}{The color model the colors should be in, can be hex, rgb, cmyk, or all (if all three types should be returned). 21 | Default is hex.} 22 | 23 | \item{websafe}{logical: should web safe colors be returned instead? Old browsers were not able to display 24 | other colors than these, with current browsers this option should not be necessary, default is FALSE.} 25 | 26 | \item{include_ids}{logical: should party facts ids be included in the output? If the colors get added to an existing data 27 | frame that already has the ids this is usually not helpful, default is FALSE.} 28 | 29 | \item{include_description}{logical: should the name of the color (e.g. "Dark blue") be included in the output? 30 | Default is FALSE.} 31 | 32 | \item{include_source}{logical: should the data source of the color be included in the output? Can be wikidata or 33 | wikipedia, default is FALSE.} 34 | 35 | \item{include_multiple}{logical: in some cases parties have multiple party colors, in these cases only one of them 36 | is included in this data set. Should information on whether the party has multiple colors according to the source, 37 | default is FALSE.} 38 | } 39 | \description{ 40 | Returns the colors of parties 41 | } 42 | \examples{ 43 | austrianparties <- data.frame(names=c("OEVP","SPOE","FPOE","Gruene","Neos"), partyfacts_id=c(1329,1384,463,1659,1970)) 44 | ## this returns a simple vector of all hex triplets 45 | austrianparties$color <- partycolor(austrianparties$partyfacts_id) 46 | ## this returns a data frame consisting of colors in all three color models, party facts ids, 47 | color descriptions and color source. 48 | austriancolors <- partycolor(austrianparties$partyfacts_id, 49 | type='all', include_ids = TRUE, 50 | include_description = TRUE, include_source = TRUE) 51 | 52 | } 53 | \keyword{colors} 54 | \keyword{parties} 55 | \keyword{party} 56 | -------------------------------------------------------------------------------- /partycoloR.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | Encoding: UTF-8 9 | 10 | AutoAppendNewline: Yes 11 | StripTrailingWhitespace: Yes 12 | LineEndingConversion: Posix 13 | 14 | BuildType: Package 15 | PackageUseDevtools: Yes 16 | PackageInstallArgs: --no-multiarch --with-keep.source 17 | PackageRoxygenize: rd,collate,namespace 18 | --------------------------------------------------------------------------------