强制y轴从0开始,插入"break",并使用ggplot2设置较大的y轴 [英] Force y axis to start at 0, insert "break", AND have a large y axis using ggplot2

查看:127
本文介绍了强制y轴从0开始,插入"break",并使用ggplot2设置较大的y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长期潜伏着另一个ggplot问题.

我仍在努力确定可复制的数字以供出版.审稿人希望看到下图的y轴从0开始,并包括换行符"//".y轴不仅需要非常大(以1500个单位为单位),而且还必须紧密缩放(以300个单位为单位).这使得审阅者希望我们添加一个换行符来表示我们的轴从0开始,但继续.

可以创建的示例:

想要的示例(请注意y轴;这是在powerpoint中手动完成的,类似图中):

有什么想法吗?谢谢!!

我的代码:

  ggplot(data = quad2,aes(x,预测的,组=组))+geom_point(aes(shape = group),size = 6)+scale_shape_manual(values = c(19,1))+geom_line(大小= 2,aes(linetype = group),颜色=黑色")+scale_linetype_manual(values = c("solid","dashed"))+geom_linerange(大小= 1,aes(ymin =预测-最低配置,ymax =预测+高配置),颜色=黑色",alpha = .8)+geom_segment(aes(xend = x,yend = ifelse(group =="Control",conf.high,conf.low)),箭头=箭头(角度= 90),颜色=红色")+实验室(x =时间",y = expression(bold("QUAD Volume(cm" ^"3" *)")),linetype =",shape =")+#传奇标题scale_y_continuous(极限= c(1500,2000)) 

可复制的数据:

  dput(quad2)structure(list(x = structure(c(1L,1L,2L,2L,3L,3L,4L,4L,5L,5L),.Label = c("PRE","MID1","MID2","MID3","POST"),class ="factor"),预测= c(1666.97185871754,1660.27445165342,1743.2831065274,1678.48945165342、1788.50605542978、1637.40907049806、1807.55826371403,1639.78265640012、1865.8766220711、1652.91070173056),标准错误= c(88.8033117577884,91.257045996107、92.9973963841595、95.3834973421298、95.0283457128716,97.3739053806999、95.6466346849776、97.9142418717957、93.3512943191676,95.5735155125126)conf.low = c(0,91.257045996107,0,95.3834973421298,0,97.3739053806999,0,97.9142418717957,0,95.5735155125126),conf.high = c(88.8033117577884,0,92.9973963841595,0,95.0283457128716、0、95.6466346849776、0、93.3512943191676,0),group = structure(c(1L,2L,1L,2L,1L,2L,1L,2L,1L,2L),.Label = c("Intervention","Control"),class ="factor"))),class ="data.frame",row.names = c(NA,-10L)) 

解决方案

绘制不连续轴非常困难,原因是您应该避免这样做.虽然我不同意您的审稿人,但是如果您确实希望y轴中断,则可以随意使用基础网格图形.

首先进行绘制.我添加的唯一内容是y轴格式和轴线主题.我们将底部刻度标记为"0".

  plt<-ggplot(data = quad2,aes(x,预测的,组=组))+geom_point(aes(shape = group),size = 6)+scale_shape_manual(values = c(19,1))+geom_line(大小= 2,aes(linetype = group),颜色=黑色")+scale_linetype_manual(values = c("solid","dashed"))+geom_linerange(大小= 1,aes(ymin =预测-低配置,ymax =预测+高配置),颜色=黑色",alpha = .8)+geom_segment(aes(xend = x,yend = ifelse(group =="Control",conf.high,conf.low)),箭头=箭头(角度= 90),颜色=红色")+实验室(x =时间",y = expression(bold("QUAD Volume(cm" ^"3" *)")),linetype =",shape =")+#传奇标题scale_y_continuous(limits = c(1400,2000),breaks = seq(1400,2000,by = 200),标签= c(0,seq(1600,2000,by = 200)),展开= c(0,0,0.05,0))+主题(axis.line = element_line()) 

然后,我们将其放入gtable并抓住y轴线:

  gt<-ggplotGrob(plt)is_yaxis<-其中(gt $ layout $ name =="axis-l")yaxis<-gt $ grobs [[is_yaxis]]#你应该抓住折线孩子yline<-yaxis $ children [[1]] 

现在,我们可以根据需要编辑行:

  yline $ x<-unit(rep(1,4),"npc")yline $ y<-unit(c(0,0.1,1,0.15),"npc")yline $ id<-c(1,1,2,2)yline $ arrow<-箭头(角度= 90) 

将其放回gtable对象并进行绘制:

  yaxis $ children [[1]]<-ylinegt $ grobs [[is_yaxis]]<-yaxis#网格绘图语法grid.newpage();grid.draw(gt) 

您可以根据需要在行编辑步骤中进行样式选择.

long-time lurker back with another ggplot question.

I am still working on finalizing a reproducible figure for publication. Reviewers would like to see the below plot's y-axis start at 0 and include line break "//". The y-axis will need to not only be pretty large (think, 1500 units) but also zoomed in pretty tightly (think, 300 units). This makes the reviewer want us to add a line break to denote that our axis starts at 0, but continues on.

Example of what I can create:

Example of what I want (note the y axis; this was done manually in powerpoint in a similar figure):

any thoughts?? Thank you!!

my code:

ggplot(data = quad2,
       aes(x, predicted, group = group)) +
  geom_point(aes(shape = group), size = 6) +
  scale_shape_manual(values=c(19, 1)) +
  geom_line(size = 2,
            aes(linetype = group),
            color = "black") +
  scale_linetype_manual(values = c("solid", "dashed")) +
  geom_linerange(size = 1,
                 aes(ymin = predicted - conf.low,
                     ymax = predicted + conf.high),
                 color = "black",
                 alpha = .8) +
   geom_segment(aes(xend = x,
                    yend = ifelse(group == "Control", conf.high, conf.low)),
                arrow = arrow(angle = 90), color = "red")+
    labs(x = "Time",
       y = expression(bold("QUAD Volume (cm"^"3"*")")),
       linetype = "",
       shape = "") + #Legend title
  scale_y_continuous(limits =c(1500, 2000)) 

reproducible data:

dput(quad2)
structure(list(x = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 
5L, 5L), .Label = c("PRE", "MID1", "MID2", "MID3", "POST"), class = "factor"), 
    predicted = c(1666.97185871754, 1660.27445165342, 1743.2831065274, 
    1678.48945165342, 1788.50605542978, 1637.40907049806, 1807.55826371403, 
    1639.78265640012, 1865.8766220711, 1652.91070173056), std.error = c(88.8033117577884, 
    91.257045996107, 92.9973963841595, 95.3834973421298, 95.0283457128716, 
    97.3739053806999, 95.6466346849776, 97.9142418717957, 93.3512943191676, 
    95.5735155125126), conf.low = c(0, 91.257045996107, 0, 95.3834973421298, 
    0, 97.3739053806999, 0, 97.9142418717957, 0, 95.5735155125126
    ), conf.high = c(88.8033117577884, 0, 92.9973963841595, 0, 
    95.0283457128716, 0, 95.6466346849776, 0, 93.3512943191676, 
    0), group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L), .Label = c("Intervention", "Control"), class = "factor")), class = "data.frame", row.names = c(NA, 
-10L))

