自ggplot2更新以来ggdraw的问题 [英] Issues with ggdraw since ggplot2 update

查看:118
本文介绍了自ggplot2更新以来ggdraw的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码做了一个图,但是,我最近注意到我的一些数据点没有包含应有的错误条,因此在编写该代码后的几个月我正在重新研究它.这一次对ggplot2进行了更新,现在我的代码中出现错误,我不知道如何解决.这些在下面详细说明.

I made a figure with the following code, however, I recently noticed some of my data points did not contain error bars as they should, so I am revisiting this code a few months after writing it. In this time there has been an update to ggplot2 and now I get errors in my code that I do not know how to fix. These are detailed below.

我的代码是:

p1 <- ggplot(dat3A, aes(x = d15N, y = Depth))

p4 <- p1 + 
    geom_point(size = 1) + 
    geom_path() + 
    scale_y_continuous(expand =  c(0, 0), limits = c(15,0), trans = "reverse") + 
    scale_x_continuous(expand = c(0, 0), limits = c(0,20)) + 
    labs(x=expression(paste(delta^'15', N[NO3],('\u2030'))), y='Depth (cm)') + 
    theme_bw() + 
    theme(axis.line = element_line(colour = "black"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),    
        panel.border = element_blank(), 
        panel.background = element_blank()) +
    geom_errorbarh(aes(xmax=d15N+d15NError, xmin=d15N-d15NError))+ 
    ggtitle("d") + 
    theme(plot.title = element_text(hjust = 0, size=8))+
    theme(axis.text = element_text(size=6), axis.title=element_text(size=8))
p13 <- ggdraw(switch_axis_position(p4, axis = 'x'))

运行时一切正常,直到我到达代码的最后一行 p13<-ggdraw(switch_axis_position(p4,axis ='x'))

When I run this all runs okay until I come to the last line of code p13 <- ggdraw(switch_axis_position(p4, axis = 'x'))

然后我得到以下错误:

gt $ grobs [[ixl]]中的错误:尝试选择少于一个元素另外:警告消息:1:不推荐使用 panel.margin .请使用 panel.spacing 属性代替2:删除了1行,其中包含缺少值(geom_errorbarh).

Error in gt$grobs[[ixl]] : attempt to select less than one element In addition: Warning messages: 1: panel.margin is deprecated. Please use panel.spacing property instead 2: Removed 1 rows containing missing values (geom_errorbarh).

我不是经验丰富的编码人员,所以不确定如何解决这些错误.我没有在代码中使用panel.margin,所以不确定如何将其更改为panel.spacing.如果有人能对此以及 gt $ grobs [[ixl]] 错误提供任何帮助,我将不胜感激!

I am not an experienced coder and so I am unsure how to solve these errors. I do not use panel.margin in my code so am not sure how to change this to panel.spacing. If anyone could provide any help on this as well as the gt$grobs[[ixl]] error I would really appreciate it!

推荐答案

来自 cowplot 插图:

从ggplot2 2.2.0开始,该软件包现在原生支持辅助轴和坐标轴在图的两边.因此,Cowplot功能switch_axis_position()已被弃用,因为它始终是难看的骇客,难以维护.

As of ggplot2 2.2.0, the package now natively supports secondary axes and axes on either side of the plot. Therefore, the cowplot function switch_axis_position() has been deprecated, since it always was an ugly hack and difficult to maintain.

实际上 ggplot2 现在具有 coord_flip 功能. switch_axis_position(p4,axis ='x')已替换为 + scale_x_continuous(position ='right')

Indeed ggplot2 now has the coord_flip function. switch_axis_position(p4, axis = 'x') as been replaced by + scale_x_continuous(position = 'right')

加上..您可能想整理一下该图形的构造.

Plus.. You probably want to tidy that graph construction a bit.

p13 <- ggplot(dat3A, aes(x = d15N, y = Depth)) + 
  geom_point(size = 1) + 
  geom_path() + 
  geom_errorbar(aes(xmax = d15N + d15NError, xmin = d15N - d15NError)) + 
  scale_y_continuous(expand =  c(0, 0), limits = c(15,0), trans = "reverse") + 
  scale_x_continuous(expand = c(0, 0), limits = c(0,20), position = 'right') + 
  labs(x=expression(paste(delta ^ '15', N[NO3], ('\u2030'))), y = 'Depth (cm)') + 
  ggtitle("d") + 
  theme_bw() + 
  theme(axis.line = element_line(colour = "black"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),    
        panel.border = element_blank(), 
        panel.background = element_blank(),
        plot.title = element_text(hjust = 0, size=8),
        axis.text = element_text(size=6),
        axis.title=element_text(size=8))

这篇关于自ggplot2更新以来ggdraw的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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