问题描述 投票:0回答:1

我有一系列的数据框,看起来像下面这个。

> head(df_maxima, 10)
        t distance_max intesity_max  s_n_max
1   4.034       49.706     1979.922 2.251441
2   4.244       49.706     2008.562 2.269629
3   8.068       77.321     2248.527 2.388716
4   8.278       77.321     2255.795 2.389224
5  12.102       99.412     2330.322 2.512899
6  12.312       99.412     2327.884 2.517018
7  16.136      121.504     2348.834 2.541273
8  16.346      121.504     2348.147 2.539562
9  20.170      138.073     2309.776 2.583442
10 20.380      138.073     2307.124 2.579166

> tail(df_maxima, 10)
          t distance_max intesity_max  s_n_max
139 280.653      331.375     1213.470 1.296612
140 280.863      331.375     1218.176 1.310372
141 284.687      331.375     1226.017 1.304955
142 284.897      331.375     1228.822 1.309873
143 288.721      325.852     1233.728 1.338480
144 288.930      325.852     1239.651 1.339542
145 292.754      325.852     1240.988 1.342206
146 292.965      325.852     1243.669 1.347340
147 296.789      325.852     1244.913 1.347174
148 296.999      325.852     1250.809 1.350358

我想把数据绘制成点的填充是基于s_n_max这一列的值。我还希望与s_n_max的某些值区间相匹配的颜色在不同的数据框和不同的图形中保持一致,以便能够直观地比较图形。这似乎是一个问题,因为s_n_max的值在不同的数据帧中可以是不同的,所以在一个数据帧中s_n_max的值可以在1.5和2.5之间,而在另一个数据帧中s_n_max的值可以在0.5和2或5之间。

我希望将标尺的颜色稳定地分配给s_n_max的4类值(s_n_max <1,1<= s_n_max <1.5,1,5 <= s_n_max <2,s_n_max >= 2),并且即使某个数据帧中的s_n_max错过了某些值,也要坚持这个颜色范围的值分配。

目前我用这段代码生成了下面的图形

my_pal_quant_2 <- RColorBrewer::brewer.pal(9, "Blues")

    ggplot()+
    geom_point(data=df_maxima, aes(x=t, y=distance_max, fill=cut(s_n_max, c(0,1,1.5,2,max(s_n_max)))), shape=21, col=my_pal_gray[5], stroke=0.01, size=3.5, alpha=1)+
    xlab("Time [s]") +
    ylab(paste("Distance from the centre", "\n" , "[\U003BCm]"))+
    theme_bw(base_size=18)+
    theme(plot.title = element_text(hjust = 0.5))+
    labs(fill="signal/noise") +
    scale_fill_manual(values =c(my_pal_quant_2[1],my_pal_quant_2[3], my_pal_quant_2[5], my_pal_quant_2[8]))+
   guides(aesthetics = "fill", fill = guide_legend(reverse = TRUE, override.aes = list(shape = 21, size= 10)))

plot 1

我以为在scale_fill_manual中引入断点并指定颜色的顺序就可以了,但从图中可以看到,浅色的颜色(my_pal_quant_2)。1)它并没有按照我的要求被分配到s_n_max值的第一个区间(s_n_max < 1)。

我想问题在于我必须修正我的比例尺的限制,但是如果我在图形的代码中这样指定限制的话

ggplot()+
geom_point(data=df_maxima, aes(x=t, y=distance_max, fill=cut(s_n_max, c(0,1,1.5,2,max(s_n_max)))), shape=21, col=my_pal_gray[5], stroke=0.01, size=3.5, alpha=1)+
xlab("Time [s]") +
ylab(paste("Distance from the centre", "\n" , "[\U003BCm]"))+
theme_bw(base_size=18)+
theme(plot.title = element_text(hjust = 0.5))+
labs(fill="signal/noise") +
scale_fill_manual(values =c(my_pal_quant_2[1],my_pal_quant_2[3], my_pal_quant_2[5], my_pal_quant_2[8]), limits=c(0,10))+
guides(aesthetics = "fill", fill = guide_legend(reverse = TRUE, override.aes = list(shape = 21, size= 10)))

