这是我的输入数据:
head(pf, 2)
userid age dob_day dob_year dob_month gender tenure friend_count friendships_initiated likes likes_received mobile_likes
1 2094382 14 19 1999 11 male 266 0 0 0 0 0
2 1192601 14 2 1999 11 female 6 0 0 0 0 0
这是我的代码:
ggplot(aes(x = age, y = friend_count), data = pf) +
geom_jitter(alpha = 1/20) + xlim(13,90) +
geom_point(alpha = 0.05, position = position_jitter(h=0), color = 'orange') +
coord_trans(y = 'sqrt') +
geom_line(stat = 'summary', fun.y = mean) +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = .1), linetype = 2, color = 'blue') +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = .5), color = 'blue') +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(probs = .9), linetype = 2, color = 'blue')
但是,我收到以下错误消息:
Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
In addition: Warning messages:
1: Removed 4906 rows containing non-finite values (stat_summary).
2: Removed 4906 rows containing non-finite values (stat_summary).
3: Removed 4906 rows containing non-finite values (stat_summary).
4: Removed 4906 rows containing non-finite values (stat_summary).
5: In trans$transform(out$range) : NaNs produced
我如何解决这些问题?
答案 0 :(得分:1)
您的数据中是否有负值?如果是这样,那就是造成问题的原因。
您正在使用coord_trans(y='sqrt')
对数据进行平方根转换,但是负数的平方根是包含 i
的虚数(请参阅:{ {3}}。
我们可以在一个简化的示例中看到这一点。此函数正常绘制:
ggplot(cars, aes(x=speed, y = dist)) +
geom_point() +
coord_trans(y = 'sqrt')
但是,如果我们将cars
乘以-1
以得到负值,则会复制您的错误。
ggplot(-1 * cars, aes(x=speed, y = dist)) +
geom_point() +
coord_trans(y = 'sqrt')
Error in if (zero_range(range)) zero_width else diff(range) :
missing value where TRUE/FALSE needed
In addition: Warning message:
In trans$transform(out$range) : NaNs produced
那么,你能做什么?这取决于您的数据是什么以及为什么选择对其进行sqrt
转换。例如,您可以先获取绝对值,或者选择与负值兼容的转换。