3

Edit: This question addresses how to color only subsets of the x-axis labels. This is not a duplicate question.

I have made the x-axis labels to represent a nucleotide sequence, and I would like to add color to different sections of the nucleotides. How? Thanks.

ggplot(data = miRNA3) + 
  geom_line(mapping = aes(x = Position, y = Count_combined)) +
  scale_y_continuous(breaks = seq(0, 120, 10)) +
  ylab("Count") +
  scale_x_continuous(breaks=1:150, labels=c("T", "G", "A", "T", "G", "T", "C", "C", "G", "T", "G", "T", "C", "C", "A", "C", "T", "C", "G", "T", "T", "G", "T", "T", "T", "T", "C", "A", "A", "C", "T", "T", "C", "T", "T", "C", "C", "C", "G", "C", "A", "A", "T", "T", "T", "A", "C", "C", "T", "T", "C", "A", "T", "G", "G", "T", "T", "A", "A", "A", "C", "A", "A", "T", "A", "A", "A", "T", "C", "A", "G", "C", "T", "A", "A", "G", "G", "T", "A", "T", "G", "G", "A", "C", "A", "C", "T", "G", "T", "A", "A", "C", "T", "A", "C", "T", "C", "T", "G", "A", "A", "G", "G", "T", "A", "A", "G", "T", "T", "G", "C", "G", "A", "G", "A", "G", "G", "A", "A", "G", "T", "T", "T", "C", "A", "A", "G", "T", "A", "G", "C", "A", "T", "T", "G", "G", "A", "T", "T", "C", "G", "G", "A", "C", "G", "T", "T", "A", "T", "G"), expand = c(0, 0)) +
xlab("Supercontig_1.4289:xxx-xxx") +
theme(panel.grid.minor.x=element_blank(),
      panel.grid.major.x=element_blank(),
      panel.grid.minor.y=element_blank())

enter image description here

Edit: I would like to make something like this (see the letters on the x-axis): enter image description here

Jon
  • 591
  • 2
  • 8
  • 19
  • 5
    Probably by supplying a list of colours to the theme and axis text. Hints in here probably (https://rstudio-pubs-static.s3.amazonaws.com/3364_d1a578f521174152b46b19d0c83cbe7e.html) – UnivStudent Feb 24 '17 at 20:53
  • also, for those of us that don't know what nucleotides *are* you might want to supply which element you'd like to see appear differently. X axis isn't enough. do you want the labels in different colours? the line? the background? – ike Feb 24 '17 at 20:58

1 Answers1

5
df = data.frame(x = 1:4, y = 1:4)
my_labs = c("G", "A", "A", "T")
my_cols = c("red", "blue", "blue", "chartreuse")

ggplot(df, aes(x, y)) + geom_point() +
  scale_x_continuous(breaks = 1:4, labels = my_labs) +
  theme(axis.text.x = element_text(color = my_cols))

enter image description here

I had no idea this was possible until I saw @UnivStudent's comment. Pretty cool!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Awesome! This did the trick, but now it seems the hiding of the gridlines is overwritten. [Image](http://i.imgur.com/Z6tQr6L.png) – Jon Feb 24 '17 at 21:21
  • Weird, if I combine my `theme` setting with yours I get colored labels with the gridlines still hidden. Edit your question (maybe with a simplified example as in my question, sharing data so it's reproducible) and show your code if you still have trouble. Maybe also mention which version of `ggplot2` you have. – Gregor Thomas Feb 24 '17 at 21:56
  • No, you're right. I just made a silly mistake. Everything works fine :) – Jon Feb 26 '17 at 05:26