结果如下图2所示--点的颜色填充消失了,限制似乎覆盖了断点。plot 2

有什么办法可以解决这个问题吗?

这里是我的数据的一个可重复使用的版本(我希望如此

> dput(df_maxima)
structure(list(t = c(4.034, 4.244, 8.068, 8.278, 12.102, 12.312, 
16.136, 16.346, 20.17, 20.38, 24.204, 24.414, 28.238, 28.448, 
32.272, 32.482, 36.306, 36.516, 40.34, 40.55, 44.374, 44.584, 
48.408, 48.618, 52.441, 52.652, 56.476, 56.686, 60.51, 60.72, 
64.544, 64.754, 68.578, 68.788, 72.611, 72.822, 76.645, 76.856, 
80.68, 80.89, 84.714, 84.924, 88.748, 88.958, 92.781, 92.992, 
96.816, 97.026, 98.175, 102, 102.21, 106.034, 106.244, 110.068, 
110.278, 114.102, 114.312, 118.136, 118.346, 122.17, 122.38, 
126.204, 126.414, 130.238, 130.448, 134.272, 134.482, 138.306, 
138.516, 142.34, 142.55, 146.373, 146.584, 150.408, 150.618, 
154.442, 154.652, 158.475, 158.686, 162.51, 162.72, 166.544, 
166.754, 170.578, 170.788, 174.612, 174.821, 178.645, 178.856, 
182.68, 182.89, 186.715, 186.924, 190.749, 190.958, 194.783, 
194.993, 198.817, 199.027, 200.175, 204, 204.21, 208.032, 208.242, 
212.067, 212.277, 216.102, 216.312, 220.135, 220.346, 224.17, 
224.381, 228.205, 228.415, 232.239, 232.449, 236.273, 236.483, 
240.307, 240.519, 244.344, 244.554, 248.378, 248.588, 252.411, 
252.621, 256.447, 256.657, 260.48, 260.691, 264.515, 264.725, 
268.55, 268.76, 272.584, 272.794, 276.618, 276.828, 280.653, 
280.863, 284.687, 284.897, 288.721, 288.93, 292.754, 292.965, 
296.789, 296.999), distance_max = c(49.706, 49.706, 77.321, 77.321, 
99.412, 99.412, 121.504, 121.504, 138.073, 138.073, 154.641, 
154.641, 160.164, 165.687, 176.733, 176.733, 182.256, 182.256, 
198.825, 198.825, 204.348, 204.348, 209.871, 209.871, 220.916, 
220.916, 226.439, 226.439, 231.962, 231.962, 237.485, 237.485, 
243.008, 243.008, 248.531, 248.531, 254.054, 254.054, 259.577, 
259.577, 265.1, 265.1, 265.1, 265.1, 276.146, 276.146, 281.668, 
281.668, 281.668, 287.191, 287.191, 292.714, 292.714, 298.237, 
298.237, 298.237, 298.237, 303.76, 303.76, 303.76, 303.76, 309.283, 
309.283, 314.806, 314.806, 320.329, 320.329, 320.329, 320.329, 
320.329, 320.329, 320.329, 320.329, 314.806, 314.806, 314.806, 
314.806, 314.806, 314.806, 314.806, 314.806, 314.806, 314.806, 
320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 
320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 
320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 
320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 320.329, 
320.329, 320.329, 320.329, 320.329, 320.329, 325.852, 325.852, 
331.375, 325.852, 331.375, 331.375, 331.375, 331.375, 331.375, 
331.375, 336.898, 336.898, 336.898, 336.898, 336.898, 336.898, 
336.898, 336.898, 336.898, 336.898, 336.898, 336.898, 331.375, 
331.375, 331.375, 331.375, 325.852, 325.852, 325.852, 325.852, 
325.852, 325.852), intesity_max = c(1979.92230381636, 2008.56166900881, 
2248.52723179505, 2255.79451634931, 2330.32234953711, 2327.88378516362, 
2348.8343415127, 2348.14680159507, 2309.77579065898, 2307.12423239007, 
2277.66403763103, 2276.1881433748, 2226.63189556725, 2223.04612992737, 
2187.44521152477, 2185.01854317659, 2113.40419145911, 2106.95311432289, 
2019.47872850255, 2014.75513931461, 1970.8748703856, 1967.29978877911, 
1892.83180486698, 1884.08321387381, 1860.28811848347, 1855.7989916887, 
1819.68600120569, 1815.31483959351, 1752.25959566801, 1748.94651654063, 
1707.44631517983, 1701.81874724901, 1672.31610178508, 1675.07262830824, 
1629.35577737165, 1622.95594527249, 1594.73271800544, 1592.26365342627, 
1565.15450424085, 1564.02814029807, 1529.45736225544, 1526.40298637471, 
1485.87646781073, 1482.32587007202, 1452.38332379034, 1454.36364601585, 
1432.32690458437, 1433.58090017712, 1416.20657028369, 1381.5077909554, 
1376.91061677601, 1364.03626066873, 1363.38629589693, 1359.20369261903, 
1361.42177642401, 1356.84766849606, 1355.1820901064, 1348.4887820217, 
1346.09076725648, 1323.16977340783, 1322.27276997107, 1306.4789364913, 
1304.89078683714, 1292.06132553484, 1291.47612962683, 1274.77496623058, 
1276.04234959366, 1271.43272691217, 1272.45637976839, 1264.41961088779, 
1262.66261404286, 1258.4304721833, 1257.08637663008, 1129.51935239079, 
1129.91981422936, 1122.72850546887, 1125.52847921021, 1114.91002191344, 
1112.57041678153, 1102.87212456803, 1103.0139141179, 1092.39671914476, 
1094.48738639713, 1090.77518264334, 1092.24215670475, 1092.36185428102, 
1094.1466007486, 1095.52481154781, 1096.7799517286, 1098.72028552569, 
1100.37722731199, 1100.14404983392, 1101.74967173289, 1098.72888304895, 
1101.63529496167, 1104.32279375354, 1106.09416482097, 1105.30981381202, 
1105.60346635643, 1112.34119682095, 1094.0590377904, 1096.12380055849, 
1094.31126456126, 1099.43669810833, 1109.82141151297, 1113.74550644555, 
1115.34987618966, 1118.59469793335, 1118.50411722512, 1121.03542726029, 
1113.02090147212, 1116.55315296217, 1116.52649565333, 1119.77567158107, 
1124.9297007421, 1128.98942805628, 1132.57644869893, 1138.45514326464, 
1140.14078719406, 1143.18950233488, 1150.2064714797, 1154.24855514644, 
1158.30088658969, 1162.97171227054, 1163.47885128066, 1168.31102965649, 
1169.73067354679, 1174.56368305098, 1174.84712548331, 1178.56944753478, 
1173.96461080875, 1178.97433433504, 1180.86639684943, 1185.49285459939, 
1195.04680018485, 1199.61087517408, 1206.40514550419, 1208.54362602938, 
1213.46993848896, 1218.17568229377, 1226.01745918658, 1228.82153926183, 
1233.72771711532, 1239.65120994883, 1240.98800664377, 1243.6691625855, 
1244.91314782737, 1250.80863046917), s_n_max = c(2.25144148537218, 
2.26962887230276, 2.38871589544491, 2.38922391175673, 2.51289889125301, 
2.5170179020928, 2.5412726374042, 2.53956231724249, 2.58344241539977, 
2.57916599135521, 2.42254325331079, 2.42816796633502, 2.37220684712286, 
2.40814651195294, 2.38006310500208, 2.37934846653566, 2.31890961006215, 
2.31608616128134, 2.26693619609292, 2.25612224153314, 2.0931881891597, 
2.09778079349956, 2.04645183980855, 2.03330490556759, 2.07703705705985, 
2.07014084276515, 2.00439268600544, 2.0013108262749, 1.93265951204332, 
1.93027690765212, 1.85367117349408, 1.84744199845415, 1.80213581085882, 
1.80267095818825, 1.79820713290254, 1.7910497892261, 1.73323861178584, 
1.72934330125006, 1.66232486917941, 1.66103765430581, 1.62892610676827, 
1.62460389580007, 1.58359519743233, 1.57663763185811, 1.59280956262804, 
1.59605224692903, 1.57513185505005, 1.57092109979469, 1.55201236893203, 
1.48736244804815, 1.48206570585183, 1.47833613438516, 1.4805189393531, 
1.49764205481733, 1.50533350109248, 1.49439604443481, 1.49045472310451, 
1.48702165261742, 1.48054230720796, 1.45413011763978, 1.45208336219909, 
1.43340417360416, 1.42969476117652, 1.4165296294318, 1.41088732655332, 
1.40026099710555, 1.39815883895423, 1.38917223571977, 1.39194103868671, 
1.37472546455223, 1.37955200329312, 1.36530070337863, 1.35742463825747, 
1.30031621280637, 1.30070123872516, 1.27445331707465, 1.2742742097163, 
1.24602906139047, 1.24817213460883, 1.22416583200257, 1.22489897712777, 
1.20550590696318, 1.20201181567261, 1.2273211640213, 1.22959755767437, 
1.22098561833547, 1.22306831038143, 1.2155481359841, 1.22077186429451, 
1.21416022186004, 1.21403825487672, 1.20445566473816, 1.20763840494796, 
1.19876531490627, 1.20279973453789, 1.20202981062653, 1.19847842971369, 
1.20046173375287, 1.19790682341363, 1.20114073410853, 1.18165803031087, 
1.1815533546873, 1.1803104555859, 1.18732518285618, 1.19454698002281, 
1.20073805437238, 1.20215081816326, 1.20169452668347, 1.201164773576, 
1.20537821528183, 1.20414555027744, 1.2082695928816, 1.20539379029823, 
1.20846070398292, 1.21463967864487, 1.22005674610479, 1.22609538571136, 
1.23340567583566, 1.22406225441233, 1.23818075625747, 1.23378181088266, 
1.23695367736438, 1.24586694689738, 1.2453508392526, 1.25057586814617, 
1.25490794009328, 1.24031510917234, 1.24841997032498, 1.25355962979258, 
1.25341734386246, 1.24963712427607, 1.25490871831107, 1.2587833687635, 
1.26083720278796, 1.27207911317142, 1.27470951063669, 1.28279376500857, 
1.2863610194323, 1.29661211997708, 1.31037230050035, 1.30495528634301, 
1.30987283392375, 1.33848020532095, 1.3395424299679, 1.34220559786466, 
1.34734031085204, 1.34717399611496, 1.35035801558689)), .Names = c("t", 
"distance_max", "intesity_max", "s_n_max"), row.names = c(NA, 
-148L), class = "data.frame")
> 
r ggplot2 colors scatter-plot fill
1个回答
0
投票

这里一个可能的解决方法是在你的4个组之外生成你的 ggplot2 并通过他们的等级 breaks 的论点 scale_fill_manual 功能。

library(dplyr)

DF <- df_maxima %>% 
  mutate(Group = cut(s_n_max, breaks = c(0,1,1.5,2, Inf), include.lowest = TRUE)) 

library(ggplot2)

ggplot(DF, 
       aes(x = t, y = distance_max,
           fill = Group))+
  geom_point(color = my_pal_quant_2[5], stroke = 0.01, shape = 21, size = 3.5, alpha = 1)+
  scale_fill_manual(breaks = levels(DF$Group), drop = FALSE,
                      values = my_pal_quant_2[c(1,3,5,8)])+
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

它能回答你的功能吗?

© www.soinside.com 2019 - 2024. All rights reserved.