颜色轴与点相同

时间:2018-07-26 21:50:55

标签: r ggplot2

如何使x轴的颜色与点相同。

理想情况下,我不想手动设置颜色,而我的真实数据集的每个方面都有20个点。

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()

1 个答案:

答案 0 :(得分:0)

尝试:

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