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"))
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_errorbarhwithgeom_errorbarand adjust youraesmappings 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 standardgeom_errorbarmight work with your flipped coordinates.2. Check Data Types and Values:
Ensure that all variables (
oddsratio,conflower,confupper, andterm) in youraes()mappings are available in yourLimit_water_Rounddata 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 withNAorNULLvalues.3. Inspect and Debug:
You might want to print out or inspect your
Limit_water_Rounddata frame to ensure it contains what you expect. If possible, try running theggplotcode 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):