简体   繁体   English

未在ggplot中为x轴因子分配颜色

[英]Colors not assigned for x-axis factors in ggplot

Why is color not being assigned when the x-axis is a factor? 当x轴是一个因子时,为什么没有分配color I have set specific colors ( color = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3","grey40") ), but they are ignored. 我已经设置了特定的颜色( color = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3","grey40") ),但是它们被忽略了。

Sample Code 样例代码

    newdat <- structure(list(crop = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), .Label = c("a1", "a2", "a3", "a4", "a5", "a6"), class = "factor"), 
    tavgdiff = c(1.09, 0.8, 2.25, 0.84, 0.79, 0.75, 0.82, 0.85, 
    0.86, 0.81, 0.82, 1.76, 0.76, 0.77, 0.78, 1.16, 0.76, 0.89, 
    0.88, 0.85, 0.85, 0.35, -0.1, -0.09, 0.35, 0.33, 0.33, 0.42, 
    0.32, 2.25, 1.76, 0.92, 0.92, 1.16, 1.76, 1.16, 0.34, 0.39, 
    0.34, 0.34, 0.35, 1.09, 0.8, 2.25, 0.84, 0.79, 0.74, 0.75, 
    0.69, 0.82, 0.85, 0.72, 0.86, 0.81, 0.82, 0.74, 0.7, 0.89, 
    0.74, 0.73, 0.73, 0.85, 0.85, 0.74, 1.09, 0.8, 2.25, 0.84, 
    0.79, 0.82, 0.85, 0.86, 0.81, 0.82, 1.76, 0.76, 0.92, 0.92, 
    0.79, 0.77, 0.78, 1.16, 0.76, 0.89, 0.88, 0.85, 0.85)), .Names = c("crop", 
"tavgdiff"), row.names = c(NA, -87L), class = "data.frame")

ggplot code ggplot代码

ggplot(NULL, aes(y = tavgdiff, x = crop, color = crop), 
             color = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3","grey40")) +
  geom_point(data = newdat, size = 1) 

Plot 情节

在此处输入图片说明

Manual color assignments are assigned using scale_colour_manual 使用scale_colour_manual分配手动颜色分配

ggplot(NULL, aes(y = tavgdiff, x = crop, color = crop)) +
  geom_point(data = newdat, size = 1) +
  scale_colour_manual(values = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3","grey40"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM