0

I am working with the plot scripted below:


library(tidyverse)

stationary <- dat %>%
  dplyr::filter(predicted == "Stationary") %>%
  ggplot(aes(x = hour, y = Y, fill = data, group = data)) + 
  geom_col(position = position_dodge()) +  
  geom_errorbar(aes(ymin = YMIN, ymax = YMAX),
                position = position_dodge(0.9)) +
  scale_fill_manual(values = c("lgcanet" = "gray80", "snpnet" = "gray40" )) +
  labs(title = "a",
       x = "Hour of the day", 
       y = "Proportion",
       fill = "Behavioural\n category") +
  theme(axis.text=element_text(size=22),
        axis.title=element_text(size=22),
        legend.position = "none")  + 

  ylim(0, 0.9)+ theme(legend.position = "none")+  
  theme(plot.title = element_text(size=22))

stationary

My question is straighforward. When I visualize the dataset dat, I have no missing values. However, when I plot stationary, there's a missing bar in position 15 of the X axis.

I hope somebody is able to let me know the reason behind this? I suspect this is due to filter, but why does it only happen when plotting with "Stationary", and not with other categories like "Moving" in my dataset?

I hope there is a fix.

dputed dataset below:

> dput(dat)
structure(list(hour = c(0L, 0L, 0L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 
17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 
21L, 22L, 22L, 22L, 23L, 23L, 23L, 0L, 0L, 0L, 1L, 1L, 1L, 2L, 
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 
7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 
12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 
16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L, 
21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L), predicted = c("Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary", "Feeding", 
"Moving", "Stationary", "Feeding", "Moving", "Stationary"), Y = c(0.154681, 
0.674219792, 0.26018171, 0.123956169, 0.770734667, 0.196624589, 
0.100965488, 0.817486337, 0.150570688, 0.210191433, 0.279995373, 
0.522647236, 0.234739907, 0.139925966, 0.686217363, 0.24114481, 
0.184914944, 0.627808135, 0.234325872, 0.254871562, 0.546399513, 
0.248319131, 0.261282084, 0.508018619, 0.227005233, 0.251133647, 
0.549151992, 0.217179979, 0.226309486, 0.590163933, 0.225626231, 
0.228739084, 0.583517505, 0.219801659, 0.252196842, 0.554826957, 
0.213678598, 0.216436233, 0.592714024, 0.181927787, 0.176007486, 
0.673224042, 0.153096459, 0.103211711, 0.791985426, 0.097062644, 
0.076794171, 0.907589555, 0.173318656, 0.291811127, 0.55956284, 
0.114016498, 0.771948996, 0.18684528, 0.10592056, 0.758283747, 
0.196845864, 0.153399026, 0.637745394, 0.288967977, 0.154103192, 
0.545776799, 0.414282403, 0.17916971, 0.477163312, 0.428690807, 
0.188625532, 0.410643344, 0.460762284, 0.185673466, 0.556603437, 
0.360123553, 0.186468006, 0.619848556, 0.102946434, 0.117420501, 
0.710255217, 0.085296176, 0.122578887, 0.705326855, 0.082978251, 
0.282820082, 0.369538165, 0.225187701, 0.340835793, 0.290042658, 
0.254104054, 0.303040651, 0.361150303, 0.219680679, 0.281013065, 
0.38755169, 0.215007656, 0.267808505, 0.437847187, 0.188066206, 
0.256738674, 0.418523792, 0.191317998, 0.282095454, 0.378921649, 
0.192197946, 0.296221256, 0.343671498, 0.21204071, 0.264178641, 
0.37476084, 0.215286911, 0.285715162, 0.343315118, 0.225349727, 
0.274045769, 0.354524621, 0.233979875, 0.277640649, 0.2921432, 
0.289202238, 0.323114441, 0.221145203, 0.357155217, 0.240638977, 
0.470048865, 0.150795243, 0.146938436, 0.673705726, 0.071591529, 
0.143295364, 0.666298039, 0.079103593, 0.218088342, 0.568232731, 
0.110925072, 0.267305927, 0.482546431, 0.151432786, 0.306767296, 
0.476052652, 0.142276041, 0.264867497, 0.502361776, 0.148534296, 
0.238832836, 0.539938351, 0.14438792), YMIN = c(0.111622584, 
0.57558919, 0.178703401, 0.088143131, 0.687337528, 0.1217464, 
0.0733921, 0.755478814, 0.093856058, 0.1693268, 0.216235582, 
0.452992291, 0.189585149, 0.075280623, 0.621341306, 0.199077922, 
0.128682591, 0.559547066, 0.191776222, 0.188045367, 0.470199491, 
0.205963272, 0.201685928, 0.445531582, 0.186592457, 0.191004511, 
0.485288287, 0.18087768, 0.172783057, 0.524384284, 0.187255536, 
0.166292047, 0.511705373, 0.180626825, 0.196842022, 0.488278071, 
0.180383614, 0.170865092, 0.531781636, 0.147661097, 0.124588753, 
0.61272206, 0.119970955, 0.069121836, 0.738342094, 0.070756358, 
0.040137355, 0.861936686, 0.142587275, 0.228164929, 0.488514426, 
0.082217165, 0.689415552, 0.114563802, 0.077481332, 0.682770968, 
0.13755071, 0.113970229, 0.536293985, 0.20134563, 0.112932108, 
0.431542955, 0.318313417, 0.139101201, 0.377559378, 0.339132576, 
0.142921212, 0.308316469, 0.368743688, 0.132494556, 0.452613721, 
0.277533897, 0.128852501, 0.512838079, 0.068314237, 0.07729792, 
0.602868608, 0.048590748, 0.075040937, 0.604890563, 0.013469543, 
0.227039126, 0.290705926, 0.153093633, 0.278753614, 0.216026099, 
0.184689256, 0.233381948, 0.285383003, 0.165621149, 0.209569197, 
0.304152031, 0.150927274, 0.206967076, 0.356326729, 0.133252296, 
0.201372373, 0.337442795, 0.144078897, 0.222104493, 0.301436677, 
0.145942262, 0.234941302, 0.272937178, 0.165504544, 0.207677165, 
0.300390926, 0.159293156, 0.227882598, 0.281692727, 0.167766199, 
0.210148377, 0.278131075, 0.176383174, 0.215768541, 0.212191421, 
0.225708559, 0.252433463, 0.15739698, 0.299506967, 0.190150005, 
0.387794877, 0.114610831, 0.106533711, 0.58181318, 0.047868642, 
0.100032326, 0.580337524, 0.041469188, 0.149052706, 0.476381247, 
0.069468115, 0.196848657, 0.403074576, 0.100976953, 0.223306634, 
0.39000654, 0.0915594, 0.195878551, 0.409341697, 0.083305359, 
0.172234465, 0.439986563, 0.08055391), YMAX = c(0.197739415, 
0.772850394, 0.341660019, 0.159769208, 0.854131807, 0.271502778, 
0.128538875, 0.87949386, 0.207285318, 0.251056066, 0.343755165, 
0.592302181, 0.279894666, 0.204571309, 0.751093421, 0.283211698, 
0.241147296, 0.696069203, 0.276875523, 0.321697757, 0.622599535, 
0.29067499, 0.320878239, 0.570505655, 0.267418009, 0.311262784, 
0.613015698, 0.253482278, 0.279835915, 0.655943582, 0.263996927, 
0.291186121, 0.655329638, 0.258976493, 0.307551662, 0.621375843, 
0.246973583, 0.262007374, 0.653646412, 0.216194476, 0.22742622, 
0.733726024, 0.186221963, 0.137301586, 0.845628758, 0.12336893, 
0.113450987, 0.953242423, 0.204050036, 0.355457325, 0.630611254, 
0.145815831, 0.854482441, 0.259126758, 0.134359788, 0.833796525, 
0.256141017, 0.192827823, 0.739196803, 0.376590324, 0.195274276, 
0.660010642, 0.510251389, 0.219238218, 0.576767247, 0.518249038, 
0.234329851, 0.51297022, 0.552780881, 0.238852376, 0.660593152, 
0.442713209, 0.24408351, 0.726859034, 0.137578631, 0.157543082, 
0.817641826, 0.122001604, 0.170116836, 0.805763147, 0.15248696, 
0.338601039, 0.448370404, 0.297281769, 0.402917971, 0.364059216, 
0.323518852, 0.372699353, 0.436917603, 0.27374021, 0.352456934, 
0.47095135, 0.279088038, 0.328649933, 0.519367645, 0.242880117, 
0.312104975, 0.499604789, 0.238557099, 0.342086415, 0.456406621, 
0.23845363, 0.357501211, 0.414405819, 0.258576876, 0.320680116, 
0.449130755, 0.271280665, 0.343547727, 0.404937508, 0.282933255, 
0.337943161, 0.430918168, 0.291576576, 0.339512757, 0.372094979, 
0.352695917, 0.39379542, 0.284893427, 0.414803466, 0.29112795, 
0.552302853, 0.186979655, 0.187343161, 0.765598272, 0.095314417, 
0.186558401, 0.752258555, 0.116737998, 0.287123977, 0.660084216, 
0.152382029, 0.337763196, 0.562018286, 0.20188862, 0.390227957, 
0.562098763, 0.192992682, 0.333856444, 0.595381856, 0.213763233, 
0.305431207, 0.639890139, 0.20822193), data = c("lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", "lgcanet", 
"lgcanet", "lgcanet", "lgcanet", "lgcanet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", 
"snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet", "snpnet"
)), class = "data.frame", row.names = c(NA, -144L))

