Home > Software engineering >  Adjust x-axis tickmarks in ggplot2
Adjust x-axis tickmarks in ggplot2

Time:11-20

In this geom_segment plot I would like to have a tickmark for every 1,000 ms on the x-axis. The code below works fine -- just except for the negative tickmark with the value -1000 on it. How can that value and the corresponding tickmark be removed?

enter image description here

My code so far:

library(dplyr)
library(stringr)
library(ggplot2)
df_3 %>%
  
  # mutate `Mutual_Gaze` to German labels:
  mutate(Mutual_Gaze = ifelse(Mutual_Gaze == "rechts_Mitte", "Center_Right",
                              ifelse(Mutual_Gaze == "rechts_links", "Left_Right", 
                                     "Center_Left"))) %>%
  
  # mutate `Mutual_Gaze` so it also contains `Subject` suffix:
  mutate(Mutual_Gaze = str_c(Mutual_Gaze,"_R", str_extract(Subject, "\\d$"))) %>%
  
  ### Group by:
  group_by(Mutual_Gaze) %>%
  
  # plot:
  ggplot(aes(x = Starttime_ms, xend = Endtime_ms,
             y = Mutual_Gaze, yend = Mutual_Gaze,
             color = Subject
  ))  
  geom_segment(size = 6)  
  labs(title = paste0(
    #"Annotation Density Plot\n", 
    df_3$File, " (Study 1)"),
       x ="Timeline"
  )  

  # determine number of x-axis tickmarks:
  scale_x_continuous(n.breaks = 35,
                     # limits ???                              # <--- 
                     limits = c(0, max(df_3$Endtime_ms)))  
  # rotate x-axis tickmark labels:
  theme(axis.text.x = element_text(angle = 90, hjust = 1))  
  #scale_y_continuous(n.breaks = 10)
  #theme_minimal()   
  theme(legend.position = "none")

Data:

