gpt4 book ai didi

r - ggplot 将错误的颜色分配给十六进制颜色

转载 作者:行者123 更新时间:2023-12-03 09:20:44 24 4
gpt4 key购买 nike

我将 df 列中的十六进制颜色分配给 ggplot。然而,在图中,它显示了十六进制颜色的名称,但不是正确的颜色,颜色与十六进制颜色不匹配。

数据框和绘图代码:

str(Trun)
'data.frame': 1043 obs. of 12 variables:
$ X : int 1 2 3 4 5 6 7 8 9 10 ...
$ DE : num 36.5 37 40.2 36.8 38.8 ...
$ hex : chr NA NA NA NA ...

unique(Trun$hex)
[1] NA "#8A8F8C" "#507085" "#3F7767" "#917652" "#DBAD5D"

ggplot(data=Trun, aes(x=X, y=DE, colour=hex)) + geom_point() + ggtitle("DE with 35* sd values")

下面是用 dput 重新处理的数据

structure(list(X = 1:50, Sample = structure(c(1L, 12L, 23L, 34L, 
45L, 47L, 48L, 49L, 50L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L,
40L, 41L, 42L, 43L, 44L, 46L), .Label = c("Sample 1", "Sample 10",
"Sample 11", "Sample 12", "Sample 13", "Sample 14", "Sample 15",
"Sample 16", "Sample 17", "Sample 18", "Sample 19", "Sample 2",
"Sample 20", "Sample 21", "Sample 22", "Sample 23", "Sample 24",
"Sample 25", "Sample 26", "Sample 27", "Sample 28", "Sample 29",
"Sample 3", "Sample 30", "Sample 31", "Sample 32", "Sample 33",
"Sample 34", "Sample 35", "Sample 36", "Sample 37", "Sample 38",
"Sample 39", "Sample 4", "Sample 40", "Sample 41", "Sample 42",
"Sample 43", "Sample 44", "Sample 45", "Sample 46", "Sample 47",
"Sample 48", "Sample 49", "Sample 5", "Sample 50", "Sample 6",
"Sample 7", "Sample 8", "Sample 9"), class = "factor"), Time = structure(1:50, .Label = c("10:51:04 AM",
"10:51:05 AM", "10:51:06 AM", "10:51:07 AM", "10:51:08 AM", "10:51:09 AM",
"10:51:10 AM", "10:51:11 AM", "10:51:12 AM", "10:51:13 AM", "10:51:14 AM",
"10:51:15 AM", "10:51:16 AM", "10:51:17 AM", "10:51:18 AM", "10:51:19 AM",
"10:51:20 AM", "10:51:21 AM", "10:51:22 AM", "10:51:23 AM", "10:51:24 AM",
"10:51:25 AM", "10:51:26 AM", "10:51:27 AM", "10:51:28 AM", "10:51:29 AM",
"10:51:30 AM", "10:51:31 AM", "10:51:32 AM", "10:51:33 AM", "10:51:34 AM",
"10:51:35 AM", "10:51:36 AM", "10:51:37 AM", "10:51:38 AM", "10:51:39 AM",
"10:51:40 AM", "10:51:41 AM", "10:51:42 AM", "10:51:43 AM", "10:51:44 AM",
"10:51:45 AM", "10:51:46 AM", "10:51:47 AM", "10:51:48 AM", "10:51:49 AM",
"10:51:50 AM", "10:51:51 AM", "10:51:52 AM", "10:51:53 AM"), class = "factor"),
L = c(57.61, 57.16, 53.96, 57.3, 55.27, 57.9, 59.05, 55.13,
53.8, 57.59, 52.23, 57.93, 58.59, 56.27, 58.62, 61.25, 56.76,
56.64, 58.49, 53.99, 53.17, 56.77, 57.35, 53.43, 55.19, 54.5,
53.17, 53.88, 55.15, 61.81, 57.03, 55.97, 54.83, 59.53, 54.29,
56.84, 53.53, 55.38, 57.84, 58.32, 54.67, 52.72, 53.94, 55.17,
58.15, 53.55, 58.75, 56.07, 58.46, 60.33), C = c(4.56, 4.17,
5.14, 3.9, 3.63, 3.47, 4.3, 4.95, 5.76, 3.49, 4.7, 4.64,
5.64, 3.76, 2.25, 4.66, 5.96, 4.13, 5.32, 4.45, 4.11, 3.88,
5.47, 4.17, 5.92, 2.71, 5.2, 4.24, 5.78, 5.37, 4.71, 4.39,
3.83, 5.01, 4.62, 5.08, 4.74, 3.62, 3.59, 4.09, 3.32, 4.06,
4.09, 5.16, 3.1, 5.59, 3.06, 3.67, 4.56, 6.75), h = c(219.98,
226.13, 233.39, 221.78, 213.56, 214.16, 230.93, 229.57, 236.17,
230.59, 235.2, 237.58, 240.43, 228.53, 206.55, 234.13, 241.12,
231.82, 227.03, 231.32, 218.68, 230.31, 228.59, 229.36, 235.56,
197.32, 232.49, 228.39, 244.63, 235.78, 231.22, 228.92, 231.17,
244.2, 228.49, 234.01, 227.85, 226.29, 210.04, 232.38, 222.71,
220.69, 226.08, 233.15, 215.76, 236.26, 206.3, 219.79, 232.37,
246.63), L1 = c(57.61, 57.16, 53.96, 57.3, 55.27, 57.9, 59.05,
55.13, 53.8, 57.59, 52.23, 57.93, 58.59, 56.27, 58.62, 61.25,
56.76, 56.64, 58.49, 53.99, 53.17, 56.77, 57.35, 53.43, 55.19,
54.5, 53.17, 53.88, 55.15, 61.81, 57.03, 55.97, 54.83, 59.53,
54.29, 56.84, 53.53, 55.38, 57.84, 58.32, 54.67, 52.72, 53.94,
55.17, 58.15, 53.55, 58.75, 56.07, 58.46, 60.33), a = c(-3.49,
-2.89, -3.06, -2.91, -3.03, -2.87, -2.71, -3.21, -3.21, -2.22,
-2.68, -2.49, -2.79, -2.49, -2.01, -2.73, -2.88, -2.55, -3.63,
-2.78, -3.21, -2.48, -3.62, -2.72, -3.35, -2.58, -3.16, -2.81,
-2.48, -3.02, -2.95, -2.88, -2.4, -2.18, -3.06, -2.99, -3.18,
-2.5, -3.11, -2.5, -2.44, -3.08, -2.84, -3.1, -2.51, -3.1,
-2.74, -2.82, -2.79, -2.68), b = c(-2.93, -3.01, -4.13, -2.6,
-2.01, -1.95, -3.34, -3.77, -4.79, -2.7, -3.86, -3.92, -4.91,
-2.82, -1, -3.78, -5.22, -3.24, -3.89, -3.47, -2.57, -2.99,
-4.11, -3.17, -4.88, -0.81, -4.12, -3.17, -5.22, -4.44, -3.67,
-3.31, -2.99, -4.51, -3.46, -4.11, -3.52, -2.61, -1.8, -3.24,
-2.25, -2.64, -2.94, -4.13, -1.81, -4.65, -1.35, -2.35, -3.61,
-6.2), DE = c(36.52, 36.95, 40.24, 36.78, 38.77, 36.13, 35.08,
39.04, 40.47, 36.47, 41.92, 36.24, 35.72, 37.8, 35.35, 32.94,
37.57, 37.46, 35.74, 40.14, 40.9, 37.31, 36.9, 40.67, 39.11,
39.48, 41.02, 40.22, 39.15, 32.48, 37.13, 38.15, 39.24, 34.72,
39.84, 37.37, 40.61, 38.67, 36.2, 35.79, 39.35, 41.35, 40.14,
39.03, 35.86, 40.7, 35.25, 37.98, 35.69, 34.2), heihgtmm = c(53.1,
67.01, 80.16, 85.3, 86.37, 92.36, 91.12, 90.56, 91.02, 94.25,
96.79, 94.36, 96.26, 95.4, 93, 91.58, 91.92, 89, 81.52, 68.57,
53.54, 49.43, 54.31, 72.51, 81.95, 82.85, 86.77, 85.47, 90.1,
87.95, 90.1, 86.97, 89.42, 89.65, 87.56, 83.48, 76.43, 63.15,
54.92, 64.79, 67.47, 64.77, 61.6, 63.58, 69.27, 79.75, 83.41,
85.56, 88.47, 90.57), hex = structure(c(1L, 1L, 1L, 1L, 3L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 2L
), .Label = c("", "#507085", "#8A8F8C"), class = "factor")), .Names = c("X",
"Sample", "Time", "L", "C", "h", "L1", "a", "b", "DE", "heihgtmm",
"hex"), class = "data.frame", row.names = c(NA, -50L))

最佳答案

我创建了一个包含十六进制值作为字符的新变量,并将该变量添加到 aes 中。

#create a new variable, substituting red for missings and turning factor into string (you can add your own colour)
Trun$hex2 <- ifelse(Trun$hex=="","#CC0000",as.character(Trun$hex))

#plot
ggplot(data=Trun, aes(x=X, y=DE, colour=hex2)) + geom_point(size=3) + #larger vor visibility
ggtitle("DE with 35* sd values") +
scale_colour_identity()

enter image description here

关于r - ggplot 将错误的颜色分配给十六进制颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879188/

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