Title: Having trouble repeating colors correctly in GWAS/Manhattan plot -- Color discordance
Tags: r;dataframe;ggplot2;command-line;colors
Question: I'm plotting GWAS summary statistics as a Manhattan plot using ggplot2. Things are coming out just as expected, but the colors are not repeating properly. Whether I am repeating 2 colors or 4 colors, there is a repeat early on that results in two chromosomes next to each other as the same color. Even if I manually define all chromosome colors, I still have repetition between two neighboring chromosomes. I'm not sure why or what I am doing wrong?
Any help is appreciated! My code is as follows:
# read in data
data <- read.table(args[1], header = TRUE)
pval = args[2]
data = as.data.frame(data)
data[pval] <- lapply(data[pval], as.numeric)
# load libraries
library(ggplot2)
library(dplyr)
data$BPcum <- NA
s <- 0
nbp <- c()
for (i in unique(data$CHR)){
nbp[i] <- max(data[data$CHR == i,]$BP)
data[data$CHR == i,&quot;BPcum&quot;] <- data[data$CHR == i,&quot;BP&quot;] + s
s <- s + nbp[i]
axisdf = data %&gt;% group_by(CHR) %&gt;% summarize(center=( max(BPcum) + min(BPcum) ) / 2 )
# -log10(pval) and bonferonni cut off
data$logp <- -log10(data[pval])
bofferroni<- -log10(0.05/dim(data)[1])
# filter data so it can graph a little more easily
data<-data[!(data[pval]==0),]
data <- filter(data, data[pval] < 1e-1)
data$logp = unlist(data$logp)
# plot
plot <- ggplot(data, aes(x=BPcum, y=logp)) +
# add bofferroni cut off
geom_hline(yintercept=bofferroni, color = &quot;red&quot;, size=.65) +
# show all points
geom_point( aes(color=as.factor(CHR)), alpha=0.4, size=1.3) +
scale_color_manual(values = rep(c(&quot;#D2A18C&quot;, &quot;#785C50&quot;), 12)) +
# custom X axis:
scale_x_continuous( label = axisdf$CHR, breaks= axisdf$center ) +
# remove space between plot area and x axis
scale_y_continuous(expand = c(0, 0) ) +
# add title and lables
ggtitle(paste(args[2],&quot;Manhattan Plot&quot;)) +
xlab(&quot;BP&quot;) + ylab(&quot;-log10(PVALUE)&quot;) +
# custom the theme:
theme_bw() +
theme(
legend.position=&quot;none&quot;,
panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
pdf(paste(&quot;gwasplot&quot;, args[2], args[1], &quot;.pdf&quot;, sep=&quot;_&quot;), height=6, width=12)
dev.off()


Here is another answer: Okay I got help from a labmate that fixed the problem, we needed to reorder the levels of CHR because they were in order of “1, 10, 11, 12… 2, 21, 22” etc. instead of “1,2,3,4…“. To do this I added this line
# reorder factor levels for plot
data$CHR = factor(data$CHR, levels = c(&quot;1&quot;,  &quot;2&quot;,  &quot;3&quot;,  &quot;4&quot;,  &quot;5&quot;, &quot;6&quot;, &quot;7&quot;,  &quot;8&quot;,  &quot;9&quot;, &quot;10&quot;,
&quot;11&quot;, &quot;12&quot;, &quot;13&quot;, &quot;14&quot;, &quot;15&quot;, &quot;16&quot;, &quot;17&quot;, &quot;18&quot;, &quot;19&quot;, &quot;20&quot;,
&quot;21&quot;, &quot;22&quot;, &quot;23&quot;, &quot;24&quot;, &quot;X&quot;))
And I also changed this
geom_point( aes(color=as.factor(CHR)), alpha=0.4, size=1.3) +
geom_point( aes(color=CHR), alpha=0.4, size=1.3) +
Hope this is helpful for others!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:12/29/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><dataframe><ggplot2><command-line><colors>', 'Title': 'Having trouble repeating colors correctly in GWAS/Manhattan plot -- Color discordance', 'ViewCount': 98, 'id': 65487169, 'user_AboutMe': '', 'user_AccountId': 17038781.0, 'user_DisplayName': 'Dana Al-Hindi', 'user_Reputation': 31.0, 'user_Views': 38.0, 'user_id': 12327225.0}}