更改特定坐标的颜色

时间:2019-02-07 10:57:54

标签: r ggplot2

我想更改某个坐标的颜色,实际上是具有注释的同一坐标。

有什么想法吗?

p1 <- ggplot(HiBAP1517, aes(BPM, Yld)) + 
  geom_point(shape=16) + 
  geom_smooth(method=lm, se = F) + 
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.title.y = element_text(color="black", size=14, face="bold"))
p2 <- p1 + 
  annotate(geom="text", x=1879, y=892.02, label="Rialto",
           color="darkorange", size = 5, hjust=1, vjust=1.3, fontface =2)
p3 <- p2 + 
  annotate(geom="text", x=1654.75, y=834.2375, label="Savannah",
           color="firebrick1", size = 5, hjust=1, vjust=1, fontface =2)
pfinal <- p3 + 
  labs(x = expression("AGDM"[PM]^{}*(gm^{-2})),
       y = expression("GY"*(gm^{-2})))

这是我的输出,但是我想更改这两个坐标的颜色(而不是黑色):

output

数据示例:

Genotype,BPM,Yld
1,1767.793447,747.0708034
2,2074.815941,775.8880562
3,2197.933995,854.3810136
5,2085.627286,845.9306447
6,1908.97774,841.4318038
7,2120.24666,875.5534429
8,2226.617509,764.3849451
9,2035.68002,810.2658242
10,2153.727,861.7024631
11,1993.568134,782.5763292
12,2013.199982,822.6565187
13,2078.275912,837.2819632
14,2042.456487,802.6913977
16,1840.058841,767.6509829
17,2013.338146,801.2064103
18,2087.151352,822.1910199
19,1988.038384,859.573342
20,2083.092896,887.2783898
21,2072.905795,861.3044422
23,1849.744525,723.5014595
24,1785.04038,747.4940519
25,2078.402869,835.7669124
26,1698.390774,681.256732
27,2065.842661,852.3073467
28,2020.285009,811.6889063
29,2039.137248,821.7951099
30,1855.665106,781.0350726
31,1792.32475,744.9001931
32,1992.616447,860.7054072
33,2025.79755,834.1452611
34,2023.274784,835.4102703
35,1703.837196,682.9995098
36,1740.44177,713.3121368
37,1970.331012,816.5239645
38,1990.223669,838.9949534
39,2081.559891,822.5936391
40,1968.990856,852.1259441
41,2178.322511,920.80226
42,1887.572381,721.0746569
43,2103.964882,821.6521912
44,2097.040605,873.0062511
45,1864.779016,755.1746154
46,1935.743565,895.4951282
47,2191.797365,888.7284615
48,1968.150754,863.7490909
49,1858.735915,759.7144347
50,1933.34954,774.4202087
51,1680.540128,717.2402198
52,1748.214736,783.3395385
53,2183.694734,855.5897436
54,2142.662802,912.635349
55,1892.205584,776.5070164
56,2230.304238,887.8378102
57,2141.882287,903.7212821
58,1983.755009,815.5541958
59,1954.653032,743.0290819
60,1801.192428,718.5391635
61,1920.709571,808.6727692
62,1796.291216,699.0526007
63,2026.074655,909.3961954
64,1863.574774,729.9547929
65,1924.971832,770.2818388
66,2129.910527,794.0297343
67,2090.201938,809.6094569
68,1987.074651,731.8146606
69,2053.104282,839.4181954
70,1872.403668,787.2339391
71,1961.144455,824.335206
72,2135.414422,881.9237509
73,1857.780642,779.9428159
74,2058.696424,840.2234927
76,2169.489819,805.3868184
77,1891.844601,756.8752683
78,2099.708756,830.6765073
79,1976.981377,786.4878009
81,1932.909878,800.0033701
82,2101.603045,834.2990498
83,1867.872044,735.4201911
84,1870.947954,703.6186056
85,2135.962836,798.3315211
86,1859.497846,762.135947
87,1966.35974,776.6730353
88,2088.086246,808.0767316
89,1964.134743,851.5441764
90,2211.81001,866.3412008
91,1881.56405,805.7430148
92,1921.941058,725.2508829
93,1576.551861,606.5037422
95,2249.995426,882.4130493
96,2092.694714,778.8794369
97,2099.861152,840.9202391
98,1837.6733,760.0247786
99,1986.16533,796.1227279
100,1981.047087,747.7190033
Rialto,1879,892.02
Savannah,1654.75,834.2375

1 个答案:

答案 0 :(得分:0)

我假设您要更改与标签相对应的 的颜色,因为未显示其沿x / y轴的精确坐标,并且在出现有关颜色的问题之前,您将需要做更多的工作来确定它们。如果不是这种情况,我将删除答案。

result

# define color corresponding to each genotype
HiBAP1517$color <- case_when(HiBAP1517$Genotype == "Savannah" ~ "firebrick1",
                             HiBAP1517$Genotype == "Rialto" ~ "darkorange",
                             TRUE ~ "black")

