r - 如何在 ggplot2 中的刻度标签之间的 x 轴下方放置垂直线以及如何以灰色/白色交替构面背景颜色
问题描述
我有以下情节:我想在 de x 轴标签之间放置线条(请参阅第二个情节以了解我想要的红色内容)。我还想让第一个面的背景为灰色,第二个为白色,依此类推。
到目前为止,这是我的代码:
ggplot(DCE,aes(x=Level,y=Marginal))+
geom_point()+
geom_line(aes(group=Attribute), size=1)+ ## connect subjects with a line
#facet_grid(.~Attribute, scale="free")+ ## 1 row of panels by roost
facet_grid(.~Attribute, scale="free", switch = "x")+ ## indien je de facet labels onder wilt
theme_bw()+
zero_margin+ ## squash together
theme(axis.text.x = element_text(angle = 90, hjust=0.95,vjust=0.2))+
geom_errorbar(aes(ymin=DCE$marginLow, ymax=DCE$MarginHigh), width=.2, size=0.8,
position=position_dodge(0.05))+
theme(strip.background =element_rect(fill="white",colour = "white", size = 1))+
theme(strip.text = element_text(colour = 'black'))+
theme(strip.placement = "outside")+
geom_hline(yintercept=0,
color = "grey", size=0.5)
这是我的数据集的 str() :
> str(DCE)
tibble [13 x 6] (S3: tbl_df/tbl/data.frame)
$ Attribute : chr [1:13] "5-year progression free survival" "5-year progression free survival" "5-year progression free survival" "5-year progression free survival" ...
$ Level : Factor w/ 13 levels "*50%","60%","65%",..: 1 2 3 4 5 6 7 9 8 10 ...
$ Marginal : num [1:13] 0 0.12 0.17 0.26 0 0.08 0 -0.11 -0.02 0 ...
$ marginLow : num [1:13] NA 0.07 0.14 0.23 NA 0.05 NA -0.14 -0.06 NA ...
$ MarginHigh: num [1:13] NA 0.17 0.21 0.3 NA 0.1 NA -0.07 -0.01 NA ...
$ subject : num [1:13] 1 1 1 1 2 2 3 3 3 4 ...
我尝试按照此页面上的示例进行操作:Secondary x-axis labels for sample size with ggplot2 on R
我的脚本现在看起来像这样:
K <- ggplot(DCE,aes(x=Level,y=Marginal))+
geom_rect(data = DCE, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3)+
geom_point(colour = "black", size = 3)+
geom_line(aes(group=Attribute), size=1, color = "black")+ ## connect subjects with a line
#facet_grid(.~Attribute, scale="free")+ ## 1 row of panels by roost
facet_grid(.~Attribute, scale="free", switch = "x")+ ## indien je de facet labels onder wilt
theme_classic()+
#theme(panel.background = element_rect(fill = 'lightgrey'))+
zero_margin+ ## squash together
#theme(panel.border = element_rect(color = "black",
#fill = NA,
#size = 1))+
theme(axis.text.x = element_text(angle = 90, hjust=0.95,vjust=0.2))+
geom_errorbar(aes(ymin=marginLow, ymax=MarginHigh), width=.2, size=0.8,
position=position_dodge(0.05))+
theme(strip.background =element_rect(fill="white",colour = "white", size = 1))+
theme(strip.text = element_text(colour = 'black', size = 10, face="bold"))+
theme(strip.placement = "outside")+
geom_hline(yintercept=0,
color = "#5E5E5E", size=0.5)+ # or #5E5E5E
labs(
x = "",
y = "Average marginal effects ")+
scale_fill_identity()+
theme(axis.title.x = element_text(margin=margin(50,0,0,0))) +
coord_cartesian(clip='off')+
sub.div <- 1/length(DCE$Attribute)
for (i in 1:(length(DCE$Attribute)-1)) {
K <- K + annotation_custom(
linesGrob(
x=unit(c(sub.div*i,sub.div*i), 'npc'),
y=unit(c(0,-0.8), 'npc') # length of the line below axis
)
)
}
K
但这会导致混乱:
我就是想不通
解决方案
推荐阅读
- python - 将由空格分隔的 cmd 行输出解析为分组的键值对
- reactjs - 如何在反应中显示数组中的产品信息?
- sql - “InvalidCastException:无法在 .net core 3.1 中转换类型为“Microsoft.SqlServer.Types.SqlHierarchyId”的对象
- azure - 从所有订阅的恢复保管库获取所有 Azure 备份
- c++ - 运行时错误:有符号整数溢出:2 * 2147483647 不能用“int”类型表示
- html - 如何在 HTML 中将屏幕分成 20/80 或 30/70 的两半?
- amazon-cloudformation - 如何在 Amazon EventBridge / Amazon CloudWatch 中监控 Cloudformation 事件?
- android - 如何从另一个活动(B)调用一个方法(在活动(A)中)来处理该活动(A)?
- c++ - 如何覆盖 RTLD_DEEPBIND
- javascript - 如何在JS中找到两个相同数组之间的差异?