gpt4 book ai didi

r - 如何使用 scale_colour_manual?

转载 作者:行者123 更新时间:2023-12-04 12:09:39 30 4
gpt4 key购买 nike

有谁知道使用 scale_colour_manual 根据数据框中包含的 HEX 颜色代码将颜色设置为预定义设置时的任何错误/技巧?

我一直在尝试使用以下代码从我的数据框中提取预定义的颜色代码。

p <- ggplot(df, aes(x, y, colour=company)) 
p <- p + geom_point()
p <- p + scale_colour_manual(breaks=df$company, values = df$col)
p

但是,由于某些原因,当绘制绘图时,数据中的颜色(即 values=df$col)不会被绘制出来。

如有任何帮助或指导,我们将不胜感激。下面是我的 ggplot 结构/数据:

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

最佳答案

结合 baptiste 和 joran 的评论,这些都有效,具体取决于您想要什么。

df <- 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")

如果你想用给定的颜色给点上色,使用scale_colour_identity():

ggplot(df, aes(x, y, colour=col)) +
geom_point() +
scale_colour_identity()

enter image description here

没有刻度,因为颜色本身就是刻度。

如果您想将公司的某些值映射到某些颜色,而这些颜色恰好出现在原始数据集中,那么您需要将该映射拉出并使用手动比例尺。

colmapping <- unique(df[c("company","col")])

ggplot(df, aes(x, y, colour=company)) +
geom_point() +
scale_colour_manual(breaks=colmapping$company, values=colmapping$col)

enter image description here

关于r - 如何使用 scale_colour_manual?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9831489/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com