如何使用ggplot

编程入门 行业动态 更新时间:2024-10-27 20:26:52
如何使用ggplot_build()手动设置ggplot框图中的凹槽?(How to customize notches in ggplot box plot after having setting them manually with ggplot_build ()?)

我有一个关于如何在ggplot boxplot中自定义槽口的问题。 该问题的作者回复了自己并给出了ggplot_build()的提示,但遗憾的是我无法使用它。

所以我的问题是:一旦你用ggplot_build()手动设置你的缺口限制,如何重用它来定制自己的盒子图?

这是我的数据

vib = c(3.94,-0.61,0.03,0.46) pla = c(0.784444444, 1.11,-1.98,-1.39) df = data.frame(value = c(vib, pla), group = c(rep("vibration", times = 4), rep("placebo", times = 4))) p <- ggplot(df, aes(x = group, y = value)) + geom_boxplot(notch=TRUE) gg <- ggplot_build(p) gg$data[[1]]$notchlower[1]<-sort(vib)[qbinom(c(.25, .975), length(vib), .5)][1] gg$data[[1]]$notchlower[2]<-sort(pla)[qbinom(c(.25, .975), length(pla), .5)][1] gg$data[[1]]$notchupper[1]<-sort(vib)[qbinom(c(.25, .975), length(vib), .5)][2] gg$data[[1]]$notchupper[2]<-sort(pla)[qbinom(c(.25, .975), length(pla), .5)][2] gg$data[[1]] ggplot(gg$data[[1]], aes(x = x)) + geom_boxplot(aes(ymin = ymin, lower = lower, middle = middle, upper = upper, ymax = ymax), stat = "identity", notch = TRUE)

以及由此产生的警告信息

Error in ifelse(notch, data$notchlower, NA) : replacement has length zero In addition: Warning message: In rep(yes, length.out = length(ans)) : 'x' is NULL so the result will be NULL

我究竟做错了什么? 非常感谢

I've got a question relative to this one about how to customize notches in ggplot boxplot. The question's author replied to himself and gave a hint with ggplot_build() but I'm unfortunately not able to use it.

So my question is: once you set manually your notch limits with ggplot_build(), how one reuses it to customize its own box plot?

Here is my data

vib = c(3.94,-0.61,0.03,0.46) pla = c(0.784444444, 1.11,-1.98,-1.39) df = data.frame(value = c(vib, pla), group = c(rep("vibration", times = 4), rep("placebo", times = 4))) p <- ggplot(df, aes(x = group, y = value)) + geom_boxplot(notch=TRUE) gg <- ggplot_build(p) gg$data[[1]]$notchlower[1]<-sort(vib)[qbinom(c(.25, .975), length(vib), .5)][1] gg$data[[1]]$notchlower[2]<-sort(pla)[qbinom(c(.25, .975), length(pla), .5)][1] gg$data[[1]]$notchupper[1]<-sort(vib)[qbinom(c(.25, .975), length(vib), .5)][2] gg$data[[1]]$notchupper[2]<-sort(pla)[qbinom(c(.25, .975), length(pla), .5)][2] gg$data[[1]] ggplot(gg$data[[1]], aes(x = x)) + geom_boxplot(aes(ymin = ymin, lower = lower, middle = middle, upper = upper, ymax = ymax), stat = "identity", notch = TRUE)

And the resulting warning message

Error in ifelse(notch, data$notchlower, NA) : replacement has length zero In addition: Warning message: In rep(yes, length.out = length(ans)) : 'x' is NULL so the result will be NULL

What am I doing wrong? Thanks a lot

最满意答案

使用ggplot_gtable来反转操作,而不是再次将其输入ggplot :

gg$data[[1]]$notchlower[1:2]<- c(-1,0) gg$data[[1]]$notchupper[1:2]<- c(0.5, 1) plot(ggplot_gtable(gg))

在此处输入图像描述

Use ggplot_gtable to reverse the operation, instead of feeding it into ggplot again:

gg$data[[1]]$notchlower[1:2]<- c(-1,0) gg$data[[1]]$notchupper[1:2]<- c(0.5, 1) plot(ggplot_gtable(gg))

enter image description here

更多推荐

本文发布于:2023-07-19 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!