0

I have two graphs I made with the package ggplot2 with standard deviation like this:

( pplot_1 <- ggplot(data=data, aes(x=data$Group, y=data$Data_1)) 
+ stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), geom = "errorbar", aes(width=0.05)) 
+ stat_summary(fun.y = mean, geom = "line", color='red') 
+ xlab("X-Axis") 
+ ylab("Y-Axis")
)

( pplot_2 <- ggplot(data=data, aes(x=data$Group, y=data$Data_2)) 
+ stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), geom = "errorbar", aes(width=0.05)) 
+ stat_summary(fun.y = mean, geom = "line", color='red') 
+ xlab("X-Axis") 
+ ylab("Y-Axis")
)

Example:

myTable <- "ID Data_1 Data_2 Group
        1     -50     -100    5.0
        2     -44     -101    5.0
        3     -48     -99     5.0
        4     -50     -80     4.9
        5     -44     -81     4.9
        6     -48     -82     4.9
        7     -48     -79     4.9
        8     -44     -40     4.8
        9     -49     -45     4.8
       10     -48     -44     4.8
       11     -60     -35     4.8
       10     -50     -2      4.7
       11     -80      0      4.7
Data <- read.table(text=myTable, header = TRUE)

I want to plot them into one graph and manually change the scale of the axes to show the difference between them and their standard deviation over time (x-axis).

schande
  • 576
  • 12
  • 27

1 Answers1

2

stat_summary and stat_summary_bin inherit aesthetics by default. You can solve this by taking the y= aesthetic out of your ggplot() function and putting it into the aesthetics of your stat_summary commands instead, like so:

ggplot(data=Data, aes(x=Data$Group)) 
+ stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), geom = "errorbar", aes(y=Data$Data_1, width=0.05)) 
+ stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), geom = "errorbar", aes(y=Data$Data_2, width=0.05)) 
+ stat_summary(fun.y = mean, geom = "line", color='red', aes(y=Data$Data_1)) 
+ stat_summary(fun.y = mean, geom = "line", color='blue', aes(y=Data$Data_2)) 
+ labs(x = "X-Axis", y = "Y-Axis") `

This will take care of plotting them in a single graph, although I am uncertain what exactly you are looking for when you say you want to change the axes manually.