[英]Multiple plots in R by connecting two points using ggplot2
假设我有一个这种形式的数据集。
我想通过为每个 model 连接 LCL 和 UCL 为X1
和X2
制作一个。预期结果应该如下图所示
下点是 LCL,上点是每个 model 的 UCL。感谢任何帮助。 这是使用dput()
function 的数据
> dput(mydata)
structure(list(x1m1 = c(0.5, 1), x1m2 = c(0.2, 1.5), x1m3 = c(0.5,
1.25), x2m1 = c(0.24, 0.98), x2m2 = c(0.1, 1.4), x2m3 = c(1,
2)), class = "data.frame", row.names = c("LCL", "UCL"))
这是一个使用 tidyverse 的解决方案(在本例中为 dplyr 和 ggplot)
library(tidyverse)
mydata$point <- rownames(mydata) # integrate Rownames in dataframe
# pivot the data longer and separate the cols
mydata <- mydata %>% pivot_longer(-c(point),
names_to="X",
values_to="value"
) %>%
separate(X,into = c("X","model"), sep=2) %>%# separate the stringcol
pivot_wider(names_from = point, values_from=value) #pivot wider again to separate LCL and UCL Column
# use
ggplot(mydata,aes(x=X,color=model,ymin = LCL, ymax=UCL))+
geom_linerange(position=position_dodge(width=0.20))
# postion argument to dodge so that the lines are not plottet above each other
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.