Home > OS >  Grouping when using geom_line in ggplot2
Grouping when using geom_line in ggplot2

Time:05-27

I am receiving this error - Error in check_aesthetics(): ## ! Aesthetics must be either length 1 or the same as the data (1): x and y in my code when I run R markdown.

Here is my code

{r, fig.show='hide'}
ggplot(data=hourlyIntensities_data)  
geom_point(mapping = aes(x = ActivityHour, y = AverageIntensity, group = 1))  
geom_line(mapping = aes(x = ActivityHour, y = AverageIntensity, group = 1))  
theme(axis.text.x = element_text(angle = 45))   
scale_x_discrete(limits = axisorder)  
ggtitle("Average Intensity user_6",
      subtitle = "4-12-2016")

here is my dput

structure(list(Id = c("user_6", "user_6", "user_6", "user_6", 
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", 
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", 
"user_6", "user_6", "user_6", "user_6", "user_6", "user_6"), 
ActivityHour = c("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", 
"4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", 
"10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", 
"3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", 
"9:00 PM", "10:00 PM", "11:00 PM"), TotalIntensity = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 28L, 13L, 13L, 143L, 20L, 11L, 19L, 
54L, 9L, 19L, 24L, 19L, 9L, 0L, 0L, 0L, 0L), AverageIntensity = c(0, 
0, 0, 0, 0, 0, 0, 0.466667, 0.216667, 0.216667, 2.383333, 
0.333333, 0.183333, 0.316667, 0.9, 0.15, 0.316667, 0.4, 0.316667, 
0.15, 0, 0, 0, 0)), row.names = c(NA, -24L), class = "data.frame")

when I remove group = 1 I no longer receive the error when printing HOWEVER I do not get a line to print on my graph.

CodePudding user response:

Put the aesthetics in the main call with ggplot() and leave geom_point() and geom_line() aesthetics empty:

ggplot(data=hourlyIntensities_data, aes(x = ActivityHour, y = AverageIntensity, group = 1 ))  
  geom_point()  
  geom_line() 
  theme(axis.text.x = element_text(angle = 45))   
  #scale_x_discrete(limits = axisorder)  
  ggtitle("Average Intensity user_6",
          subtitle = "4-12-2016")

enter image description here

CodePudding user response:

Turn your time variable into a date-time variable, instead of a character variable, first:

library(ggplot2)
hourlyIntensities_data <- structure(list(Id = c("user_6", "user_6", "user_6", "user_6", 
                      "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", 
                      "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", "user_6", 
                      "user_6", "user_6", "user_6", "user_6", "user_6", "user_6"), 
               ActivityHour = c("12:00 AM", "1:00 AM", "2:00 AM", "3:00 AM", 
                                "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", 
                                "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", 
                                "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", 
                                "9:00 PM", "10:00 PM", "11:00 PM"), TotalIntensity = c(0L, 
                                                                                       0L, 0L, 0L, 0L, 0L, 0L, 28L, 13L, 13L, 143L, 20L, 11L, 19L, 
                                                                                       54L, 9L, 19L, 24L, 19L, 9L, 0L, 0L, 0L, 0L), AverageIntensity = c(0, 
                                                                                                                                                         0, 0, 0, 0, 0, 0, 0.466667, 0.216667, 0.216667, 2.383333, 
                                                                                                                                                         0.333333, 0.183333, 0.316667, 0.9, 0.15, 0.316667, 0.4, 0.316667, 
                                                                                                                                                         0.15, 0, 0, 0, 0)), row.names = c(NA, -24L), class = "data.frame")
hourlyIntensities_data$ActivityHour <- as.POSIXct(hourlyIntensities_data$ActivityHour, 
                                                  format = "%I:%M %p")

ggplot(hourlyIntensities_data)  
  geom_point(mapping = aes(x = ActivityHour, y = AverageIntensity))  
  geom_line(mapping = aes(x = ActivityHour, y = AverageIntensity))  
  theme(axis.text.x = element_text(angle = 45))   
  scale_x_datetime(date_labels = "%I:%M %p")  
  ggtitle("Average Intensity user_6",
          subtitle = "4-12-2016")

Created on 2022-05-26 by the enter image description here

  • Related