1. ホーム
  2. r

[解決済み] scale_colour_manual の使い方を教えてください。

2022-02-15 01:07:50

質問事項

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

<イグ

色そのものが尺度であるため、尺度は存在しない。

もし、ある会社のある値をある色にマッピングしたい場合、その色がたまたま元のデータセットに沿ったものであれば、そのマッピングを引き出し、手動でスケールを使用する必要があります。

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

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

<イグ