-1

Problem context: Scatter plot; Using ggplot2 with data from data frame; seeking to add a custom geom-line() [or geom_abline()] to include inside scatter plot confidence interval upper and lower bands on discrete data using linear regression model.

Error context: Message Error: Discrete value supplied to continuous scale

Yes, I know and understand that the data is discrete. Which is recommended for adding line to numeric data for show lines that permit fitted custom lines locations? Similar to what cars::scatterplot() does with quartiles line positions displayed, but I seek to add custom confidence interval upper and lower bands. For ggplot2 which is recommended: geom_line() or geom_segment() or geom_abline()?

ggplot(nt, aes(x=hr, y=temp)) + geom_point(aes(color=temp), size=3) + 
 scale_color_gradientn(colors = c("#00AFBB", "#E7B800", "#FC4E97")) +
    geom_line(data=nt, aes(x = hr, y = temp, color = 'black'))

sample data: for data frame 'nt' (correction: nt, normtemp, same names)

   temp
   <dbl>
   hr
   <int>
   1    96.3    70      
   2    96.7    71      
   3    96.9    74      
   4    97.0    80      
   5    97.1    73      
   6    97.1    75  
manager_matt
  • 395
  • 4
  • 19
  • Don't specify the color in the aesthetic for geom_line: `aes(x = hr, y = temp), color = 'black')` – Edward Mar 12 '20 at 06:50
  • But the problem concerns the `temp` variable (I think). I'm guessing it is discrete, but you're using `scale-color_gradient`, which expects a continuous variable. Maybe you want `scale_color_manual`? – Edward Mar 12 '20 at 06:53
  • And please include (always) a sample of the data (both `nt` and `normtemp`). Makes it easier to diagnose errors. :) – Edward Mar 12 '20 at 06:58
  • 1
    The 'sample data' you added is only one structure, but the code uses two: `nt` and `normtemp`? [Here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) is a excellent explanation how to add a minimal reproducible example. The MRE helps others to find and test an answer to your question! Also, none of the variables in your 'sample data' (again is this `nt` or `normtemp` ?) is discrete (they are both numberic). A bit of help would go a long way ;) – dario Mar 12 '20 at 08:08
  • Sorry, my mistake, nt is the variable data frame, not normtemp – manager_matt Mar 13 '20 at 03:36

1 Answers1

0

with plot(), rather than ggplot() + geom_point(), I did accomplish to get scatter plot confidence intervals plotted for upper and lower bands.

This was accomplished by using

ylimits <- c(min(fitted.pred[,"lwr"]), max(fitted.pred[,"upr"]))

and applying ylim=ylimits to the plot parameters:

plot(df, ylim=ylimits)

and adding the abline(model) for the model line for confidence intervals, and setting lines, multiple times for the different confidence interval levels, where xplot = data.frame:

lines(xplot[,], fitted.conf_int[,"lwr"])
manager_matt
  • 395
  • 4
  • 19