1 Answer 1

2

It's because of the setting your pass on ylim.

Because for x = 15 and data = lgcanet y is 0.90758956. So, when you pass ylim(0,0.9) ggplot do not plot anything that is out of this value and so it remove your column (it's like if you were doing filter(y >=0 & y <= 0.9) with dplyr before plotting).

When you plot stationary, you can notice the warning message in the console indicating that ggplot2 remove one column:

Warning messages:

1: Removed 1 rows containing missing values (geom_col).

2: Removed 1 rows containing missing values (geom_errorbar).

To fix it, you can plot it using ylim(0,1) as I did below:

dat %>% filter(predicted == "Stationary") %>%
  ggplot(aes(x = hour, y = Y, fill = data)) +
  geom_col(position = position_dodge())+
  geom_errorbar(aes(ymin = YMIN, ymax = YMAX),
                position = position_dodge(0.9))+
  scale_fill_manual(values = c("lgcanet" = "gray80", "snpnet" = "gray40" )) +
  labs(title = "a",
       x = "Hour of the day", 
       y = "Proportion",
       fill = "Behavioural\n category") +
  theme(axis.text=element_text(size=22),
        axis.title=element_text(size=22),
        legend.position = "none")  + 
  
  ylim(0, 1)+ theme(legend.position = "none")+  
  theme(plot.title = element_text(size=22))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.