transform - R and ggplot2: How to turn axes into logarithmic scale? Given confidence interval constraints -
i'm new here , appreciate help! i've got simple r script plot log transformed data using ggplot , plot on 95% confidence , prediction intervals. however, i'm stuck on how format axes... i'd them in log scale. learned tutorial , have tried go through script transform axes, messes confidence intervals.
i've tried using:
scale_y_continuous(trans=log2_trans())+ scale_x_continuous(trans=log2_trans())
but doesn't transform 95% confidence interval... suggestions appreciated! basically, i'm looking easy way log scales nice on nice graph.
here's code (i didn't include data bit reference):
x <- c(6135.0613509,945.2650501,1927.8260200,110.0000000, 3812.9674276,3.2991626,1173.4923354,945.2650501, 114.2114798,11.2463797) y <- c(370.00,32.00,2900.00,52.00,1500.00,0.06,16.00,50.00,5.00,11.00) df <- data.frame(x, y) # log transformation log_x <- log(x) log_y <- log(y) # plot plot(log_x,log_y) # linear regression - linear model function model1 <- lm(log_y ~ log_x, data = df) summary(model1) abline(model1, col = "lightblue") # add trendline plot of log transformed data # load ggplot2 library(ggplot2) # confidence , prediction intervals temp_var <- predict(model1, interval = "prediction") ## warning in predict.lm(model1, interval = "prediction"): predictions on ## current data refer _future_ responses new_df <- cbind(df, temp_var) ggplot(new_df, aes(log_x, log_y))+ geom_point() + geom_line(aes(y = lwr), color = "red", linetype = "dashed")+ geom_line(aes(y = upr), color = "red", linetype = "dashed")+ geom_smooth(method = lm, se = true)
Comments
Post a Comment