Home > OS >  Axis titles are disappearing in ggplot2 when I try to manually set months
Axis titles are disappearing in ggplot2 when I try to manually set months

Time:07-24

I'm trying to use months as the x-axis for a ggplot graph, but I have multiple times in each day, and I need the times to be in chronological order. I've tried manually setting the breaks and labels to month numbers and names, but now the x-axis is just blank. What should I do?

month_nums <- seq(1, 12)
month_names <- month.abb

time_strs <- paste(moods$Month, moods$Day, moods$Year, moods$Hour, moods$Minute, sep=":")
timestamps <- as.POSIXct(time_strs, format = "%m:%d:%Y:%H:%M", tz="EST")

ggplot(data2, aes(x=timestamps))  
  geom_smooth(aes(y=anxious2, color="Anxiety"), size=1.2)  
  geom_smooth(aes(y=happy2, color="Happiness"), size=1.2)  
  geom_smooth(aes(y=social2, color="Sociability"), size=1.2)  
  scale_color_manual(name="Variables", 
                     values=c("red", 
                              "grey20",
                              "grey40"))   
  scale_x_continuous(name="Timestamps", breaks=month_nums, labels=month_names)  
  scale_y_continuous(name="Intensity")  
  ggtitle("Emotions")

# moods data
structure(list(Year = c("2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021"), 
Month = c("9", "9", "9", "9", "9", "10", "10", "10", "10", "10"), 
Day = c("29", "29", "30", "30", "30", "1", "1", "1", "1", "2"), 
Hour = c("16", "21", "7", "12", "16", "8", "12", "19", "22", "13"), 
Minute = c("24", "52", "58", "53", "18", "42", "24", "49", "18", "27"), 
happy = c("3", "4", "5", "3", "4", "5", "2", "3", "1", "1"), 
social = c("6", "5", "8", "8", "4", "10", "3", "2", "2", "2"), 
anxiety = c("-", "-", "1", "2", "1", "1", "2", "1", "1", "1")), 
row.names = 2:11, class = "data.frame")

CodePudding user response:

As timestamps is a datetime/POSIXct, you'll find scale_x_datetime much handier than scale_x_continuous. You can tweek the arguments to show what you prefer.

scale_x_date(date_breaks = "1 month", date_labels = "%b")

Output:

enter image description here

Data:

I needed to change your variable names of the provided data a little bit, add some (smaller) missing parts, as well as extending the range of the data to more points than 2 in order to have some output to show here.

moods <- structure(list(Year = c("2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021"), 
               Month = c("9", "7", "8", "9", "9", "10", "10", "11", "12", "12"), 
               Day = c("29", "29", "30", "30", "30", "1", "1", "1", "1", "2"), 
               Hour = c("16", "21", "7", "12", "16", "8", "12", "19", "22", "13"), 
               Minute = c("24", "52", "58", "53", "18", "42", "24", "49", "18", "27"), 
               happy = c("3", "4", "5", "3", "4", "5", "2", "3", "1", "1"), 
               social = c("6", "5", "8", "8", "4", "10", "3", "2", "2", "2"), 
               anxiety = c(NA, NA, "1", "2", "1", "1", "2", "1", "1", "1")), 
          row.names = 2:11, class = "data.frame") |> mutate(across(c(happy:anxiety), as.numeric))

data2 <- moods

data2$time_strs <- paste(moods$Month, moods$Day, moods$Year, moods$Hour, moods$Minute, sep=":")
data2$timestamps <- as.POSIXct(data2$time_strs, format = "%m:%d:%Y:%H:%M", tz="EST")

ggplot(data2, aes(x=timestamps))  
  geom_smooth(aes(y=anxiety, color="Anxiety"), size=1.2)  
  geom_smooth(aes(y=happy, color="Happiness"), size=1.2)  
  geom_smooth(aes(y=social, color="Sociability"), size=1.2)  
  scale_color_manual(name="Variables", 
                     values=c("red", 
                              "grey20",
                              "grey40"))   
  scale_x_datetime(date_breaks = "1 month", date_labels = "%b")  
  scale_y_continuous(name="Intensity")  
  ggtitle("Emotions")
  • Related