解决方案

Plotting discontinuous axis is made difficult for a reason, that reason being that you should avoid doing it whenever possible. While I disagree with your reviewers, you can get down and dirty with the underlying grid graphics if you truly want a y-axis break.

First make your plot. The only thing I added was y-axis formatting and an axis line theme. We'll just label the bottom tick with "0".

plt <- ggplot(data = quad2,
       aes(x, predicted, group = group)) +
  geom_point(aes(shape = group), size = 6) +
  scale_shape_manual(values=c(19, 1)) +
  geom_line(size = 2,
            aes(linetype = group),
            color = "black") +
  scale_linetype_manual(values = c("solid", "dashed")) +
  geom_linerange(size = 1,
                 aes(ymin = predicted - conf.low,
                     ymax = predicted + conf.high),
                 color = "black",
                 alpha = .8) +
  geom_segment(aes(xend = x,
                   yend = ifelse(group == "Control", conf.high, conf.low)),
               arrow = arrow(angle = 90), color = "red")+
  labs(x = "Time",
       y = expression(bold("QUAD Volume (cm"^"3"*")")),
       linetype = "",
       shape = "") + #Legend title
  scale_y_continuous(limits =c(1400, 2000),
                     breaks = seq(1400, 2000, by = 200),
                     labels = c(0, seq(1600, 2000, by = 200)),
                     expand = c(0,0,0.05,0)) +
  theme(axis.line = element_line())

Then, we'll make this into a gtable and grab the y-axis line:

gt <- ggplotGrob(plt)

is_yaxis <- which(gt$layout$name == "axis-l")
yaxis <- gt$grobs[[is_yaxis]]

# You should grab the polyline child
yline <- yaxis$children[[1]]

Now we can edit the line as we see fit:

yline$x <- unit(rep(1, 4), "npc")
yline$y <- unit(c(0, 0.1, 1, 0.15), "npc")
yline$id <- c(1, 1, 2, 2)
yline$arrow <- arrow(angle = 90)

Place it back into the gtable object and plot it:

yaxis$children[[1]] <- yline

gt$grobs[[is_yaxis]] <- yaxis

# grid plotting syntax
grid.newpage(); grid.draw(gt)      

You can make stylistic choices at the line editing step as you see fit.

这篇关于强制y轴从0开始,插入"break",并使用ggplot2设置较大的y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