0

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.

My code is:

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'))

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

Then I get the following errors:

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).

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!

GGamba
  • 13,140
  • 3
  • 38
  • 47
SoCo
  • 19
  • 3

1 Answers1

0

From the cowplot vignette:

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.

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))
GGamba
  • 13,140
  • 3
  • 38
  • 47
  • `geom_errorbarh` is a horizontal geom from the `ggstance` pacakage. It's not clear whether the OP was using that package, but it's possible that it wasn't a typo. – eipi10 Feb 19 '17 at 01:39
  • Thanks for the help! That sorted out all my problems! I was intending to use geom_errorbarh, but this just worked from ggplot2 as far as I know. – SoCo Feb 19 '17 at 20:10
  • Pls consider upvoting and accepting the answer if you think that can help others – GGamba Feb 19 '17 at 20:12