R 将两个图重叠在一起
我正在处理两个情节 图1:茎叶图R 将两个图重叠在一起,r,ggplot2,plot,R,Ggplot2,Plot,我正在处理两个情节 图1:茎叶图 data("mtcars") x <- mtcars$wt stem(x) 1 | 5689 2 | 123 2 | 56889 3 | 22224444 3 | 55667888 4 | 1 4 | 5 | 334 数据(“mtcars”) xstem()实际上并不生成典型的图形图(我也不能保存或轻松地从其结构中提取信息),因此我不确定如何将stem()输出与图形图叠加,例如plot()或ggplot()生成的
data("mtcars")
x <- mtcars$wt
stem(x)
1 | 5689
2 | 123
2 | 56889
3 | 22224444
3 | 55667888
4 | 1
4 |
5 | 334
数据(“mtcars”)
xstem()
实际上并不生成典型的图形图(我也不能保存或轻松地从其结构中提取信息),因此我不确定如何将stem()
输出与图形图叠加,例如plot()
或ggplot()
生成的图形图
但是,将两个绘图信息放在同一个绘图上的一种替代方法是使用ggplot()
和annotate()
来显示绘图1的stem()
信息和geom_point()
来显示绘图2的散点图
stem(x) # copy and paste this stem-and-leaf plot to a variable
sal <- c("1 | 5689", "2 | 123", "2 | 56889", "3 | 22224444", "3 | 55667888", "4 | 1", "4 | ", "5 | 334")
# make sal into a single string, collapse using new lines ("\n")
sal2 <- paste(sal, collapse="\n\n") # here I am using two new lines to widen the line spacing
sal2 <- gsub("\\|" ," ", sal2) # change the vertical bar ("|") to a space if you want to later replace it with geom_segment() (optional depending on how you like your aesthetics)
sal_x_position <- min(x) + (max(x) - min(x))/2 # the center of x-axis will be the center of the stem-and-leaf
df <- data.frame(x, y)
ggplot(df, aes(x, y)) +
# plot Plot 1
annotate("text", x = sal_x_position, y = 0, label = sal2, angle = 90, hjust = 0, col = "red") +
geom_segment(aes(x = 2, y = 0.015, xend = 5, yend = 0.015), col = "red") + # add a line between the stems and leaves (caveat: must choose custom coordinates for its location)
# plot Plot 2
geom_point(pch = 8) +
geom_line() +
# customize other plot aesthetics
theme_bw() +
theme(panel.grid = element_blank())
stem(x)#将此茎叶图复制并粘贴到变量中
sal不幸的是,stem
函数没有返回任何东西,这使生活变得困难。另外,代码是用C编写的,这是可用的。我尝试使用简单的R函数复制stem
函数,这当然与C代码不匹配,但它适用于这个示例数据集。我当然没有采纳stem的任何论点(比例、宽度、原子)
现在我们需要从头开始重新设计stem
函数。我首先使用hist
函数来定义“最佳”断点,我猜这与stem
的作用类似
h <- hist(round(x,1), right=FALSE, plot=F)
bin <- h$breaks; bin
#[1] 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
要打印的实际字符(pch
)来自小数点后的第一位数字
pch <- as.character(round(10*(round(sort(x),1) %% 1))); pch
# [1] "5" "6" "8" "9" "1" "2" "3" "5" "6" "8" "8" "9" "1" "2" "2" "2"
#[17] "4" "4" "4" "4" "5" "5" "6" "6" "7" "8" "8" "8" "1" "2" "3" "4"
谢谢爱德华,这很有趣,我现在正在学习你的例子
par(mar=c(2,1,1,1))
plot(x, y, pch = 8, xaxt="n", yaxt="n", ylab="", col="grey)
h <- hist(round(x,1), right=FALSE, plot=F)
bin <- h$breaks; bin
#[1] 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
xgr <- sort(cut(round(x,1), breaks = bin, right=FALSE, labels = FALSE, include = TRUE))
library(data.table)
y <- rowid(xgr)/length(xgr); y
pch <- as.character(round(10*(round(sort(x),1) %% 1))); pch
# [1] "5" "6" "8" "9" "1" "2" "3" "5" "6" "8" "8" "9" "1" "2" "2" "2"
#[17] "4" "4" "4" "4" "5" "5" "6" "6" "7" "8" "8" "8" "1" "2" "3" "4"
at <- seq(min(x), max(x), length.out=length(bin)-1)
x <- rep(at, h$counts)
points(x, y, pch = pch, col="red")
axis(side=1, at=at, labels=trunc(bin[-length(bin)]), tck=-0.02, mgp=c(1,0.3,0), col="red", col.axis="red")
x <- mtcars$drat
stem(x, scale=0.5)
The decimal point is at the |
2 | 889
3 | 0111112222
3 | 567778999999
4 | 111224
4 | 9