- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有谁知道使用 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)
关于r - 如何使用 scale_colour_manual?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9831489/
有谁知道使用 scale_colour_manual 根据数据框中包含的 HEX 颜色代码将颜色设置为预定义设置时的任何错误/技巧? 我一直在尝试使用以下代码从我的数据框中提取预定义的颜色代码。 p
我知道之前有人问过一个相同的问题。 ggplot legend - scale_colour_manual not working 但是这个问题涉及一个比我这里的数据稍微复杂的数据集,答案建议重组数据
我想在使用两个 geom_line 图和 scale_colour_manual 的 ggplot 图表中为图例键标签使用一个变量。如果我明确使用字符串来定义图例键标签,一切正常,就像第 6.4.4
我正在使用 scale_colour_manual 来指定我需要的可能颜色。但是,如果我选择 red,我会得到刺眼的红色,而不是如果我一开始不使用 scale_colour_manual 时会出现的较
我正在尝试向我的图表添加图例,但没有显示任何内容。这是我的代码: ggplot(main, aes(x = ceiling(session/2))) + geom_line(aes(y = C_ove
我正在使用 ggplot 绘制散点图。我想要具有特定颜色和填充的点(例如,在 plot、colour="blue"、fill="cyan4" 中),但我可以'不知道如何。到目前为止我所掌握的是: gg
我将 ggpairs 用于 3 个组的数据。问题是并非所有变量都具有所有组,因此,一些相关性只需要显示 2 个组。由于 ggpairs 自动按字母顺序对组进行排序,因此着色不一致。第一种颜色始终分配给
使用 scale_colour_manual() , 可以从 RColorBrewer 中选择特定颜色用作颜色值吗? 例如,在: scale_colour_manual(breaks=c("A","B
我是一名优秀的程序员,十分优秀!