---
output:
html_document:
code_folding: hide
---
```{r, message=FALSE, fig.width=12, fig.height=8, dev='svg', class.source = 'fold-show'}
library(colorspace)
library(RColorBrewer)
library(ggplot2)
```
```{r, message=FALSE, fig.width=12, fig.height=8, dev='svg', class.source = 'fold-hide'}
hcl_palettes(plot = TRUE)
display.brewer.all()
pal.list <- row.names(brewer.pal.info)
for (i in 1:length(pal.list)) {
pal.name <- pal.list[i]
color.num <- brewer.pal.info[pal.name,]$maxcolors
color.codes <- brewer.pal(color.num,pal.name)
print(pal.name)
print(color.codes)
}
df <- iris[1:18, ]
df$number <- 1:nrow(df)
# Define the number of colors you want
mycolors <- colorRampPalette(brewer.pal(12, "Paired"))(18)
# Create a ggplot with 18 colors
# Use scale_fill_manual
df$Color = mycolors
p0 <- ggplot(df, aes(Color, number))
p <- p0 + geom_col(aes(fill = factor(number))) + scale_fill_manual(values = mycolors)
p <- p + theme(axis.text.x = element_text(angle = 30, hjust = 1)) + guides(fill=guide_legend(title="ID"))
p
df <- iris[1:14, ]
df$number <- 1:nrow(df)
mycolors <- c("#F0188E","#98DEF6","#D2C3DD","#00AA9E","#6A7B9F","#DD7931","#B24E9B","#004A7B","#BCD556","#512C83","#00944C","#00AFEA","#FAB46F","#999999")
df$Color = mycolors
p0 <- ggplot(df, aes(Color, number))
p <- p0 + geom_col(aes(fill = factor(number))) + scale_fill_manual(values = mycolors)
p <- p + theme(axis.text.x = element_text(angle = 30, hjust = 1)) + guides(fill=guide_legend(title="ID"))
p
```
```{r, message=FALSE, fig.width=8, fig.height=1, dev='svg', class.source = 'fold-hide'}
for (i in 2:8) {
X <- c(1:i)
Y <- rep(1, length(X))
p <- ggplot(as.data.frame(cbind(X,Y)),aes(x = X, y=Y, fill = factor(X))) + geom_bar(stat="identity") + theme(legend.position="none") + labs(x="", y="", title=paste(i, "Colors")) + scale_y_continuous(breaks=NULL, expand=c(0,0)) + scale_x_continuous(breaks=NULL, expand=c(0,0))
print(p)
print(ggplot_build(p)$data[[1]]$fill)
}
sessionInfo()
```