user11418708
user11418708

Reputation: 902

Convert bar lines to continuous

I would like to use the geom_line() function to represent a continuous trend. How can update this plot have continuous lines instead of bar lines?

Below teunbrand solution that works for 50 cases but my data set consist of 675 cases - I don't understand why fails to plot continuous line

 df<-structure(list(index = 1:100, time = c("04:00", "04:15", "04:30", 
"04:45", "05:00", "05:15", "05:30", "05:45", "06:00", "06:15", 
"06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", 
"08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", 
"10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", 
"11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", 
"13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", 
"15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", 
"17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", 
"18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", 
"20:30", "20:45", "21:00", "21:15", "21:30", "21:45", "22:00", 
"22:15", "22:30", "22:45", "23:00", "23:15", "23:30", "23:45", 
"00:00", "00:15", "00:30", "00:45", "01:00", "01:15", "01:30", 
"01:45", "02:00", "02:15", "02:30", "02:45", "03:00", "03:15", 
"03:30", "03:45", "04:00", "04:15", "04:30", "04:45"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "value", class = "factor"), value = c(0, 
0.00138248847926267, 0.00138248847926267, 0.000460829493087558, 
0.00184331797235023, 0.00368663594470046, 0.00599078341013825, 
0.00737327188940092, 0.0184331797235023, 0.0207373271889401, 
0.0248847926267281, 0.0271889400921659, 0.0456221198156682, 0.047926267281106, 
0.0534562211981567, 0.056221198156682, 0.0783410138248848, 0.0806451612903226, 
0.0875576036866359, 0.0903225806451613, 0.105990783410138, 0.107834101382488, 
0.114746543778802, 0.114285714285714, 0.130414746543779, 0.134101382488479, 
0.138709677419355, 0.136866359447005, 0.148387096774194, 0.153456221198157, 
0.154377880184332, 0.151152073732719, 0.146543778801843, 0.144239631336406, 
0.145161290322581, 0.145161290322581, 0.140552995391705, 0.140092165898618, 
0.145622119815668, 0.146082949308756, 0.148847926267281, 0.150230414746544, 
0.150230414746544, 0.147004608294931, 0.145161290322581, 0.148387096774194, 
0.149769585253456, 0.150691244239631, 0.141935483870968, 0.141935483870968, 
0.135944700460829, 0.137327188940092, 0.132718894009217, 0.130875576036866, 
0.126728110599078, 0.12073732718894, 0.102764976958525, 0.102764976958525, 
0.100460829493088, 0.0990783410138249, 0.0857142857142857, 0.084331797235023, 
0.0838709677419355, 0.0838709677419355, 0.0723502304147465, 0.0705069124423963, 
0.0668202764976959, 0.0640552995391705, 0.0580645161290323, 0.0589861751152074, 
0.0557603686635945, 0.0548387096774194, 0.0451612903225806, 0.0470046082949309, 
0.0433179723502304, 0.0419354838709677, 0.0331797235023042, 0.0299539170506912, 
0.0271889400921659, 0.0253456221198157, 0.0188940092165899, 0.0179723502304147, 
0.0175115207373272, 0.0170506912442396, 0.0119815668202765, 0.0110599078341014, 
0.0101382488479263, 0.0101382488479263, 0.00829493087557604, 
0.00737327188940092, 0.00737327188940092, 0.00737327188940092, 
0.00829493087557604, 0.00829493087557604, 0.00829493087557604, 
0.00737327188940092, 0.00276497695852535, 0.00506912442396313, 
0.00645161290322581, 0.00737327188940092), time_2 = c("04:00", 
"04:15", "04:30", "04:45", "05:00", "05:15", "05:30", "05:45", 
"06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", 
"07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", 
"09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", 
"11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", 
"13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", 
"14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", 
"16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", 
"18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", 
"20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30", 
"21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", 
"23:30", "23:45", "00:00", "00:15", "00:30", "00:45", "01:00", 
"01:15", "01:30", "01:45", "02:00", "02:15", "02:30", "02:45", 
"03:00", "03:15", "03:30", "03:45", "04:00", "04:15", "04:30", 
"04:45")), row.names = c(NA, 100L), class = "data.frame")

Plot with 100 cases

Plot with 100 cases

Plot with 120 cases

Plot with 120 cases

Plot with 200 cases

Plot with 200 cases enter image description here

Upvotes: 0

Views: 41

Answers (1)

teunbrand
teunbrand

Reputation: 38023

The problem is likely that ggplot2 doesn't understand that your character time variable can be interpreted as time. To circumvent this, you'd have to convert it to a date.

library(lubridate)
library(ggplot2)

df$time_2 <- as_date(hm(df$time))

ggplot(df, aes(time_2, value)) +
  geom_line()

enter image description here

Small note: the time is converted to a POSIXct class which was 1970-01-01 00:00 as starting point, so your year will technically be in 1970, but that doesn't matter for the interpretation of the plot, as it won't be displayed.

Upvotes: 2

Related Questions