我想用这些数据绘制一个ggplot,但是我不断收到错误
"Error in if (max_y < 0.75) max_y = 0.75 : missing value where TRUE/FALSE needed"
下面是我的数据(即称为cumtable
):
X1 X2
cummean 1 NaN
X 2 NaN
X.1 3 NaN
X.2 4 0.085958396
X.3 5 0.579000051
X.4 6 NaN
X.5 7 0.828634359
X.6 8 NaN
X.7 9 NaN
X.8 10 0.286389237
X.9 11 0.060052680
X.10 12 NaN
X.11 13 0.523594292
X.12 14 0.114135103
X.13 15 NaN
X.14 16 NaN
X.15 17 NaN
X.16 18 NaN
X.17 19 0.740793870
X.18 20 0.709364041
X.19 21 0.483116357
X.20 22 NaN
X.21 23 NaN
X.22 24 0.995054034
X.23 25 0.783238116
X.24 26 NaN
X.25 27 NaN
X.26 28 0.578728321
X.27 29 1.023346667
X.28 30 0.777934859
X.29 31 0.898343165
X.30 32 0.484138182
X.31 33 0.074883785
X.32 34 0.430788239
X.33 35 0.483925567
X.34 36 NaN
X.35 37 0.484475666
X.36 38 NaN
X.37 39 0.428624931
X.38 40 0.098168286
X.39 41 NaN
X.40 42 0.923720114
X.41 43 0.891545077
X.42 44 0.557582982
X.43 45 0.162431464
X.44 46 0.698346912
X.45 47 0.963385003
X.46 48 0.130043928
X.47 49 0.777339044
X.48 50 0.188371148
X.49 51 NaN
X.50 52 0.969137929
X.51 53 0.083899745
X.52 54 0.110960336
X.53 55 NaN
X.54 56 NaN
X.55 57 NaN
X.56 58 0.770282794
X.57 59 NaN
X.58 60 0.495483189
X.59 61 0.466880181
X.60 62 0.915412418
X.61 63 0.340461942
X.62 64 0.284567410
X.63 65 NaN
X.64 66 0.764779562
X.65 67 9.021032202
X.66 68 0.717319265
X.67 69 NaN
X.68 70 0.660980523
X.69 71 0.278495425
X.70 72 0.327933658
X.71 73 0.415319194
X.72 74 0.605468929
X.73 75 0.636267888
X.74 76 0.610612250
X.75 77 0.848616067
X.76 78 NaN
X.77 79 0.524346792
X.78 80 0.858387070
X.79 81 NaN
X.80 82 0.885703729
X.81 83 0.571135262
X.82 84 0.363989504
X.83 85 -0.003483148
X.84 86 NaN
X.85 87 NaN
X.86 88 0.604364690
X.87 89 NaN
X.88 90 -1.485618961
X.89 91 NaN
X.90 92 0.790697810
X.91 93 NaN
X.92 94 0.670599183
X.93 95 0.082025677
X.94 96 NaN
X.95 97 0.618979367
X.96 98 NaN
X.97 99 0.570178950
X.98 100 NaN
这是我的代码:
trial1 = cumtable
max_y = ceiling(max(trial1$X2))
if (max_y < .75) max_y = .75
min_y = floor(min(trial1$X2))
if (min_y > .4) min_y = .4
last_mean = round(trial1[nrow(trial),2],9)
plot1 = ggplot(trial1, aes(x=index,y=cum_mean)) +
geom_line(colour = "blue") +
geom_abline(intercept=0.5,slope=0, color = 'red', size=.5) +
theme(plot.title = element_text(size=rel(1.5)),
panel.background = element_rect()) +
labs(x = "n (number of tosses)",
y = "Cumulative Average") +
scale_y_continuous(limits = c(min_y, max_y)) +
scale_x_continuous(trans = "log10",
breaks = trans_breaks("log10",function(x) 10^x),
labels = trans_format("log10",math_format(10^.x))) +
annotate("text",
label=paste("Cumulative mean =", last_mean,
"\nEV =", prob,
"\nSample size =", nflip),
y=(max_y - .20),
x=10^(log10(nflip)/2), colour="darkgreen") +
annotate("text",
label=paste("P(Heads)=", prob),
y=(max_y - .80),
x=10^(log10(nflip)/2), colour="red")
我认为这是因为last_mean = round(trial1[nrow(trial),2],9)
(第6行),所以我删除了这一行,但仍然遇到相同的错误。
我也尝试删除所有带有NA的行,但仍然出现相同的错误,我担心会得到离散的图。我不想删除任何NA,而是将它们替换为ggplot中的未填充点。
目标:
成功用数据绘制了ggplot
NaN将在ggplot上显示为未填充的点。
答案 0 :(得分:0)
我通常会发现诸如missing value where TRUE/FALSE needed
之类的任何错误,通常意味着将if ()
括号中的任何变量或表达式严格地视为不是TRUE或FALSE值。
现在您有类似if (max_y < .75)
的东西,但它的评估结果是类似if (NA < .75)
的东西,反过来又叫if(NA)
,这是不允许的。
我认为,如果将第二行代码max_y = ceiling(max(trial1$X2))
更改为max_y = ceiling(max(trial1$X2, na.rm = TRUE))
,则不会得出NA。然后,您的max_y
将成功成为数字并起作用。