0

How can I make the x axis be colored the same as the points.

Ideally, I'd like to not have to manually set colors and my real dataset has 20 points in each facet.

d = data.frame(x = c("A","B","C"),y = c(1,2,3), color = c("A","B","C"))
    ggplot(d, aes(x= x, y = y, color = color))+geom_point()
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • The entire x-axis, or the axis labels? – Anonymous coward Jul 26 '18 at 21:54
  • If you mean the labels, the question is a duplicate of https://stackoverflow.com/questions/24075446/how-to-get-axis-ticks-labels-with-different-colors-within-a-single-axis-for-a-gg – shadow Jul 26 '18 at 21:56
  • This is not a duplicate. I'd like to not have to manually set the color with col vec. Is there a way to dynamically get the colors the points use and color the x labels the same? – user3022875 Jul 26 '18 at 22:08
  • *"color the x labels the same"* I don't understand what you mean. Do you want to colour the axis *labels*? The *line*? If so how? And why? Can you provide a mock-up of your expected output plot? – Maurits Evers Jul 26 '18 at 22:35
  • If you look at the plot the points are colored. I'd like the x axis labels to be same color as the points. – user3022875 Jul 26 '18 at 23:47

1 Answers1

0

Try:

library(RColorBrewer)
myColors <- brewer.pal(length(levels(as.factor(d$color))),"Set1")
names(myColors) <- levels(as.factor(d$color))
colScale <- scale_colour_manual(name = "color",values = myColors)
ggplot(d, aes(x= x, y = y, color = color))+geom_point()+ theme(axis.text.x =element_text( colour = myColors))+colScale
Iman
  • 2,224
  • 15
  • 35