df_3 <- structure(list(Subject = c("Rater_1", "Rater_1", "Rater_1", "Rater_1", 
"Rater_1", "Rater_1", "Rater_1", "Rater_1", "Rater_1", "Rater_1", 
"Rater_1", "Rater_1", "Rater_1", "Rater_1", "Rater_1", "Rater_1", 
"Rater_1", "Rater_1", "Rater_1", "Rater_1", "Rater_2", "Rater_2", 
"Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_2", 
"Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_2", 
"Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_2", 
"Rater_2", "Rater_2", "Rater_2", "Rater_2", "Rater_3", "Rater_3", 
"Rater_3", "Rater_3", "Rater_3", "Rater_3", "Rater_3", "Rater_3", 
"Rater_3", "Rater_3", "Rater_3", "Rater_3", "Rater_3", "Rater_3", 
"Rater_3", "Rater_3", "Rater_3", "Rater_3", "Rater_3", "Rater_3", 
"Rater_3", "Rater_4", "Rater_4", "Rater_4", "Rater_4", "Rater_4", 
"Rater_4", "Rater_4", "Rater_4", "Rater_4", "Rater_4", "Rater_4", 
"Rater_4", "Rater_4", "Rater_4", "Rater_4", "Rater_4", "Rater_4", 
"Rater_4", "Rater_4", "Rater_4", "Rater_5", "Rater_5", "Rater_5", 
"Rater_5", "Rater_5", "Rater_5", "Rater_5", "Rater_5", "Rater_5", 
"Rater_5", "Rater_5", "Rater_5", "Rater_5", "Rater_5", "Rater_5", 
"Rater_5", "Rater_5", "Rater_5", "Rater_5", "Rater_5", "Rater_5"
), Mutual_Gaze = c("rechts_Mitte", "rechts_links", "Mitte_links", 
"Mitte_links", "rechts_links", "Mitte_links", "rechts_links", 
"Mitte_links", "rechts_links", "rechts_links", "Mitte_links", 
"Mitte_links", "Mitte_links", "rechts_links", "Mitte_links", 
"Mitte_links", "rechts_links", "rechts_links", "rechts_links", 
"rechts_links", "rechts_Mitte", "rechts_links", "Mitte_links", 
"rechts_links", "Mitte_links", "rechts_links", "Mitte_links", 
"rechts_links", "rechts_links", "rechts_Mitte", "Mitte_links", 
"Mitte_links", "Mitte_links", "Mitte_links", "rechts_links", 
"rechts_links", "rechts_links", "rechts_links", "Mitte_links", 
"Mitte_links", "rechts_links", "rechts_links", "rechts_links", 
"rechts_links", "rechts_Mitte", "rechts_links", "Mitte_links", 
"rechts_links", "Mitte_links", "rechts_links", "Mitte_links", 
"rechts_links", "rechts_links", "Mitte_links", "Mitte_links", 
"Mitte_links", "rechts_links", "rechts_links", "rechts_links", 
"rechts_links", "Mitte_links", "Mitte_links", "rechts_links", 
"rechts_links", "rechts_links", "rechts_Mitte", "Mitte_links", 
"rechts_links", "Mitte_links", "rechts_links", "Mitte_links", 
"rechts_links", "rechts_links", "Mitte_links", "Mitte_links", 
"Mitte_links", "rechts_links", "rechts_links", "rechts_links", 
"Mitte_links", "Mitte_links", "rechts_links", "rechts_links", 
"rechts_links", "rechts_links", "rechts_Mitte", "rechts_Mitte", 
"rechts_links", "Mitte_links", "rechts_links", "Mitte_links", 
"rechts_links", "Mitte_links", "rechts_links", "rechts_links", 
"rechts_links", "Mitte_links", "Mitte_links", "Mitte_links", 
"rechts_links", "Mitte_links", "Mitte_links", "rechts_links", 
"rechts_links", "rechts_links", "rechts_links"), Starttime_ms = c(500, 
1520, 2313, 3139, 3767, 7354, 8993, 9566, 10702, 13180, 14144, 
17447, 19121, 19766, 24721, 25436, 26522, 30597, 31952, 34187, 
350, 1490, 2210, 3710, 7300, 8920, 9500, 10670, 13170, 13880, 
14120, 17400, 18120, 19080, 19610, 19790, 21465, 21650, 24610, 
25360, 26480, 30620, 31930, 34160, 550, 1521, 2256, 3717, 7362, 
8951, 9561, 10702, 13174, 14139, 17326, 19111, 19816, 21288, 
21447, 21634, 24564, 25349, 26505, 30610, 34194, 570, 2355, 3800, 
7423, 8999, 9614, 10799, 13352, 14244, 17526, 19110, 19953, 21269, 
21444, 24735, 25403, 26501, 30607, 31960, 34456, 7, 665, 1520, 
2316, 3705, 7335, 8958, 9631, 10728, 11325, 13179, 14139, 17492, 
19176, 19636, 24645, 25415, 30634, 30653, 31954, 34204), Endtime_ms = c(1350, 
1620, 2958, 3534, 4255, 8581, 9269, 10287, 11377, 13710, 14874, 
17843, 19418, 23861, 25029, 26136, 26627, 31289, 33115, 34690, 
1360, 1630, 3500, 4260, 8570, 9280, 10280, 11380, 13720, 13930, 
14880, 17770, 18190, 19400, 19660, 21350, 21565, 23870, 25000, 
26130, 26590, 31280, 33120, 34690, 1334, 1630, 3491, 4256, 8578, 
9272, 10318, 11372, 13717, 14847, 18134, 19411, 21221, 21344, 
21534, 23969, 25016, 26126, 26591, 33154, 34690, 1255, 3466, 
4191, 8533, 9239, 10276, 11360, 13683, 14814, 18119, 19414, 21228, 
21343, 23866, 25030, 26128, 26586, 31283, 33160, 34684, 665, 
1324, 1608, 3487, 4225, 8567, 9271, 10287, 11325, 11394, 13697, 
14845, 17738, 19396, 23900, 25030, 26072, 30653, 31250, 33119, 
34641), File = c("Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A", 
"Triad A", "Triad A", "Triad A", "Triad A", "Triad A", "Triad A"
)), class = "data.frame", row.names = c(NA, -106L))

CodePudding user response:

Try breaks = seq(0, max(df_3$Endtime_ms), by = 1000) instead of limits.

library(dplyr)
library(stringr)
library(ggplot2)

df_3 %>%
  # mutate `Mutual_Gaze` to German labels:
  mutate(Mutual_Gaze = ifelse(Mutual_Gaze == "rechts_Mitte", "Center_Right",
                              ifelse(Mutual_Gaze == "rechts_links", "Left_Right", 
                                     "Center_Left"))) %>%
  
  # mutate `Mutual_Gaze` so it also contains `Subject` suffix:
  mutate(Mutual_Gaze = str_c(Mutual_Gaze,"_R", str_extract(Subject, "\\d$"))) %>%
  
  ### Group by:
  group_by(Mutual_Gaze) %>%
  
  # plot:
  ggplot(aes(x = Starttime_ms, xend = Endtime_ms,
             y = Mutual_Gaze, yend = Mutual_Gaze,
             color = Subject
  ))  
  geom_segment(size = 6)  
  labs(title = paste0(
    #"Annotation Density Plot\n", 
    df_3$File, " (Study 1)"),
    x ="Timeline"
  )  
  
  # determine number of x-axis tickmarks:
  scale_x_continuous(breaks = seq(0, max(df_3$Endtime_ms), by = 1000))  
  # rotate x-axis tickmark labels:
  theme(axis.text.x = element_text(angle = 90, hjust = 1))  
  #scale_y_continuous(n.breaks = 10)
  #theme_minimal()   
  theme(legend.position = "none")
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.

Created on 2022-11-19 with enter image description here

  • Related