r - 在 ggplot2 上显示水平误差线(带箭头?),即使它们超过 x 轴限制
问题描述
我是 R 的初学者。我希望误差条 (geom_errorbarh) 显示在绘图上,即使它们超过 x 轴限制。也许有一些迹象表明他们正在像箭头一样继续情节?我不想为少数异常值误差条扩展 x 轴限制,因为大部分绘图将被压扁到左侧。
data<-data.frame(index=c(1,2,3,1,2,3),
estimate=c("Network Estimate", "Indirect Estimate", "Direct Estimate",
"Network Estimate", "Indirect Estimate", "Direct Estimate"),
RR=c(1.15,2.2,1.12,1.84,1.21,2.73),
ci_l=c(0.5,0.98,0.64,0.87,0.36,1.11),
ci_u=c(2.44,12.09,1.97,1.9,2.8,11.72),
Comparison=c("Example 2", "Example 2", "Example 2",
"Example 1", "Example 1", "Example 1"))
xname<-"Relative Risk"
p <- ggplot(data, aes(y=index, x=RR, xmin=ci_l, xmax=ci_u))+
geom_point()+
geom_point(data=subset(data, Comparison=="All"), color="Black", size=2)+
geom_errorbarh(height=.1)+
scale_x_continuous(limits=c(0,3.5), breaks = c(0:3), name=xname)+
scale_y_continuous(name = "", breaks=1:6, labels =data$estimate)+
geom_vline(xintercept=1, color="black", linetype="dashed", alpha=.5)+
facet_grid(Comparison~., scales= "free", space="free") +
theme_grey()+
theme(text=element_text(family="Times",size=14, color="black"))+
theme(panel.spacing = unit(1, "lines"))+
theme(strip.text.y = element_text(angle=0))
p
解决方案
这回答了你的问题了吗?
ggplot(data, aes(y=index, x=RR, xmin=ci_l, xmax=ci_u))+
geom_point()+
geom_point(data=subset(data, Comparison=="All"), color="Black", size=2)+
geom_errorbarh(height=.1)+
scale_x_continuous(breaks = c(0:3),
#removed this
#limits = c(0, 3.5),
name = xname) +
#added this
coord_cartesian(xlim = c(0, 3.5)) +
geom_segment(
data = data %>% filter(Comparison == "Example 1"),
x = 3.6, y = 3,
xend = 3.65, yend = 3,
lineend = "round",
linejoin = "round",
size = 0.5,
arrow = arrow(length = unit(0.3, "cm"))) +
geom_segment(
data = data %>% filter(Comparison == "Example 2"),
x = 3.6, y = 2,
xend = 3.65, yend = 2,
lineend = "round",
linejoin = "round",
size = 0.5,
arrow = arrow(length = unit(0.3, "cm"))) +
scale_y_continuous(name = "", breaks=1:6, labels =data$estimate)+
geom_vline(xintercept=1, color="black", linetype="dashed", alpha=.5)+
facet_grid(Comparison~., scales= "free", space="free") +
theme_grey()+
theme(text=element_text(family="Times",size=14, color="black"))+
theme(panel.spacing = unit(1, "lines"))+
theme(strip.text.y = element_text(angle=0))
推荐阅读
- angular - Ionic Web 应用程序 - 后退导航使页面空白(白屏)
- mysql - 重置 mariadb 的 root 密码不起作用
- firebase - 如何在 Flutter 和 Firebase 中注册新用户?
- c# - 每次重新启动应用程序后,第一次调用 dbcontext 会冻结应用程序
- sql-server - 如何通过插入子目录处理 T-SQL 文件表中的名称配置?
- java - 如何在 GMAIL 上发送消息 JavaMail?
- pointers - 为什么 `std::ptr::null` 不能用于未定义大小的类型?
- laravel - 按月提取每个状态的计数
- prolog - Prolog 检查重复项以回答查询(简单的方法?)
- c# - 使用 C# Oledb 过滤 excel 电子表格并返回特定值