例如,我有数字:43.2, 45.3, 48.1, 54.2
。当我使用stem()函数绘制图形时,我得到以下信息:stem()函数显示如下
x <- c(43.2, 45.3, 48.1, 54.2)
stem(x)
#
# The decimal point is 1 digit(s) to the right of the |
#
# 4 | 3
# 4 | 58
# 5 | 4
但是从图中我只能知道数字是:
43,45,48,54
。所以我想要一个函数来绘制茎叶图,通过它我仍然可以知道确切的数字而无需四舍五入。此外,我希望它仅使用一位数字来表示词干,而在叶中显示其他数字。例如,
43.2
应该是茎4
和叶子3.2
,43.22
应该是茎4
和叶子3.22
。对于数字:
43.2, 45.3, 48.1, 54.2
。我想要这样的情节:最佳答案
x <- c(43.2, 45.3, 48.1, 54.2)
stem(x)
#
# The decimal point is 1 digit(s) to the right of the |
#
# 4 | 3
# 4 | 58
# 5 | 4
stem2(x)
#
# The decimal point is 1 digit(s) to the right of the |
#
# 4 | 3.2
# 4 | 5.3, 8.1
# 5 | 4.2
stem2 <- function(x) {
cx <- gsub('^.', ', ', as.character(sort(x)))
co <- capture.output(stem(x))
m <- gregexpr('\\d(?!.*\\|)', co, perl = TRUE)
l <- regmatches(co, m)
l <- t(do.call('rbind', lapply(l, `length<-`, max(lengths(l)))))
l[!is.na(l)] <- cx
regmatches(co, m) <- data.frame(l)
cat(gsub(' , ', ' ', co), sep = '\n')
}
关于r - 如何绘制茎叶图以显示真实的叶子而不四舍五入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39309193/