我正在制作一个小面包裹的森林图,我想手动设置y轴标签。
这是我的示例数据:
set.seed(1)
df <- data.frame(x=rnorm(10),y=c(1:5,1:3,1:2),
group=c(rep("a",5),rep("b",3),rep("c",2)),
name=c(paste("a",1:5,sep=""),paste("b",1:3,sep=""),paste("c",1:2,sep="")))
df$xmin <- df$x-runif(10,0.5,0.7)
df$xmax <- df$x+runif(10,0.5,0.7)
这是我正在尝试的代码:
library(ggplot2)
p <- ggplot(df,aes(y = y, x = x))+
geom_point()+
scale_y_discrete(limits = df$name, expand = c(.1,0))+
facet_wrap(~group,ncol=3,scales="free")+
geom_segment(aes(x = xmin, xend = xmax, y = y, yend = y))+
geom_vline(lty=2, aes(xintercept=0), colour = 'red')
产生这个数字:
正如您所见,中间和最右侧刻面的y轴刻度标签与df$name
不一致。
答案 0 :(得分:0)
ggplot(df,aes(y = name, x = x)) +
geom_point() +
facet_wrap(~group,ncol=3,scales="free") +
geom_segment(aes(x = xmin, xend = xmax, y = name, yend = name)) +
geom_vline(lty=2, aes(xintercept=0), colour = 'red')