Does anyone know any bugs/tricks when using scale_colour_manual to set colors to predefined settings based on HEX color codes contained in your data frame?
I have been trying to to use the following code to pull predefined color codes from my data frame.
p <- ggplot(df, aes(x, y, colour=company))
p <- p + geom_point()
p <- p + scale_colour_manual(breaks=df$company, values = df$col)
p
However, for some reason when the plot is rendered the colors that are in the data (i.e. values=df$col) are not ploted.
Any assistance or guidance would be greatly appreciated. Below is my ggplot structure/data:
structure(list(data = structure(list(x = c(119, 34, 34, 46, 86,
70, 61, 71, 84, 30, 84, 85, 3, 16, 5, 167, 72, 85, 5, 44, 85,
63, 15, 86, 148, 83, 105, 11, 11, 98, 169, 14, 11, 28, 98, 52,
52, 52, 90, 11, 176, 94, 94, 52, 9, 176, 148, 9, 105, 121, 98,
2, 28, 9, 148, 43, 148, 170, 146, 40, 61, 40, 98, 9, 5, 3, 45,
46, 6, 128, 123, 39, 45, 2, 19), y = c(1195.26050420168, 4005.55882352941,
4102.94117647059, 3670.23913043478, 7705.47674418605, 7373.34285714286,
4745.40983606557, 5158.43661971831, 7686.20238095238, 7301.43333333333,
7309.02380952381, 7336.03529411765, 3475.33333333333, 5467.4375,
5117, 3861.70658682635, 8940.34722222222, 7317.41176470588, 4182.8,
6301.90909090909, 7272.83529411765, 4670.47619047619, 6489.53333333333,
7452.24418604651, 7762.66891891892, 7768.5421686747, 5560.00504761905,
9360.71454545455, 10984.6854545455, 6677.94989795918, 5029.19917159763,
17012.2242857143, 8613.21818181818, 5632.53571428571, 7572.70642857143,
12890, 8876.30557692308, 12993.5084615385, 364.15, 10914, 13005.2405113636,
11607.0582978723, 2946.36404255319, 14684.7426923077, 1783.90444444444,
11400.6083522727, 4940.83817567568, 1609.11111111111, 7636.22476190476,
318.785123966942, 8798.37673469388, 459.3, 8735.70928571429,
1616.36222222222, 6279.4397972973, 1564.2711627907, 5410.90743243243,
2569.18029411765, 6825.9702739726, 22469.3135, 6288.34426229508,
22726.2695, 11580.1029591837, 1733.73333333333, 2528.8, 1161.66666666667,
4126.06666666667, 4024.13043478261, 3336.5, 2928.9375, 2675.44715447154,
4249.79487179487, 7005.95555555556, 1601, 3982.26315789474),
company = c("chief", "chief", "chief", "chief", "CHK", "CHK",
"CHK", "CHK", "CHK", "CHK", "CHK", "CHK", "CHK", "CHK", "CHK",
"CHK", "CHK", "CHK", "CHK", "CHK", "CHK", "CHK", "CHK", "CHK",
"CHK", "CHK", "COG", "COG", "COG", "COG", "COG", "COG", "COG",
"COG", "COG", "COG", "COG", "COG", "COG", "COG", "COG", "COG",
"COG", "COG", "COG", "COG", "COG", "COG", "COG", "COG", "COG",
"COG", "COG", "COG", "COG", "COG", "COG", "COG", "COG", "COG",
"COG", "COG", "COG", "COG", "CRZO", "CRZO", "CRZO", "CRZO",
"CRZO", "Other", "Other", "WPX", "WPX", "WPX", "WPX"), col = c("#000000",
"#000000", "#000000", "#000000", "red", "red", "red", "red",
"red", "red", "red", "red", "red", "red", "red", "red", "red",
"red", "red", "red", "red", "red", "red", "red", "red", "red",
"blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue",
"blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue",
"blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue",
"blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue",
"blue", "blue", "blue", "blue", "blue", "blue", "darkgreen",
"darkgreen", "darkgreen", "darkgreen", "darkgreen", "#000000",
"#000000", "orange", "orange", "orange", "orange")), .Names = c("x",
"y", "company", "col"), row.names = c(NA, -75L), class = "data.frame"),
layers = list(<environment>), scales = <S4 object of class structure("Scales", package = "ggplot2")>,
mapping = structure(list(x = quote(x), y = quote(y), colour = quote(company)), .Names = c("x",
"y", "colour"), class = "uneval"), options = structure(list(
labels = structure(list(x = "x", y = "y", colour = "company"), .Names = c("x",
"y", "colour"))), .Names = "labels"), coordinates = structure(list(
limits = structure(list(x = NULL, y = NULL), .Names = c("x",
"y")), wise = FALSE), .Names = c("limits", "wise"), class = c("cartesian",
"coord")), facet = structure(list(shrink = TRUE), .Names = "shrink", class = c("null",
"facet")), plot_env = <environment>), .Names = c("data",
"layers", "scales", "mapping", "options", "coordinates", "facet",
"plot_env"), class = "ggplot")
Combining the comments from baptiste and joran, either of these work, depending on what you want.
If you want to color points by the given color, use
scale_colour_identity():There is no scale, because the color itself is the scale.
If you want to map certain values of company to certain colors, and those colors just happen to be along in the original data set, then you need to pull that mapping out and use the manual scale.