R ggplot 无法设置对数轴限制
R ggplot cannot set log axis limits
我正在尝试使用 ggplot2
中的 scale_x_log10
在直方图上显示一些非常偏斜的数据分布。我最终会在同一帧中有许多相似的图,所以我需要将轴设置为特定限制(ylim=0,150 和 xlim=0,25000)。使用 here 中的示例,我正在尝试:
#histograms by age
PfMSP119_1_5hist <- ggplot(s1_to_5_adj, aes(x = PfMSP119_adj))+
geom_histogram(bins = 1500) +
ylim(c(0,150)) +
scale_x_log10(limit = c(0,25000)) +
xlab("MFI value") +
ylab("Frequency") +
labs(title = "Age 1-5") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank())
但是生成的图表是空白的,没有截断 x 轴。
尝试另一种方法,我分别调用 scale_x_log10
和 xlim
:
PfMSP119_1_5hist <- ggplot(s1_to_5_adj, aes(x = PfMSP119_adj))+
geom_histogram(bins = 1500) +
ylim(c(0,150)) +
xlim(c(0,25000)) +
scale_x_log10() +
xlab("MFI value") +
ylab("Frequency") +
labs(title = "Age 1-5") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank())
我收到警告:
Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.
生成的图表上的 x 轴未设置为我指定的限制。
数据:
dput(s1_to_5_adj$PfMSP119_adj)
c(63.83391104, 57.82601353, 61.69849758, 33.1065109, 34.98756265,
16.55325545, 35.36377301, 20.31535896, 57.18397337, 40.63071792,
33.47425393, 37.29988296, 24.86658864, 32.51784668, 32.51784668,
32.51784668, 31.56143942, 30.60503217, 83.20743121, 33.27774601,
147.2540261, 91.51380154, 95.67351979, 79.0946642, 45.63153704,
91.26307407, 54.75784444, 35.92885894, 26.47389606, 30.25588121,
78.94894005, 27.57408649, 16.68957866, 29.75098805, 57.32507454,
26.84845263, 20.31774794, 54.64094134, 20.58934021, 22.96503332,
22.17313562, 24.54882872, 26.13262412, 21.38123791, 19.00554481,
38.01108963, 60.18422524, 49.09765743, 61.76802064, 210.7428022,
180.8148303, 159.3041005, 101.6304046, 96.64240931, 174.5798362,
343.1479027, 1070.992956, 292.3111764, 587.5552408, 261.5158518,
267.7668987, 358.0142608, 1363.627725, 280.659379, 158.5420101,
247.5349309, 283.4312351, 425.5207724, 200.4210316, 456.2915647,
48.95313475, 120.267578, 223.008725, 290.6970101, 80.98419823,
41.23667352, 38.94574722, 54.98223137, 32.0729683, 46.96398929,
28.63657884, 576.3861393, 392.4025337, 201.5914116, 162.4230268,
440.554493, 191.8891511, 72.69486729, 54.33379256, 89.93179459,
53.8284401, 309.2272091, 554.3184044, 24.0321675, 30.89850107,
35.81701072, 81.37068455, 19.47332622, 19.47332622, 18.77785028,
15.3004706, 25.25721271, 49.02870703, 14.11432475, 14.11432475,
40.0154283, 15.56155545, 18.52566125, 22.97181995, 33.34619025,
16.82050109, 19.11420579, 37.46384334, 45.10952566, 23.70161517,
198.0231719, 16.05593286, 34.1505622, 26.76665686, 23.99769235,
22.15171602, 25.84366869, 33.22757403, 28.61263319, 23.99769235,
23.99769235, 91.37582858, 21.22872785, 23.07470419, 68.94089313,
288.2721413, 96.47749148, 29.68538199, 30.74557421, 18.47224286,
22.53613629, 23.64447086, 14.77779429, 19.21113257, 39.94087069,
58.3751187, 41.47705803, 89.09886539, 32.98155951, 15.22225823,
49.04949875, 21.14202533, 20.29634431, 34.41740114, 39.00638796,
6471.028532, 43.59537478, 56.21508853, 65.39306217, 30.97566103,
25.95917146, 30.43489068, 19.69316455, 27.74945915, 30.43489068,
20.5883084, 68.0309321, 39.38632911, 42.07176064, 30.43489068,
26.88069224, 43.68112489, 35.84092299, 35.84092299, 24.64063455,
22.40057687, 45.99563655, 51.25228073, 47.3097976, 67.02221326,
42.05315342, 40.73899237, 27.59738193, 53.88060282, 44.68147551,
77.53550162, 31.53986506, 60.88174922, 26.63576528, 22.83065596,
80.85857319, 21.87937863, 33.29470661, 24.73321062, 35.67289993,
62.78430389, 19.97682396, 22.38329865, 29.03779284, 29.03779284,
45.97650534, 22.61151537, 33.91727306, 28.26439422, 52.00648536,
47.48418228, 38.43957613, 49.74533382, 25.47063486, 25.47063486,
21.22552905, 61.02339601, 29.71574066, 27.59318776, 20.16425259,
20.16425259, 39.26722874, 36.08339938, 23.24303103, 44.46492892,
53.56002802, 40.42266266, 32.33813013, 25.26416416, 25.19439477,
35.99199253, 59.98665422, 59.98665422, 41.99065796, 32.39279328,
31.79292674, 19.8299787, 25.04839415, 19.8299787, 49.05310522,
205.6055687, 45.92205595, 57.40256993, 19.8299787, 48.78881242,
26.270899, 40.03184609, 45.03582685, 125.099519, 40.03184609,
42.73749092, 55.20259244, 37.39530456, 43.48186637, 39.4054414,
81.52849945, 63.86399123, 76.09326615, 74.86804452, 60.4198254,
95.88363596, 45.97160628, 89.31626363, 229.2012942, 53.85245307,
43.34465735, 34.1503361, 48.59855521, 61.96737844, 219.5800584,
44.45485844, 25.59522153, 110.4635877, 40.20515013, 27, 1333.076644,
28.38010597, 528.1853056, 160.8206005, 58.3368845, 84.5, 72.52693748,
320.2722567, 28.38010597, 35.17141806, 65.65331372, 28.13713445,
65.65331372, 59.79141071, 44.55046288, 24.61999264, 42.30690966,
43.93409849, 73.22349749, 120.4119736, 58.57879799, 73.22349749,
49.43107012, 35.83752583, 38.30907934, 34.60174908, 265.6920019,
22.24398155, 30.27653045, 38.42451986, 51.23269315, 47.57321506,
34.76504178, 109.7843425, 51.23269315, 153.6980794, 44.8286065,
23.19293513, 59.14198459, 88.71297689, 80.59544959, 36.52887284,
40.58763648, 54.50339756, 37.10869621, 47.54551702, 48.75660692,
48.75660692, 130.6677066, 62.40845686, 48.75660692, 46.80634265,
45.83121051, 56.55766403, 953.5460808, 52.63079018, 54.17875459,
32.50725276, 38.69911042, 30.95928834, 35, 45, 48, 47, 79, 11707.95725,
31, 83, 31, 18, 168, 47, 56, 31, 36, 28.68109761, 35.50993038,
34.14416383, 35.50993038, 92.59837032, 33.55013417, 28.18211271,
42.94417174, 33.55013417, 71.12628445, 64.41625761, 48.98319589,
47, 24, 30, 31, 27, 21, 34, 77.5, 20, 30, 30.5, 34, 33, 196,
47, 41, 114, 51.64431376, 45.68535448, 78.45963051, 49.657994,
71.50751135, 51.64431376, 59.5895928, 83.42542991)
我认为您的问题是 log(0) 未定义(或 R 的 -Inf),因此您不能在对数转换轴上将 x 限制设置为 0 而不会出错。
我通常的解决方法是将轴限制设置为 1(因为 log(1) = 0),如下所示。
ggplot(s1_to_5_adj, aes(x = PfMSP119_adj))+
geom_histogram(bins = 1500) +
ylim(c(0,150)) +
scale_x_log10(limit = c(1,25000)) +
xlab("MFI value") +
ylab("Frequency") +
labs(title = "Age 1-5") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank())
我正在尝试使用 ggplot2
中的 scale_x_log10
在直方图上显示一些非常偏斜的数据分布。我最终会在同一帧中有许多相似的图,所以我需要将轴设置为特定限制(ylim=0,150 和 xlim=0,25000)。使用 here 中的示例,我正在尝试:
#histograms by age
PfMSP119_1_5hist <- ggplot(s1_to_5_adj, aes(x = PfMSP119_adj))+
geom_histogram(bins = 1500) +
ylim(c(0,150)) +
scale_x_log10(limit = c(0,25000)) +
xlab("MFI value") +
ylab("Frequency") +
labs(title = "Age 1-5") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank())
但是生成的图表是空白的,没有截断 x 轴。
尝试另一种方法,我分别调用 scale_x_log10
和 xlim
:
PfMSP119_1_5hist <- ggplot(s1_to_5_adj, aes(x = PfMSP119_adj))+
geom_histogram(bins = 1500) +
ylim(c(0,150)) +
xlim(c(0,25000)) +
scale_x_log10() +
xlab("MFI value") +
ylab("Frequency") +
labs(title = "Age 1-5") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank())
我收到警告:
Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.
生成的图表上的 x 轴未设置为我指定的限制。
数据:
dput(s1_to_5_adj$PfMSP119_adj)
c(63.83391104, 57.82601353, 61.69849758, 33.1065109, 34.98756265,
16.55325545, 35.36377301, 20.31535896, 57.18397337, 40.63071792,
33.47425393, 37.29988296, 24.86658864, 32.51784668, 32.51784668,
32.51784668, 31.56143942, 30.60503217, 83.20743121, 33.27774601,
147.2540261, 91.51380154, 95.67351979, 79.0946642, 45.63153704,
91.26307407, 54.75784444, 35.92885894, 26.47389606, 30.25588121,
78.94894005, 27.57408649, 16.68957866, 29.75098805, 57.32507454,
26.84845263, 20.31774794, 54.64094134, 20.58934021, 22.96503332,
22.17313562, 24.54882872, 26.13262412, 21.38123791, 19.00554481,
38.01108963, 60.18422524, 49.09765743, 61.76802064, 210.7428022,
180.8148303, 159.3041005, 101.6304046, 96.64240931, 174.5798362,
343.1479027, 1070.992956, 292.3111764, 587.5552408, 261.5158518,
267.7668987, 358.0142608, 1363.627725, 280.659379, 158.5420101,
247.5349309, 283.4312351, 425.5207724, 200.4210316, 456.2915647,
48.95313475, 120.267578, 223.008725, 290.6970101, 80.98419823,
41.23667352, 38.94574722, 54.98223137, 32.0729683, 46.96398929,
28.63657884, 576.3861393, 392.4025337, 201.5914116, 162.4230268,
440.554493, 191.8891511, 72.69486729, 54.33379256, 89.93179459,
53.8284401, 309.2272091, 554.3184044, 24.0321675, 30.89850107,
35.81701072, 81.37068455, 19.47332622, 19.47332622, 18.77785028,
15.3004706, 25.25721271, 49.02870703, 14.11432475, 14.11432475,
40.0154283, 15.56155545, 18.52566125, 22.97181995, 33.34619025,
16.82050109, 19.11420579, 37.46384334, 45.10952566, 23.70161517,
198.0231719, 16.05593286, 34.1505622, 26.76665686, 23.99769235,
22.15171602, 25.84366869, 33.22757403, 28.61263319, 23.99769235,
23.99769235, 91.37582858, 21.22872785, 23.07470419, 68.94089313,
288.2721413, 96.47749148, 29.68538199, 30.74557421, 18.47224286,
22.53613629, 23.64447086, 14.77779429, 19.21113257, 39.94087069,
58.3751187, 41.47705803, 89.09886539, 32.98155951, 15.22225823,
49.04949875, 21.14202533, 20.29634431, 34.41740114, 39.00638796,
6471.028532, 43.59537478, 56.21508853, 65.39306217, 30.97566103,
25.95917146, 30.43489068, 19.69316455, 27.74945915, 30.43489068,
20.5883084, 68.0309321, 39.38632911, 42.07176064, 30.43489068,
26.88069224, 43.68112489, 35.84092299, 35.84092299, 24.64063455,
22.40057687, 45.99563655, 51.25228073, 47.3097976, 67.02221326,
42.05315342, 40.73899237, 27.59738193, 53.88060282, 44.68147551,
77.53550162, 31.53986506, 60.88174922, 26.63576528, 22.83065596,
80.85857319, 21.87937863, 33.29470661, 24.73321062, 35.67289993,
62.78430389, 19.97682396, 22.38329865, 29.03779284, 29.03779284,
45.97650534, 22.61151537, 33.91727306, 28.26439422, 52.00648536,
47.48418228, 38.43957613, 49.74533382, 25.47063486, 25.47063486,
21.22552905, 61.02339601, 29.71574066, 27.59318776, 20.16425259,
20.16425259, 39.26722874, 36.08339938, 23.24303103, 44.46492892,
53.56002802, 40.42266266, 32.33813013, 25.26416416, 25.19439477,
35.99199253, 59.98665422, 59.98665422, 41.99065796, 32.39279328,
31.79292674, 19.8299787, 25.04839415, 19.8299787, 49.05310522,
205.6055687, 45.92205595, 57.40256993, 19.8299787, 48.78881242,
26.270899, 40.03184609, 45.03582685, 125.099519, 40.03184609,
42.73749092, 55.20259244, 37.39530456, 43.48186637, 39.4054414,
81.52849945, 63.86399123, 76.09326615, 74.86804452, 60.4198254,
95.88363596, 45.97160628, 89.31626363, 229.2012942, 53.85245307,
43.34465735, 34.1503361, 48.59855521, 61.96737844, 219.5800584,
44.45485844, 25.59522153, 110.4635877, 40.20515013, 27, 1333.076644,
28.38010597, 528.1853056, 160.8206005, 58.3368845, 84.5, 72.52693748,
320.2722567, 28.38010597, 35.17141806, 65.65331372, 28.13713445,
65.65331372, 59.79141071, 44.55046288, 24.61999264, 42.30690966,
43.93409849, 73.22349749, 120.4119736, 58.57879799, 73.22349749,
49.43107012, 35.83752583, 38.30907934, 34.60174908, 265.6920019,
22.24398155, 30.27653045, 38.42451986, 51.23269315, 47.57321506,
34.76504178, 109.7843425, 51.23269315, 153.6980794, 44.8286065,
23.19293513, 59.14198459, 88.71297689, 80.59544959, 36.52887284,
40.58763648, 54.50339756, 37.10869621, 47.54551702, 48.75660692,
48.75660692, 130.6677066, 62.40845686, 48.75660692, 46.80634265,
45.83121051, 56.55766403, 953.5460808, 52.63079018, 54.17875459,
32.50725276, 38.69911042, 30.95928834, 35, 45, 48, 47, 79, 11707.95725,
31, 83, 31, 18, 168, 47, 56, 31, 36, 28.68109761, 35.50993038,
34.14416383, 35.50993038, 92.59837032, 33.55013417, 28.18211271,
42.94417174, 33.55013417, 71.12628445, 64.41625761, 48.98319589,
47, 24, 30, 31, 27, 21, 34, 77.5, 20, 30, 30.5, 34, 33, 196,
47, 41, 114, 51.64431376, 45.68535448, 78.45963051, 49.657994,
71.50751135, 51.64431376, 59.5895928, 83.42542991)
我认为您的问题是 log(0) 未定义(或 R 的 -Inf),因此您不能在对数转换轴上将 x 限制设置为 0 而不会出错。
我通常的解决方法是将轴限制设置为 1(因为 log(1) = 0),如下所示。
ggplot(s1_to_5_adj, aes(x = PfMSP119_adj))+
geom_histogram(bins = 1500) +
ylim(c(0,150)) +
scale_x_log10(limit = c(1,25000)) +
xlab("MFI value") +
ylab("Frequency") +
labs(title = "Age 1-5") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank())