Vedda
Vedda

Reputation: 7435

Colors not assigned for x-axis factors in ggplot

Why is color not being assigned when the x-axis is a factor? I have set specific colors (color = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3","grey40")), but they are ignored.

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(NULL, aes(y = tavgdiff, x = crop, color = crop), 
             color = c("#F8766D", "#A3A500", "#00BF7D", "#00B0F6", "#E76BF3","grey40")) +
  geom_point(data = newdat, size = 1) 

Plot

enter image description here

Upvotes: 0

Views: 121

Answers (1)

Djork
Djork

Reputation: 3369

Manual color assignments are assigned using 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"))

Upvotes: 2

Related Questions