Error in -x: invalid argument to unary operator using ggplot 2 multiline code

176 views Asked by At

I am trying to make a forest plot using odds ratios; however, I keep running into a problem. I get this message "Error in -x: invalid argument to unary operator" when I run the code.

I have checked for extra "+" at the beginning of my lines of code, but cannot seem to find any.

This is the code I am trying to run:

ggplot(Limit_water_Round, aes(y = term, x = oddsratio)) +
 geom_point(shape = 18, size = 5) +  
 geom_errorbarh(aes(xmin = conflower, xmax = confupper), height = 0.25) +
 geom_vline(xintercept = 1, color = "red", linetype = "solid", cex = 1, alpha = 0.5) +
 scale_y_continuous(name = "", breaks=1:11, labels = Limit_water_Round$term, 
 trans = "reverse") +
  xlab("Odds Ratio (95% CI)") + 
  ylab(" ") + 
  theme_bw() + theme(panel.border = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.text.x.bottom = element_text(size = 12, colour = "black"),
        axis.title.x = element_text(size = 12, colour = "black"))
1

There are 1 answers

3
Nijiati_Abulizi On BEST ANSWER

The error message you're encountering might result when there's an issue with the arithmetic operation being performed on your data, not necessarily an issue with the ggplot code itself. From your code, it seems like there is a typo in the geom_errorbarh.

Here are a few steps to troubleshoot and resolve your issue:

1. Correct typos for Horizontal Error Bars:

Replace geom_errorbarh with geom_errorbar and adjust your aes mappings appropriately. Since you are placing terms on the y-axis and odds ratios on the x-axis, you might not need a horizontal error bar function; the standard geom_errorbar might work with your flipped coordinates.

2. Check Data Types and Values:

Ensure that all variables (oddsratio, conflower, confupper, and term) in your aes() mappings are available in your Limit_water_Round data frame and have the correct data types and values. The error message might result from trying to perform arithmetic on non-numeric data types or dealing with NA or NULL values.

3. Inspect and Debug:

You might want to print out or inspect your Limit_water_Round data frame to ensure it contains what you expect. If possible, try running the ggplot code with a subset of your data or with dummy data to identify where the error might be occurring.

Suggested/Adjusted Code:

Here's a rough adjustment to your code (you may need to tweak it further based on your specific data and desired output):

ggplot(Limit_water_Round, aes(y = term, x = oddsratio)) +
 geom_point(shape = 18, size = 5) +  
 geom_errorbar(aes(xmin = conflower, xmax = confupper), height = 0.25) +
 geom_vline(xintercept = 1, color = "red", linetype = "solid", size = 1, alpha = 0.5) +
 scale_y_continuous(name = "", breaks = 1:11, labels = Limit_water_Round$term) +
 coord_flip() +
 labs(x = "Odds Ratio (95% CI)", y = " ") +
 theme_bw() + 
 theme(panel.border = element_blank(),
       panel.background = element_blank(),
       panel.grid.major = element_blank(), 
       panel.grid.minor = element_blank(), 
       axis.line = element_line(colour = "black"),
       axis.text.y = element_text(size = 12, colour = "black"),
       axis.text.x.bottom = element_text(size = 12, colour = "black"),
       axis.title.x = element_text(size = 12, colour = "black"))