itep-data / R-questions

:question: :hand: Ask your R questions here
0 stars 0 forks source link

Adding error bars to scatter plots with ggscatter #3

Open djamieleo13 opened 3 years ago

djamieleo13 commented 3 years ago

DATA: trueavgalt | concavg 2.7 | 9.555555556 13.5 | 14.33333333 27 | 30 67.5 | 59.11111111 135 | 139.8888889

I have attempted to plot this scatter plot priorly in ggplot2 and it works (attached image of my ideal plot):

library(googledrive) library(ggplot2) library(scales) library(reshape2) library(gridExtra) library(grid) library(dplyr) library(png) library(grid) library(tidyverse) library(reprex) library(ggpubr) library(cowplot) library(forcats) library(cowplot) library(ggrepel) library(ggpmisc) library(wesanderson) library(methods) library(mgcv)

datadil <- read.csv("G:\My Drive\1.Mohanty Lab\WRITE-NOW\Vera_Microplastics_Transport\Rstudio\JamieDillution\dillution_2.csv")

dilu <- ggplot(datadil, aes(x = trueavgalt, y = concavg)) + geom_point(size =3, shape =1, stroke = 2)+ stat_smooth(aes(x = trueavgalt), method = "lm", formula = y ~ x, se = FALSE)+ geom_errorbar(aes(ymin=concavg-stdev, ymax=concavg+stdev), width=.2, position=position_dodge(0.05))+ stat_regline_equation(label.y = 170, aes(label = ..rr.label..), size = 5) + labs(y= expression(Detected~(n~mL^{-1})), x=expression(Prepared~(n~mL^{-1})))+

theme_bw()+ theme(legend.position = "none")+ theme(legend.title = element_blank())+ theme(legend.position = c(0.88, 0.3))+ theme(axis.text.x=element_text(size = 14), axis.title.x=element_text(size = 14), axis.text.y=element_text(size=14), axis.title.y=element_text(size = 14), panel.background = element_blank(),

panel.grid.major = element_line(),

    #panel.grid.minor = element_line(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())

dilu

Ideal

Now I wanted to add Pearson's analysis, so I have switched to ggpubr (see second attached image for how close it is but without bars) but can't seem to add geom.errorbars without: Error in !is.null(facet.by) | combine : operations are possible only for numeric, logical or complex types

datadil <- read.csv("G:\My Drive\1.Mohanty Lab\WRITE-NOW\Vera_Microplastics_Transport\Rstudio\JamieDillution\dillution_2.csv") library ("ggpubr") ggscatter(datadil, x = "trueavgalt", y = "concavg", size =3, shape =1, stroke = 2, add = "reg.line", add.params = list(colors= "blue"), conf.int = FALSE, cor.coef = TRUE, cor.method = "pearson", geom_errorbar(aes(ymin= "concavg"-stdev, ymax="concavg"+stdev), width=.2, position=position_dodge(0.05)), xlab = "Prepared~(n~mL^{-1}", ylab = "Detected~(n~mL^{-1}" )

pearsontrial

PLEASE HELP ASAP

dKvale commented 3 years ago

That's looking good. Similar to your ggplot,I think you add extra chart elements the same way with the + sign. So you can try pulling out the geom_errorbar(..) code from ggscatter(), and add it at the end with a +.

Like this, dropping the quotes around the convavg column:

ggscatter(datadil,
          x = "trueavgalt", y = "concavg",
          size =3, shape =1, stroke = 2,
          add = "reg.line", 
          conf.int = FALSE, 
          cor.coef = TRUE, 
          cor.method = "pearson",
          add.params = list(colors= "blue"),
          xlab = "Prepared~(nmL^{-1}", ylab = "Detected(n~mL^{-1}" ) + 
geom_errorbar(data = datadil, 
                      aes(ymin = concavg-stdev, ymax = concavg+stdev),
                      width = 0.2,
                      position = position_dodge(0.05)) + 
labs(title = "Sample chart")