# plot
ggplot(HiBAP1517,
       # specify color aesthetic here for both geom_point & geom_text to inherit
       aes(x = BPM, y = Yld, color = color)) + 

  geom_point(shape = 16) +
  geom_smooth(color = "#3366FF", # maintain default color for geom_smooth line
              method = lm, se = F) +

  # position labels based on their coordinates, rather than hard-code them via annotate()
  geom_text(aes(label = ifelse(Genotype %in% c("Savannah", "Rialto"), 
                               Genotype, "")),
            hjust = 1, vjust = 1) +

  # use defined colors directly
  scale_color_identity() + 

  # other aesthetic parameters, irrelevant to the question at hand
  labs(x = expression("AGDM"[PM]^{}*(gm^{-2})),
       y = expression("GY"*(gm^{-2}))) +
  theme_classic() +
  theme(axis.title.x = element_text(color="black", size=14, face="bold"),
        axis.title.y = element_text(color="black", size=14, face="bold"))

使用的数据:

> dput(HiBAP1517)
structure(list(Genotype = c("1", "2", "3", "5", "6", "7", "8", 
"9", "10", "11", "12", "13", "14", "16", "17", "18", "19", "20", 
"21", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", 
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", 
"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", 
"55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", 
"66", "67", "68", "69", "70", "71", "72", "73", "74", "76", "77", 
"78", "79", "81", "82", "83", "84", "85", "86", "87", "88", "89", 
"90", "91", "92", "93", "95", "96", "97", "98", "99", "100", 
"Rialto", "Savannah"), BPM = c(1767.793447, 2074.815941, 2197.933995, 
2085.627286, 1908.97774, 2120.24666, 2226.617509, 2035.68002, 
2153.727, 1993.568134, 2013.199982, 2078.275912, 2042.456487, 
1840.058841, 2013.338146, 2087.151352, 1988.038384, 2083.092896, 
2072.905795, 1849.744525, 1785.04038, 2078.402869, 1698.390774, 
2065.842661, 2020.285009, 2039.137248, 1855.665106, 1792.32475, 
1992.616447, 2025.79755, 2023.274784, 1703.837196, 1740.44177, 
1970.331012, 1990.223669, 2081.559891, 1968.990856, 2178.322511, 
1887.572381, 2103.964882, 2097.040605, 1864.779016, 1935.743565, 
2191.797365, 1968.150754, 1858.735915, 1933.34954, 1680.540128, 
1748.214736, 2183.694734, 2142.662802, 1892.205584, 2230.304238, 
2141.882287, 1983.755009, 1954.653032, 1801.192428, 1920.709571, 
1796.291216, 2026.074655, 1863.574774, 1924.971832, 2129.910527, 
2090.201938, 1987.074651, 2053.104282, 1872.403668, 1961.144455, 
2135.414422, 1857.780642, 2058.696424, 2169.489819, 1891.844601, 
2099.708756, 1976.981377, 1932.909878, 2101.603045, 1867.872044, 
1870.947954, 2135.962836, 1859.497846, 1966.35974, 2088.086246, 
1964.134743, 2211.81001, 1881.56405, 1921.941058, 1576.551861, 
2249.995426, 2092.694714, 2099.861152, 1837.6733, 1986.16533, 
1981.047087, 1879, 1654.75), Yld = c(747.0708034, 775.8880562, 
854.3810136, 845.9306447, 841.4318038, 875.5534429, 764.3849451, 
810.2658242, 861.7024631, 782.5763292, 822.6565187, 837.2819632, 
802.6913977, 767.6509829, 801.2064103, 822.1910199, 859.573342, 
887.2783898, 861.3044422, 723.5014595, 747.4940519, 835.7669124, 
681.256732, 852.3073467, 811.6889063, 821.7951099, 781.0350726, 
744.9001931, 860.7054072, 834.1452611, 835.4102703, 682.9995098, 
713.3121368, 816.5239645, 838.9949534, 822.5936391, 852.1259441, 
920.80226, 721.0746569, 821.6521912, 873.0062511, 755.1746154, 
895.4951282, 888.7284615, 863.7490909, 759.7144347, 774.4202087, 
717.2402198, 783.3395385, 855.5897436, 912.635349, 776.5070164, 
887.8378102, 903.7212821, 815.5541958, 743.0290819, 718.5391635, 
808.6727692, 699.0526007, 909.3961954, 729.9547929, 770.2818388, 
794.0297343, 809.6094569, 731.8146606, 839.4181954, 787.2339391, 
824.335206, 881.9237509, 779.9428159, 840.2234927, 805.3868184, 
756.8752683, 830.6765073, 786.4878009, 800.0033701, 834.2990498, 
735.4201911, 703.6186056, 798.3315211, 762.135947, 776.6730353, 
808.0767316, 851.5441764, 866.3412008, 805.7430148, 725.2508829, 
606.5037422, 882.4130493, 778.8794369, 840.9202391, 760.0247786, 
796.1227279, 747.7190033, 892.02, 834.2375)), class = "data.frame", row.names = c(NA, 
-96L))
相关问题