-1

I am taking an online R class in Udemy, here is the code for a Linear regression visualization. I got the error message from the last command. The data from the website about bikesharing: https://www.kaggle.com/c/bike-sharing-demand/data

The data looks like: enter image description here

bike <- read.csv("train.csv")
library(ggplot2)
bike$datetime <- as.POSIXct(bike$datetime)
pl <- ggplot(bike, aes(datetime, count)) + geom_point(aes(color = temp),alpha = 0.5)
pl
print(pl)
pl + scale_color_continuous(low='#55D8CE',high='#FF6E2E') +theme_bw()
cor(bike[,c('temp','count')])
ggplot(bike,aes(factor(season),count)) + geom_boxplot(aes(color=factor(season))) +theme_bw()
bike$hour <- sapply(bike$datetime,function(x){format(x,"%H")})
library(dplyr)
pl <- ggplot(filter(bike,workingday==1),aes(hour,count)) 
pl <- pl + geom_point()
pl
pl <- pl + scale_color_gradientn(colours = c('dark blue','blue','light blue','light green','yellow','orange','red'))

the pl looks like: enter image description here I got the error message from the last command when I was trying to add colors: Error in if (!alpha || all(lab_in[, 4] == 1)) { : missing value where TRUE/FALSE needed

I have no idea how to solve the problem, anyone can help? thank you so much.

sooo
  • 21
  • 7
  • There is a typo : you have `scale_color_gradientn` instead of `scale_color_gradient` but I think that doesn't solve either. Do you need `ggplot(filter(bike,workingday==1),aes(hour,count, color = factor(hour))) + geom_point()` ? – Ronak Shah Jan 04 '20 at 09:27
  • Thank you. yeah, I need that. – sooo Jan 04 '20 at 20:29
  • I just followed the class code. No idea about that. – sooo Jan 04 '20 at 20:32

2 Answers2

0

If you want to add different colors for different hours you can do

library(dplyr)
library(ggplot2)

ggplot(filter(bike,workingday==1),aes(hour,count, color = hour)) + geom_point()

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

I got the same error here. Changing the colour word with a space to hex code solved it for me, e.g. I used scale_colour_gradientn(colours=c("#000066","blue","#b0cceb","#6dcf72","yellow","orange","red"))

denis
  • 5,580
  • 1
  • 13
  • 40
Dee
  • 1