Rainfall runoff Plotting - r
My data frame is as follows,
Date,Precipitation,Observed,Simulated
1/1/1988,21.90,3.06,15.27
1/2/1988,12.34,6.70,17.46
1/3/1988,19.76,18.32,32.45
1/4/1988,5.54,98.20,46.67
1/5/1988,6.50,91.92,37.43
1/6/1988,11.04,38.12,20.94
1/7/1988,17.90,44.14,26.64
1/8/1988,23.76,41.07,118.2
1/9/1988,59.74,169.06,225.4
1/10/1988,51.58,371.91,325.3
1/11/1988,18.08,447.05,387.1
1/12/1988,2.92,295.40,329.6
1/13/1988,2.72,133.90,218.7
1/14/1988,0.00,95.82,128.6
1/15/1988,7.38,70.32,70.17
1/16/1988,3.50,36.21,38.42
1/17/1988,4.58,9.43,21.97
1/18/1988,7.84,35.74,12.7
1/19/1988,16.86,24.32,12.96
1/20/1988,5.00,33.90,18.56
1/21/1988,0.96,27.06,20.9
1/22/1988,15.54,23.20,42.36
1/23/1988,11.36,16.41,54.19
1/24/1988,1.06,23.94,48.3
1/25/1988,6.42,17.35,42.32
1/26/1988,0.00,14.91,33.91
1/27/1988,2.44,10.13,25.46
1/28/1988,2.00,14.33,16.39
1/29/1988,2.36,10.62,6.423
1/30/1988,4.20,18.65,6.175
1/31/1988,15.80,12.67,38.42
2/1/1988,0.46,15.50,52.92
2/2/1988,2.50,14.91,35.03
2/3/1988,1.24,11.36,22.25
2/4/1988,0.04,16.72,15.55
2/5/1988,3.02,22.83,11.84
2/6/1988,0.00,10.86,10.33
2/7/1988,2.72,5.43,9.352
2/8/1988,0.36,3.32,9.41
2/9/1988,4.92,2.25,8.246
2/10/1988,1.94,21.38,8.059
2/11/1988,1.68,66.37,8.936
2/12/1988,0.00,66.37,8.247
2/13/1988,14.04,14.91,8.071
2/14/1988,2.08,9.90,9.941
2/15/1988,5.64,7.29,10.41
2/16/1988,0.00,21.38,10.08
2/17/1988,0.00,15.21,9.821
2/18/1988,1.44,13.49,10.43
2/19/1988,5.66,8.32,10.66
2/20/1988,0.32,4.77,8.877
2/21/1988,0.96,3.32,7.622
2/22/1988,1.36,2.46,6.069
2/23/1988,0.00,1.94,6.852
2/24/1988,0.36,1.48,7.538
2/25/1988,1.96,1.23,7.785
2/26/1988,4.08,1.08,9.163
2/27/1988,0.00,0.94,10.55
2/28/1988,0.56,0.81,10.5
2/29/1988,0.00,0.81,10.34
3/1/1988,0.00,0.69,10.77
3/2/1988,0.08,0.58,11.25
3/3/1988,0.82,0.53,10.86
3/4/1988,7.48,0.49,7.274
3/5/1988,6.26,0.40,5.618
3/6/1988,17.26,0.49,6.707
3/7/1988,3.42,0.35,8.519
3/8/1988,0.64,0.28,9.339
3/9/1988,3.64,0.32,7.926
3/10/1988,2.00,0.64,6.804
3/11/1988,19.60,0.75,4.982
3/12/1988,0.72,0.69,1.149
3/13/1988,12.52,0.64,1.99
3/14/1988,12.58,0.69,4.057
3/15/1988,22.34,0.81,13.46
3/16/1988,23.44,18.65,20.58
3/17/1988,35.20,65.08,41.81
3/18/1988,10.24,78.59,54.7
3/19/1988,47.32,146.45,88.96
3/20/1988,10.50,172.28,111.9
3/21/1988,0.12,215.85,88.96
3/22/1988,0.00,86.61,62.25
3/23/1988,5.88,49.53,42.3
3/24/1988,4.34,32.56,27.56
3/25/1988,0.50,121.94,18.82
3/26/1988,2.00,88.11,12.78
3/27/1988,14.30,35.28,7.07
3/28/1988,4.76,55.83,6.714
3/29/1988,2.60,17.67,6.184
3/30/1988,2.80,52.92,6.394
3/31/1988,2.80,63.80,7.282
4/1/1988,4.12,34.36,6.681
4/2/1988,23.88,14.62,18.06
4/3/1988,40.14,21.74,63.34
4/4/1988,20.74,31.24,128
4/5/1988,14.72,196.87,148.2
4/6/1988,3.08,121.94,115.5
4/7/1988,8.58,52.92,76.39
4/8/1988,4.32,99.81,48.48
4/9/1988,14.54,31.68,29.99
4/10/1988,3.62,53.49,20.66
4/11/1988,2.20,112.29,17.43
4/12/1988,2.06,58.83,14.83
4/13/1988,1.84,33.00,14.34
4/14/1988,0.00,50.08,13.21
4/15/1988,4.32,25.87,11.88
4/16/1988,6.84,21.03,11.32
4/17/1988,1.74,130.16,10.49
4/18/1988,19.20,55.83,10.87
4/19/1988,8.32,42.08,13.14
4/20/1988,4.30,21.38,14.37
4/21/1988,7.96,116.62,14.34
4/22/1988,11.52,60.67,14.35
4/23/1988,33.82,59.44,107.3
4/24/1988,5.66,198.03,152.3
4/25/1988,18.32,247.39,131.8
4/26/1988,17.80,130.16,121
4/27/1988,2.68,219.52,85.55
4/28/1988,0.00,148.43,52.51
4/29/1988,5.42,98.20,33.55
4/30/1988,1.12,54.65,24.19
5/1/1988,2.90,30.81,19.78
5/2/1988,0.00,16.72,17.36
5/3/1988,5.28,10.62,15.42
5/4/1988,0.00,7.69,10.36
5/5/1988,0.00,9.43,8.719
5/6/1988,0.00,5.60,8.94
5/7/1988,0.16,4.46,7.924
5/8/1988,0.00,3.72,6.791
5/9/1988,0.32,3.19,4.776
5/10/1988,3.24,3.32,6.717
5/11/1988,0.08,6.89,8.701
5/12/1988,0.00,4.61,9.747
5/13/1988,0.00,4.01,8.976
5/14/1988,0.00,3.19,8.588
5/15/1988,0.00,2.04,10.46
5/16/1988,0.00,1.75,12.28
5/17/1988,0.00,1.84,12.8
5/18/1988,3.26,1.56,12.48
5/19/1988,0.00,1.39,11.55
5/20/1988,2.84,1.39,11.39
5/21/1988,14.68,1.48,11.79
5/22/1988,8.00,1.39,12.92
5/23/1988,0.24,1.31,13.17
5/24/1988,0.00,1.08,12.73
5/25/1988,0.12,1.23,12.26
5/26/1988,3.12,1.01,12.14
5/27/1988,0.00,0.94,12.18
5/28/1988,0.00,1.23,12.56
5/29/1988,0.48,1.16,12.86
5/30/1988,0.00,0.94,11.86
5/31/1988,0.00,0.94,11.16
6/1/1988,2.72,0.94,11.3
6/2/1988,0.00,0.88,12.22
6/3/1988,0.92,0.81,13.16
6/4/1988,0.00,0.81,13.31
6/5/1988,0.00,0.69,13.42
6/6/1988,0.00,0.69,13.69
6/7/1988,0.00,0.64,13.95
6/8/1988,0.00,0.64,14.15
6/9/1988,0.00,0.58,14.18
6/10/1988,0.00,0.58,13.17
6/11/1988,0.00,0.58,12.07
6/12/1988,0.00,0.53,11.68
6/13/1988,0.00,0.49,10.58
6/14/1988,0.00,0.40,9.865
6/15/1988,0.00,0.40,9.251
6/16/1988,0.00,0.32,9.095
6/17/1988,0.00,0.32,9.796
6/18/1988,0.00,0.28,11.09
6/19/1988,0.00,0.25,12.24
6/20/1988,0.00,0.25,12.51
6/21/1988,0.00,0.25,12.83
6/22/1988,0.00,0.25,12.24
6/23/1988,0.00,0.25,11.73
6/24/1988,0.00,0.18,11.34
6/25/1988,0.00,0.18,11.88
6/26/1988,0.00,0.18,12.57
6/27/1988,0.00,0.16,13.2
6/28/1988,5.50,0.16,12.72
6/29/1988,0.00,0.13,11.74
6/30/1988,0.00,0.13,11.08
7/1/1988,0.00,0.32,11.06
7/2/1988,0.00,0.28,11.23
7/3/1988,0.00,0.28,11.97
7/4/1988,0.00,0.25,12.37
7/5/1988,0.00,0.25,13
7/6/1988,0.00,0.21,13.21
7/7/1988,0.00,0.18,13.29
7/8/1988,0.00,0.13,13.41
7/9/1988,0.00,0.13,13.49
7/10/1988,0.00,0.13,13.33
7/11/1988,0.00,0.13,13.13
7/12/1988,0.00,0.13,11.59
7/13/1988,0.00,0.11,10.34
7/14/1988,0.00,0.11,9.258
7/15/1988,0.00,0.09,9.584
7/16/1988,0.00,0.09,10.67
7/17/1988,0.00,0.09,11.51
7/18/1988,0.00,0.09,11.22
7/19/1988,0.00,0.13,10.27
7/20/1988,0.00,0.18,6.438
7/21/1988,0.00,0.28,2.296
7/22/1988,0.00,0.25,3.29
7/23/1988,0.00,0.32,5.341
7/24/1988,0.00,0.40,7.088
7/25/1988,0.00,0.35,7.95
7/26/1988,0.00,0.32,9.277
7/27/1988,0.00,0.32,10.6
7/28/1988,0.00,0.25,10.83
7/29/1988,0.00,0.25,10.31
7/30/1988,0.00,0.25,8.075
7/31/1988,0.00,0.18,7.727
8/1/1988,0.00,0.21,7.823
8/2/1988,0.00,0.21,8.826
8/3/1988,0.00,0.18,9.881
8/4/1988,0.00,0.18,9.957
8/5/1988,0.00,0.18,10.3
8/6/1988,0.00,0.18,9.91
8/7/1988,2.00,0.18,10.55
8/8/1988,2.64,0.18,10.41
8/9/1988,2.60,0.18,10.79
8/10/1988,0.00,0.18,9.982
8/11/1988,0.00,0.58,9.223
8/12/1988,0.00,0.64,7.946
8/13/1988,0.00,0.40,2.514
8/14/1988,0.00,0.32,4.496
8/15/1988,0.00,0.69,6.865
8/16/1988,0.00,0.81,9.129
8/17/1988,0.00,0.58,10.58
8/18/1988,0.00,0.32,11.36
8/19/1988,0.00,0.53,11.24
8/20/1988,0.00,0.32,11.25
8/21/1988,3.00,0.32,10.33
8/22/1988,0.00,0.25,9.991
8/23/1988,0.00,0.25,10.26
8/24/1988,0.00,0.21,9.69
8/25/1988,0.00,0.18,10.23
8/26/1988,1.20,0.18,9.806
8/27/1988,0.40,0.18,10.48
8/28/1988,18.32,0.16,11.04
8/29/1988,0.00,0.13,10.73
8/30/1988,0.00,0.13,10.2
8/31/1988,0.00,0.44,8.738
9/1/1988,0.00,0.58,7.971
9/2/1988,0.00,0.49,6.974
9/3/1988,0.00,0.32,7.245
9/4/1988,0.00,0.25,6.757
9/5/1988,0.00,0.18,5.911
9/6/1988,0.00,0.16,7.438
9/7/1988,0.00,0.13,7.892
9/8/1988,1.74,0.13,8.176
9/9/1988,0.00,0.09,7.239
9/10/1988,0.32,0.09,8.05
9/11/1988,0.00,0.09,8.973
9/12/1988,0.00,0.06,9.029
9/13/1988,0.00,0.04,7.839
9/14/1988,0.00,0.03,8.298
9/15/1988,0.00,0.03,9.274
9/16/1988,0.00,0.03,9.598
9/17/1988,0.20,0.01,9.834
9/18/1988,0.00,0.01,8.965
9/19/1988,0.00,0.01,7.586
9/20/1988,0.00,0.00,6.39
9/21/1988,0.00,0.00,6.086
9/22/1988,0.00,0.01,5.242
9/23/1988,0.00,0.01,5.468
9/24/1988,0.00,0.00,7.112
9/25/1988,0.00,0.00,8.564
9/26/1988,0.00,0.01,8.098
9/27/1988,0.00,0.01,7.438
9/28/1988,0.00,0.01,5.027
9/29/1988,0.00,0.01,3.847
9/30/1988,0.00,0.00,3.736
10/1/1988,19.98,0.00,4.882
10/2/1988,1.44,0.00,5.67
10/3/1988,4.76,0.00,5.89
10/4/1988,0.00,0.00,5.984
10/5/1988,0.00,0.01,4.795
10/6/1988,0.46,0.09,1.18
10/7/1988,0.16,0.16,1.71
10/8/1988,2.92,0.09,2.805
10/9/1988,0.00,0.06,3.646
10/10/1988,0.00,0.13,3.978
10/11/1988,3.02,0.11,3.825
10/12/1988,12.22,0.09,4.28
10/13/1988,5.72,0.13,5.557
10/14/1988,0.00,0.13,5.941
10/15/1988,0.00,0.09,6.157
10/16/1988,0.00,0.09,6.48
10/17/1988,2.12,0.16,7.368
10/18/1988,0.00,0.40,7.508
10/19/1988,0.00,0.28,7.449
10/20/1988,0.00,0.21,6.893
10/21/1988,1.74,0.18,7.525
10/22/1988,0.00,0.09,8.059
10/23/1988,0.00,0.09,6.449
10/24/1988,5.74,0.09,2.135
10/25/1988,0.08,0.06,2.628
10/26/1988,8.02,0.04,3.698
10/27/1988,1.08,0.03,3.943
10/28/1988,0.00,0.02,3.831
10/29/1988,2.66,0.01,4.499
10/30/1988,1.66,0.01,5.826
10/31/1988,2.88,0.09,5.149
11/1/1988,4.52,0.03,4.765
11/2/1988,2.92,0.01,4.237
11/3/1988,1.66,0.00,5.34
11/4/1988,1.40,0.00,5.443
11/5/1988,2.44,0.00,2.925
11/6/1988,2.76,0.00,2.2
11/7/1988,4.86,0.00,0.872
11/8/1988,6.00,0.03,1.637
11/9/1988,10.34,0.06,0
11/10/1988,4.36,0.06,0.4189
11/11/1988,2.00,0.81,0.7399
11/12/1988,0.58,4.46,0.0379
11/13/1988,3.72,3.59,0
11/14/1988,0.00,2.04,0.7838
11/15/1988,0.00,1.39,2.604
11/16/1988,0.00,1.94,3.007
11/17/1988,2.56,1.39,3.594
11/18/1988,6.48,0.69,3.388
11/19/1988,0.48,0.69,3.61
11/20/1988,6.92,0.40,4.088
11/21/1988,9.48,0.58,4.012
11/22/1988,2.50,0.49,2.636
11/23/1988,1.92,1.94,4.444
11/24/1988,3.78,3.45,5.406
11/25/1988,0.04,1.56,6.103
11/26/1988,1.52,1.56,6.55
11/27/1988,2.04,1.39,6.061
11/28/1988,0.00,2.46,6.712
11/29/1988,2.32,1.56,7.358
11/30/1988,0.00,1.08,7.037
12/1/1988,0.00,0.88,7.034
12/2/1988,1.04,0.64,5.991
12/3/1988,9.80,0.49,6.684
12/4/1988,0.00,0.40,7.438
12/5/1988,1.26,0.32,7.088
12/6/1988,1.00,0.28,6.931
12/7/1988,2.70,0.25,5.799
12/8/1988,0.76,0.21,3.556
12/9/1988,3.12,0.09,2.857
12/10/1988,0.04,0.09,2.951
12/11/1988,4.00,0.13,3.475
12/12/1988,0.40,0.35,4.755
12/13/1988,0.00,0.35,6.497
12/14/1988,2.36,0.64,6.792
12/15/1988,0.00,0.69,5.049
12/16/1988,1.88,0.49,3.227
12/17/1988,0.64,0.40,3.096
12/18/1988,1.56,0.28,3.878
12/19/1988,2.32,0.25,5.608
12/20/1988,3.18,0.25,6.537
12/21/1988,3.84,0.32,7.767
12/22/1988,1.86,0.32,8.419
12/23/1988,3.28,0.28,8.728
12/24/1988,14.22,1.48,8.467
12/25/1988,4.64,1.48,5.57
12/26/1988,3.20,1.39,3.291
12/27/1988,0.00,23.57,0.897
12/28/1988,4.92,22.46,1.262
12/29/1988,2.02,5.26,1.976
12/30/1988,0.00,43.62,2.227
12/31/1988,0.00,55.83,3.271
I would like top produce a plot like the following using ggplot2
I did it with excel but I would like to plot it using ggplot2 so as I can customize most of stuffs.
The entire dataset for the plot is available in the following link.
https://drive.google.com/file/d/10VXqgHyDOp7V4Ui5qQJD-f2V-7OcRA-e/view?usp=sharing
Help please.
There's some solutions in the following link:
https://rpubs.com/cxiao/hydrograph-ggplot2-plot
I personally use mine
library(ggplot2)
library(data.table)
library(lubridate)
library(tidyverse)
# store the data in csv
data <- fread('data.csv')
data <- melt(data, measure.vars = c('Observed', 'Simulated'), value.name = 'Streamflow') %>%
.[,Date := fast_strptime(Date, '%m/%d/%Y') %>% as_date()]
summary(data)
# Calculate the range needed to avoid having your hyetograph and hydrograph overlap
maxRange <- 600 # set how wide of the first axis (streamflow)
coeff <- 0.4 # set the shrink coeffcient of Precipitation
# Plot the data
ggplot(data = data, aes(x = Date)) +
# Use geom_tile to create the inverted hyetograph
# y = the center point of each bar
# maxRange - Precipitation/coeff/2
geom_tile(aes(y = maxRange - Precipitation/coeff/2,
height = Precipitation/coeff,
fill = 'PColor')
)+
# Plot your discharge data
geom_line(aes(y = Streamflow,
color = variable),
alpha = 0.8,
size = 0.7) +
# Create a second axis with sec_axis() and format the labels to display the original precipitation units.
scale_y_continuous(name = "Streamflow ()",
limit = c(0, maxRange),
expand = c(0, 0),
sec.axis = sec_axis(trans = ~(.-maxRange)*coeff,
name = "Precipitation (mm/d)"))+
scale_fill_manual(values = c('PColor' = "#386cb0"),
labels = c('PColor' = 'Precipitation'),
name = NULL
)+
scale_color_manual(values = c('black', '#e41a1c'),
name = NULL)+
theme_bw()+
guides(color = guide_legend(nrow = 1)) +
theme(
# legend.position = c(0.75, 0.5),
legend.position = 'top',
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
Related
Hide some legend entries in ggplot
I have the follow lines of code: ggplot() + geom_line(data=TS_SimHeads_HOBS_final, aes(x=as.Date(Date), y=BH2672), color='red') + geom_point(data=Hydro_dates_wellData_2014_2018, aes(x=as.Date(Date), y=BH2672), color='red') + geom_line(data=TS_SimHeads_HOBS_final, aes(x=as.Date(Date), y=BH3025), color='green') + geom_point(data=Hydro_dates_wellData_2014_2018, aes(x=as.Date(Date), y=BH3025), color='green') + xlab("Date") + ylab("Head") #theme_bw() which generate the following plot: What I am trying to do, unsuccessfully, is to include legends only for the lines (points are the experimental data and lines the simulated ones). Some data for reproduction purposes: Date BH2672 BH278 BH2978 BH2987 BH3025 BH312 BH3963 BH3962 BH3957 2014-02-19 31.28400 78.86755 5.671027 39.48419 53.60201 44.29516 69.23685 61.70843 56.13871 2014-02-20 30.76656 78.87344 5.656940 39.49012 53.56489 44.50679 69.50910 61.70638 56.09621 2014-02-21 30.43226 78.88097 5.642136 39.49902 53.56041 44.65761 69.65709 61.70126 56.04346 2014-02-22 30.16532 78.88979 5.643818 39.51101 53.56065 44.78333 69.75621 61.69643 55.99459 2014-02-23 29.93577 78.89954 5.650873 39.52544 53.55970 44.89429 69.82983 61.69332 55.95241 2014-02-24 29.73162 78.90991 5.658991 39.54147 53.55682 44.99520 69.88845 61.69236 55.91639
As is quite often the case you first have to convert both of your datasets to long or tidy format using e.g. tidyr::pivot_longer which will result in a new column with the variable names as categories which could then be mapped on the color aes. Doing so will automatically create a legend and also allows to simplify your code. And if you want only the lines to appear in the legend then you could add show.legend=FALSE to geom_point. Finally you can set your desired colors via scale_color_manual. As you provided only one dataset I used this for both datasets which however shouldn't matter. Also, to make my life a bit easier I have put the datasets in an named list: library(dplyr, warn = FALSE) library(tidyr) library(ggplot2) data_list <- list(data = Hydro_dates_wellData_2014_2018, sim = TS_SimHeads_HOBS_final) %>% lapply(function(x) { x %>% select(Date, BH2672, BH3025) %>% mutate(Date = as.Date(Date)) %>% tidyr::pivot_longer(-Date) }) ggplot() + geom_line(data=data_list$sim, aes(x=Date, y=value, color = name)) + geom_point(data=data_list$data, aes(x=Date, y=value, color = name), show.legend = FALSE) + scale_color_manual(values = c(BH2672 = "red", BH3025 = "green")) + labs(x = "Date", y = "Head") DATA TS_SimHeads_HOBS_final <- structure(list(Date = c( "2014-02-19", "2014-02-20", "2014-02-21", "2014-02-22", "2014-02-23", "2014-02-24" ), BH2672 = c( 31.284, 30.76656, 30.43226, 30.16532, 29.93577, 29.73162 ), BH278 = c( 78.86755, 78.87344, 78.88097, 78.88979, 78.89954, 78.90991 ), BH2978 = c( 5.671027, 5.65694, 5.642136, 5.643818, 5.650873, 5.658991 ), BH2987 = c( 39.48419, 39.49012, 39.49902, 39.51101, 39.52544, 39.54147 ), BH3025 = c( 53.60201, 53.56489, 53.56041, 53.56065, 53.5597, 53.55682 ), BH312 = c( 44.29516, 44.50679, 44.65761, 44.78333, 44.89429, 44.9952 ), BH3963 = c( 69.23685, 69.5091, 69.65709, 69.75621, 69.82983, 69.88845 ), BH3962 = c( 61.70843, 61.70638, 61.70126, 61.69643, 61.69332, 61.69236 ), BH3957 = c( 56.13871, 56.09621, 56.04346, 55.99459, 55.95241, 55.91639 )), class = "data.frame", row.names = c( NA, -6L )) Hydro_dates_wellData_2014_2018 <- TS_SimHeads_HOBS_final
loess() doesn't smooth subsequently but over pooled data
I have time-series of 2d obsverations that I'm trying to smooth to take out some of the observation variability. I've been applying loess(), but just noticed it doesn't seem to smooth as a function of time but just across the entire pooled coordinates. Am I missing something? Is there a different function I should be using? df<-structure(list(timestamp = structure(c(1586488380, 1586488440, 1586488560, 1586488620, 1586488680, 1586488740, 1586488800, 1586488860, 1586489520, 1586489580, 1586489700, 1586489820, 1586489880, 1586489940, 1586490000, 1586490060, 1586490120, 1586490180, 1586490240, 1586490300, 1586490360, 1586490420, 1586490480, 1586490540, 1586490600, 1586490660, 1586490720, 1586490780, 1586490840, 1586490900, 1586490960, 1586491020, 1586491200, 1586491260, 1586491320, 1586491380, 1586491440, 1586491500, 1586491560, 1586491620, 1586491680, 1586491740, 1586491800, 1586491860, 1586491920, 1586491980, 1586492040, 1586492100, 1586492160, 1586492220, 1586492280, 1586492340, 1586492400, 1586492460, 1586492520, 1586492580, 1586492640, 1586492700, 1586492760, 1586492820, 1586492880, 1586492940, 1586493000, 1586493060, 1586493120, 1586493180, 1586493240, 1586493300, 1586493360, 1586493420, 1586493480, 1586493540, 1586493600, 1586493660, 1586493720, 1586493780, 1586493840, 1586493900, 1586493960, 1586494020, 1586494200, 1586494260, 1586494320, 1586494380, 1586494440, 1586494500, 1586494560, 1586494620, 1586494680, 1586494740, 1586494800, 1586494860, 1586494920, 1586494980, 1586495040, 1586495100, 1586495160, 1586495220, 1586495280, 1586495340, 1586495400, 1586495460, 1586495520, 1586495580, 1586495640, 1586495700, 1586495760, 1586495820, 1586495880, 1586495940, 1586496000, 1586496060, 1586496120, 1586496180, 1586496240, 1586496300, 1586496360, 1586496420, 1586496480, 1586496540, 1586496600, 1586496660, 1586496720, 1586496780, 1586496840, 1586496900, 1586496960, 1586497020, 1586497080, 1586497140, 1586497200, 1586497260, 1586497320, 1586497380, 1586497440, 1586497500, 1586497560, 1586497620, 1586497680, 1586497740, 1586497800, 1586497860, 1586497920, 1586497980, 1586498040, 1586498100, 1586498160, 1586498220, 1586498280, 1586498340), class = c("POSIXct", "POSIXt"), tzone = "UTC"), easting = c(740.582355718548, 740.582355718548, 739.726374785548, 739.611045841548, 739.508690311548, 739.398269506548, 739.278804356548, 739.627760514548, 737.913640733548, 738.088450601548, 738.551491861548, 738.957133488548, 739.137345557548, 739.304664573548, 739.460440784548, 739.605842807548, 739.741887116548, 739.719077482548, 739.369420509548, 738.973489249548, 738.521335985548, 739.279305656548, 739.993757669548, 740.085239162548, 740.172262825548, 740.255157063548, 740.334219013548, 740.409718260548, 740.481900024548, 740.550987811548, 740.573883125548, 740.252267406548, 739.261723439548, 738.935233921548, 738.774921432548, 738.615895069548, 738.451107559548, 738.280235586548, 738.493740162548, 738.717501067548, 738.925752666548, 739.120074872548, 739.301840012548, 739.472245999548, 739.632343117548, 739.890965132548, 740.098495936548, 740.293354354548, 740.476683179548, 740.649491986548, 740.678160672548, 740.443560695548, 740.398855065548, 740.451032168548, 740.467918582548, 740.160041067548, 739.819912921548, 739.641686751548, 739.457020461548, 739.265544595548, 739.193281488548, 739.227252654548, 738.995761471548, 738.822890745548, 738.775446949548, 738.726816309548, 738.676941805548, 738.625762928548, 739.254178659548, 739.727445331548, 740.019566884548, 740.129316037548, 740.255273807548, 740.442527947548, 740.615257127548, 740.775140223548, 740.923607252548, 741.055065964548, 741.139279130548, 740.907234314548, 739.290829342548, 739.115359714548, 740.664354207548, 740.589899151548, 740.578913554548, 740.571708783548, 740.568311076548, 740.568740893548, 740.527920123548, 740.358565457548, 740.392277156548, 740.370981239548, 740.289917518548, 740.214089159548, 740.192635592548, 740.176850935548, 740.160315351548, 740.143068630548, 740.103174446548, 740.008327647548, 740.058271768548, 740.205384482548, 740.211048172548, 740.150159818548, 740.122028309548, 740.230164637548, 740.271076846548, 740.075087486548, 739.768752873548, 739.586722485548, 739.940259334548, 740.233576255548, 740.473614136548, 740.495703912548, 740.341935547548, 740.186820856548, 740.204435025548, 740.299218490548, 740.318343269548, 740.238895133548, 739.999671854548, 740.062183564548, 740.196345466548, 740.329697802548, 740.418193609548, 740.311257937548, 740.270203214548, 740.209679752548, 740.146590442548, 740.079785501548, 740.170176300548, 740.268945921548, 740.217498771548, 740.133923060548, 740.117921377548, 740.177771453548, 740.140658663548, 740.080204534548, 740.108449333548, 740.145621912548, 740.182429420548, 740.010376475548, 739.819150336548, 739.616854492548, 739.369690457548, 739.104183601548, 738.938020260548, 738.843359187548, 738.802644324548, 738.761524527548 ), northing = c(2307.15134120986, 2307.15134120986, 2307.60836846986, 2307.72110371186, 2307.83015111886, 2307.94605545486, 2308.06963872386, 2307.94323643186, 2308.11539257586, 2307.98516105286, 2307.67209087786, 2307.39795736686, 2307.27544716286, 2307.16124100486, 2307.05447137086, 2306.95438746086, 2306.86033624586, 2306.85049579286, 2307.02449397686, 2307.22230535086, 2307.44905018086, 2306.99878407786, 2306.57790074586, 2306.51052483586, 2306.44607531386, 2306.38433869586, 2306.32512182186, 2306.26824947086, 2306.21356227886, 2306.16091500786, 2306.12077142386, 2306.17964098286, 2306.35042514386, 2306.42512419786, 2306.46180283886, 2306.49818722086, 2306.53588972286, 2306.57498431686, 2306.47587680786, 2306.37665531786, 2306.28431106486, 2306.19814347186, 2306.11754401386, 2306.04198150286, 2305.97099021786, 2305.88981694586, 2305.83737042086, 2305.78847203986, 2305.74280289286, 2305.70008126986, 2305.72436278986, 2305.79211047386, 2305.75001607586, 2305.66373079386, 2305.59454921786, 2305.66723079486, 2305.74826733386, 2305.75631243686, 2305.76412484986, 2305.77169383886, 2305.76318962486, 2305.74988901286, 2305.84052390686, 2305.91140100786, 2305.95786180686, 2306.00518488486, 2306.05340722986, 2306.10256811686, 2305.87083530186, 2305.67745118186, 2305.55808577486, 2305.51324056486, 2305.46177226186, 2305.38525730986, 2305.31467748186, 2305.24934676386, 2305.18868082786, 2305.13496475486, 2305.08628958686, 2305.07937405386, 2305.33244795286, 2305.26640414086, 2304.97847050686, 2304.88865807586, 2304.78121096686, 2304.67333694586, 2304.56517670086, 2304.45687199986, 2304.26299422086, 2303.81398617786, 2303.81772073686, 2303.82576399386, 2303.82937144386, 2303.85166918186, 2303.85679798586, 2303.85841996086, 2303.85934299186, 2303.85962745886, 2303.81418344986, 2303.71268455886, 2303.70396413486, 2303.71187848686, 2303.71241867586, 2303.72409686386, 2303.75626565486, 2303.81432053886, 2303.80604508386, 2303.57280991386, 2303.21896587386, 2303.06912105986, 2303.28351126486, 2303.45378473786, 2303.49375232686, 2303.50460433986, 2303.48302188886, 2303.48174695086, 2303.52692291386, 2303.61686633486, 2303.41139580686, 2303.32679365886, 2303.06984393086, 2303.15017783486, 2303.29293566086, 2303.44528532286, 2303.48203523086, 2302.89274879786, 2302.81440275486, 2302.73512963586, 2302.65410710886, 2302.56757728186, 2302.77320543086, 2303.00846800486, 2303.01932301786, 2302.97477011386, 2303.03742546386, 2303.13970331386, 2303.07628123386, 2302.97297111586, 2303.02123867886, 2303.08476293486, 2303.14766331986, 2303.17026860886, 2303.18249014186, 2303.19025912386, 2303.15218828686, 2303.11064943486, 2302.98849464786, 2302.87632040886, 2302.82807292386, 2302.77934558786)), row.names = 5905:6054, class = "data.frame") df.fitted<-loess(northing ~ easting, span = .5, data = df) df$northing.fitted<-df.fitted$fitted ggplot(df, aes(x=easting,y=northing)) + geom_path(color='orangered2') + geom_point(aes(y=northing.fitted)) So, instead of smoothing the "cluster", I'd like to use a rolling average smoothing each x/y pair as a function of time.
You need to regress both easting and northing as functions of time to get smoother x, y values: df$numtime <- as.numeric(df$timestamp) df.fitted.northing <-loess(northing ~ numtime, span = .5, data = df) df.fitted.easting <- loess(easting ~ numtime, span = .5, data = df) newdat <- data.frame(numtime = seq(min(df$numtime), max(df$numtime), len = 1000)) newdat$northing <- predict(df.fitted.northing, newdat) newdat$easting <- predict(df.fitted.easting, newdat) ggplot(df, aes(easting, northing)) + geom_path(aes(color = "original path"), alpha = 0.6, size = 0.5, arrow = arrow(length = unit(0.1, "inches"))) + geom_point(aes(color = "original path"), alpha = 0.6, size = 1) + geom_path(data = newdat, size = 1, aes(color = "smoothed"), arrow = arrow(length = unit(0.1, "inches"))) + coord_equal() + theme_light() + scale_color_manual(values = c("original path" = "orangered2", "smoothed" = "deepskyblue4"), name = "")
Is there a way to split a correlation matrix to only display a certain section of it (R)?
I have a dataset (randomized data, for this example) that I'm working with, and I need to only display a certain part of the matrix. Is there a way to keep the same parameters specified in the code, but to only display the part of the matrix that I need? The code that I will show below generates the matrix shown here: I like the size of everything, particularly speaking of the Pearson's correlation value legend on the right. However, I need to only display everything that is in the red box in the following picture: Here is the code that I'm working with: Data: sen <- structure(list(STAGE3 = c(0.876917687075825, 0.412349699244156, 0.0276263864194565, 0.715878164202211, 0.113324379019157, 0.697977125373629, 0.495239700082692, 0.433931430983351, 0.969730865766935, 0.325140127448327, 0.571408271249494, 0.795998354603895, 0.677190885671071, 0.725950370073618, 0.773033245228681, 0.124492827494149, 0.392125485039593, 0.293970448598711, 0.574176683397314, 0.439175490268417, 0.187274945963726, 0.114361533064052, 0.984810340190489, 0.094824786617737, 0.365076362766766, 0.484529293489265, 0.0248352738577235, 0.370080530701056, 0.916058724203794, 0.698768788065752, 0.0486805863644058, 0.417384720565167, 0.575510045429799, 0.306442633779882, 0.352619466947156, 0.0468312121293604, 0.719674300918072, 0.245979619378491, 0.497903453826379, 0.815929672015352, 0.579266045428163, 0.493374660814721, 0.981395698013249, 0.501428573306618, 0.232710091203988, 0.295487027432436, 0.0358089940602796, 0.122221799715662, 0.475075779687791, 0.64716894430911, 0.411728227914661, 0.189801935164384, 0.995784139092795, 0.338669053292779, 0.888464605534105, 0.727167494736264, 0.359872858562472, 0.501785825883555, 0.131682859300131, 0.316615799479412), STAGE2 = c(0.731671739637962, 0.0349659624268288, 0.250468754401736, 0.692725665227725, 0.63127309683438, 0.267791520174364, 0.683510378409481, 0.0930250575768417, 0.0469303813447592, 0.664296422188774, 0.104791391299402, 0.769846767284999, 0.6757286415883, 0.752123872001356, 0.90542496106528, 0.495894318363979, 0.693023609703169, 0.268140720274296, 0.715499146536921, 0.28736047756967, 0.0734999857226017, 0.220321023927838, 0.168970847093894, 0.329637479054441, 0.932333806354766, 0.920347276344009, 0.0188743828318305, 0.28700830085674, 0.122086809397366, 0.721930469997516, 0.269049759688981, 0.942247445889545, 0.121226461766639, 0.269410103956184, 0.543882827865607, 0.355909915494972, 0.805553875119072, 0.562638932909877, 0.545114635056338, 0.799859513505137, 0.028300052307727, 0.583254546237197, 0.256765686729071, 0.611805711034515, 0.12397867417326, 0.904971687781288, 0.383547195151525, 0.798974934455656, 0.808113210022865, 0.180237564596422, 0.523175753649262, 0.429905001793785, 0.304352173583082, 0.40672229380892, 0.216918826340633, 0.995255476036526, 0.769882865282478, 0.315877004728925, 0.473236774091463, 0.977350069130875), STAGE1 = c(0.368965432370926, 0.700308646686629, 0.37243236843735, 0.118248974397151, 0.924365141290828, 0.762350776297416, 0.697822321048335, 0.319088598768702, 0.877683213554975, 0.750204754502701, 0.504154725402079, 0.142208638643596, 0.789965310283345, 0.23064744899036, 0.356016354550943, 0.62336479144862, 0.213046955322873, 0.0244722022581561, 0.988358158842575, 0.823084572956891, 0.303622687500738, 0.96764100797214, 0.230471412999795, 0.817462150200591, 0.65596635231198, 0.91076738319561, 0.819508996182778, 0.213131722185214, 0.588018916178037, 0.123125204015868, 0.562792521212946, 0.660044396503998, 0.978415415705398, 0.0434221393194675, 0.376954390238487, 0.0621651663358314, 0.465209931429691, 0.384575866404939, 0.242944268682905, 0.0795230807069031, 0.0199700669678636, 0.32561072886232, 0.93772541505141, 0.585428916722007, 0.318073982629541, 0.32173669386666, 0.999051218264009, 0.317443552571237, 0.660217053596061, 0.823899098997627, 0.514457733357758, 0.541111096768051, 0.55508809891723, 0.984535365206941, 0.471258908268993, 0.993506687275954, 0.437682961713191, 0.131816605892007, 0.624970126586813, 0.464518729700115), VEG1 = c(0.210969928562774, 0.898985728644631, 0.22294741757493, 0.648093601120814, 0.575025933877221, 0.836263821166674, 0.365992846024045, 0.975590314436119, 0.7669346163546, 0.606281773283188, 0.727857331528672, 0.063336204942156, 0.56024387105928, 0.572624926379006, 0.928959824695475, 0.0649894364822472, 0.484687521083679, 0.0408377810735494, 0.0863494174729038, 0.38604744652937, 0.408453836726946, 0.886479379706545, 0.484111782315899, 0.931864491830317, 0.822804779811041, 0.42950876407607, 0.870394703931469, 0.643439731712405, 0.9667566201692, 0.304607549846345, 0.364859919775087, 0.103200556844174, 0.548045455964784, 0.198120269406476, 0.0343580658743184, 0.802310877023954, 0.993783336617898, 0.0816308254407008, 0.75765020441394, 0.864780385461517, 0.312678655208107, 0.74836775788609, 0.534055051243098, 0.288890283554979, 0.953079521748438, 0.731857197421753, 0.142177987385593, 0.996762443657084, 0.243292464894029, 0.491579671466928, 0.966627062162325, 0.339857447154563, 0.461728147289064, 0.634729865229662, 0.3609970911949, 0.258390752729148, 0.506726536795493, 0.672180274413292, 0.747445719779432, 0.29334486037021), VEG2 = c(0.380862280240474, 0.627729953103904, 0.878278401019092, 0.5613186824923, 0.636249763005486, 0.62292681302113, 0.720444249110863, 0.312557900422543, 0.234122422714287, 0.499639072408177, 0.855974632998296, 0.952555098662679, 0.309983642251446, 0.882338335501354, 0.361924385825836, 0.0113958292196743, 0.302133129735417, 0.0208791658903581, 0.117232590999101, 0.925267325435408, 0.0711269914368884, 0.552243014270943, 0.125495115593106, 0.661915190374669, 0.0293896936238214, 0.294418615555011, 0.308905573205888, 0.26936911134833, 0.931505940770319, 0.021435120451226, 0.227312617286182, 0.0195785283931383, 0.099692235112247, 0.0692925181417872, 0.832555037914636, 0.380793087982671, 0.68110703188308, 0.0936612801494288, 0.051047767208204, 0.273844437996178, 0.800343046949624, 0.911380903925364, 0.582479041522435, 0.748111458524328, 0.303383302981895, 0.906904782788438, 0.328934737098847, 0.69720659917789, 0.166551542287389, 0.875547286349842, 0.265071728355448, 0.698643970290581, 0.271578241223373, 0.956035715886986, 0.583251175640849, 0.268613096173177, 0.738778731483354, 0.22125582045959, 0.289055369557255, 0.697577213911879), VEG3 = c(0.413110318831049, 0.193370500853792, 0.0443323484313173, 0.312394866761789, 0.381013563679286, 0.774340211752776, 0.315822280223384, 0.499646073445189, 0.0433440470476795, 0.63592018453082, 0.375881818437784, 0.758815519342307, 0.517769074796318, 0.0878879638478763, 0.368268890348083, 0.86661723542387, 0.159257788537873, 0.302684958923527, 0.947570919817041, 0.157392788910852, 0.125769229458094, 0.348650174982873, 0.526963940972057, 0.632646354644645, 0.351109914949622, 0.836552506892888, 0.541982479414601, 0.427697355405521, 0.799356060602721, 0.205934189654569, 0.511986141383475, 0.807482528548695, 0.232665456121877, 0.933325748417277, 0.00683041685826713, 0.21579707708936, 0.92805622685536, 0.754838770281907, 0.39901873988239, 0.991423691630203, 0.73157540991456, 0.572610353705451, 0.806848128394244, 0.445431445389894, 0.26939539811961, 0.584527298222492, 0.386004220382543, 0.242375752770125, 0.558781312238412, 0.026491283834456, 0.343044539065029, 0.802187562912211, 0.885495299962394, 0.853256462542473, 0.126077883381321, 0.780548336023421, 0.840250789403559, 0.724812876802032, 0.704098036072535, 0.201989727535322), VEG4 = c(0.640799340986514, 0.987906611244886, 0.619024206202544, 0.209427026032481, 0.489727139189696, 0.687333141302772, 0.705507906383631, 0.208889941178721, 0.396874200897516, 0.712945929935184, 0.973888093276408, 0.763823354263263, 0.377489995555215, 0.0106930906722855, 0.803664402691951, 0.278756822036515, 0.0550468580770419, 0.265168521344556, 0.987573377094547, 0.734619443919844, 0.732929467499789, 0.241731972053084, 0.559823481667716, 0.155701349465554, 0.478406027901528, 0.961160568832851, 0.0998154836815058, 0.412094044212779, 0.243883425544374, 0.942308621371675, 0.939264039325847, 0.255191997669476, 0.25098970135836, 0.882652992136823, 0.287854570522429, 0.332318399664456, 0.412416503681846, 0.321868386633491, 0.415802982737621, 0.518373582745234, 0.160366074863356, 0.328725259142546, 0.740582698190321, 0.630219424448588, 0.0963326376312392, 0.841597785551852, 0.157610936357946, 0.37344035882236, 0.665277345336568, 0.472350881237637, 0.905421464878138, 0.67001107287601, 0.931361601273204, 0.478463787477743, 0.457093329803116, 0.901963213218536, 0.0989040910780277, 0.648496145325191, 0.149756719258531, 0.766506521363941), VEG5 = c(0.180283594647103, 0.787728519142257, 0.577806490890149, 0.909505719647681, 0.843259853705306, 0.19170485676349, 0.88285289130116, 0.390956223533144, 0.400453739302465, 0.138993973168619, 0.701071911445177, 0.0275528476660395, 0.196129789901458, 0.243754073834767, 0.586994020311301, 0.309287604588564, 0.54116171026274, 0.804029207727762, 0.633322226579327, 0.576488317293166, 0.657529806993353, 0.896942372401784, 0.69552906585738, 0.00605796084044197, 0.0333828607443704, 0.421697067898017, 0.344727073066524, 0.261778629704536, 0.662947164033825, 0.264729400387399, 0.361422182455181, 0.867093056031147, 0.994741928030988, 0.1442062746899, 0.757388857488693, 0.0417225005179914, 0.448764060787373, 0.844033548534798, 0.615968575276885, 0.424045632312236, 0.419379052486891, 0.47584349576189, 0.209702827083009, 0.765825214487151, 0.834675742228314, 0.1887294652982, 0.960231969290817, 0.935011727569393, 0.976674243295263, 0.820848463468705, 0.341388666484707, 0.552464582497579, 0.573838987965132, 0.698330744449092, 0.383783839725491, 0.713851582153272, 0.964603044208545, 0.70783623057194, 0.608167299267236, 0.461582775185318)), class = "data.frame", row.names = c(NA, -60L)) Code: library(ggplot2) library(ggcorrplot) library(corrplot) #Generate correlation matrix res <- cor(sen) #Generate matrix graphic ggcorrplot(res, hc.order = FALSE, type = "lower", outline.col = "white", ggtheme = ggplot2::theme_gray, colors = c("#6D9EC1", "white", "#E46726"), lab = TRUE, lab_size = 7, show.diag=FALSE, legend.title = "r-value", tl.cex=17)+ theme( legend.key.width=unit(1, "cm"), legend.key.height=unit(2.5, "cm"), legend.title=element_text(size=20), legend.text=element_text(size=15) )
library(reshape2) library(ggplot2) res <- cor(sen) res[upper.tri(res)] = NA diag(res) = NA d = melt(res) d = d[d$Var2 %in% paste0("STAGE", 1:3),] graphics.off() ggplot(d, aes(x = Var1, y = Var2, fill = value, label = round(value, 2))) + geom_tile(color = "black") + geom_text() + labs(fill = "r-value") + coord_equal() #OPTIONAL
I found that another solution could be to just resize the scale to where I could crop out the unneeded variables. This isn't the most technical answer, but it works for the parameters I need: png("wholeplant.png", width=900, height=600) ggcorrplot(res, hc.order = FALSE, type = "lower", outline.col = "white", ggtheme = ggplot2::theme_gray, colors = c("#6D9EC1", "white", "#E46726"), lab = TRUE, lab_size = 7, show.diag=FALSE, legend.title = "r-value", tl.cex=17)+ theme( #resize the legend width and height legend.key.width=unit(1, "cm"), legend.key.height=unit(1, "cm"), legend.title=element_text(size=20), legend.text=element_text(size=15), #Put it where it won't be out of the way legend.position = c(1.1, 0.2) ) dev.off()
How can I modify time on x-axis in ggplot?
I am creating a plot with r and I would like to change the time on the x-axis. There should be labels for every hour. I used: scale_x_datetime(breaks="1 hour", labels=date_format("%H:%M")), but unfortunately, it is not working. Does someone has an idea? Thanks for helping. This is my code: input2 <- "C:\\Users\\time_distance.csv" time_distance <- read.csv(input2, sep=";") library(scales) time <- strptime(time_distance$time, format = "%H:%M:%S") plot2 <-ggplot(time_distance, aes(x = time, y = distance, group = 1)) + geom_point(stat = "identity") + geom_smooth(method = lm, color = "red", se = FALSE) + theme(legend.position = "none") + theme_bw() + labs(y = "Distance [m]", x = "time [hour]")+ scale_y_continuous(limits = c(0,1600), breaks = seq(100, 1500, 100)) print(plot2) This is my data: dput(time_distance) structure(list(time = c("12:51:57", "12:55:16", "12:56:29", "13:25:05", "13:36:54", "13:55:37", "14:11:20", "14:13:17", "15:14:26", "15:18:48", "15:21:01", "15:22:29", "15:25:13", "15:28:16", "15:28:26", "15:39:58", "15:46:49", "15:50:45", "15:59:51", "16:02:38", "16:24:05", "16:35:17", "11:15:24", "13:32:40", "14:42:39", "15:24:08", "15:32:28", "16:43:48", "16:48:42", "17:10:28", "17:27:55", "11:23:25", "12:19:21", "12:27:19", "12:28:39", "12:47:18", "13:17:01", "14:06:26", "14:15:35", "14:18:06", "14:26:26", "14:50:32", "15:25:26", "15:33:50", "15:56:02", "15:58:45", "16:11:02", "16:35:42", "17:16:24", "17:28:30", "08:58:43", "09:55:52", "10:14:17", "10:24:17", "10:36:42", "10:55:35", "14:18:29", "14:36:59", "15:22:46", "15:51:35", "15:58:29", "16:36:36", "16:47:48", "13:33:52", "14:31:38", "14:49:03", "16:13:31", "11:11:20", "11:46:00", "11:50:43", "12:13:11", "13:07:31", "13:27:46", "14:37:37", "15:27:51", "10:07:52", "10:18:18", "11:56:53", "12:18:37", "12:45:55", "13:14:20", "13:23:21", "13:30:15", "13:31:34", "13:33:12", "13:48:06", "13:53:52", "14:30:18", "14:44:24", "14:51:14", "15:03:19", "15:33:32", "15:49:00", "15:55:53", "16:07:24", "16:11:43", "16:21:43", "16:35:50", "16:39:37", "16:48:41", "10:02:45", "12:14:40", "13:10:27", "14:36:28", "14:51:12", "15:38:50", "15:58:29", "10:53:33", "11:11:27", "11:32:26", "11:38:36", "12:56:03", "13:45:09", "14:39:48", "14:51:57", "14:53:25", "15:08:02", "16:01:32", "16:22:14", "16:46:01", "08:57:18", "09:07:51", "09:25:10", "09:34:32", "10:15:35", "10:33:54", "11:07:55", "11:26:00", "11:40:21", "11:50:45", "11:57:16", "12:55:00", "13:21:18", "14:47:07", "14:50:21", "14:56:56", "15:06:39", "15:21:53", "15:36:26", "15:48:37", "15:54:50", "16:01:28", "16:12:21", "16:21:53", "16:26:20", "16:30:52", "09:49:00", "10:13:53", "10:27:21", "10:46:43", "12:24:04", "12:30:04", "12:54:33", "13:33:14", "13:52:55", "14:12:14", "14:37:37", "14:42:58", "14:44:37", "14:51:39", "15:08:57", "15:38:08", "15:49:06", "16:05:53", "17:01:34", "08:31:56", "09:44:00", "10:19:35", "10:47:49", "11:18:16", "11:35:49", "12:32:43", "12:43:45", "13:11:05", "13:24:34", "13:32:46", "13:42:01", "14:20:17", "14:31:11", "14:36:30", "16:06:58", "08:30:07", "09:02:22", "10:03:07", "10:29:09", "10:52:23", "11:47:59", "12:58:26", "13:47:26", "13:53:05", "14:08:33", "14:16:46", "14:28:50", "15:16:42", "16:07:43", "08:27:08", "08:52:03", "09:04:13", "09:14:04", "09:22:39", "09:32:25", "09:39:04", "10:18:02", "13:06:58", "13:21:03", "13:37:28", "13:41:09", "14:17:06", "14:36:17", "14:51:45"), distance = c(89.6472646, 162.833946, 204.1718123, 69.32061609, 145.5725233, 157.8104904, 142.7204165, 139.4100682, 156.290279, 281.1730457, 211.3723597, 146.9900352, 155.5278723, 121.630972, 115.0260845, 103.4678487, 535.2962882, 172.2392646, 187.1019506, 192.2072681, 163.1432699, 328.1146666, 161.378541, 276.9657775, 288.6843714, 232.236379, 286.5742551, 171.9799195, 131.3821584, 645.23548, 328.8999153, 83.1376454, 206.8425108, 160.1471859, 163.4999165, 71.84934976, 122.7265289, 156.6223912, 245.7737619, 76.4010552, 191.7314754, 241.2484589, 314.4240603, 168.1778327, 249.4432742, 171.5837494, 445.289732, 150.781544, 231.0174121, 233.6595053, 212.0908014, 274.9652469, 230.2231595, 463.3457859, 185.4275877, 413.7982665, 225.4934765, 171.8698762, 287.8326822, 457.6477022, 216.539991, 203.7116093, 261.3755307, 176.3162834, 113.2523456, 186.0197098, 110.2207489, 164.0611501, 111.7164405, 157.4453558, 158.5689564, 155.240531, 117.3045189, 117.6454036, 224.4921484, 326.2455013, 205.1963079, 166.1579876, 245.2281083, 168.9163027, 280.1466904, 331.9011914, 268.5518097, 296.9010562, 476.5082804, 391.8845907, 255.7662667, 483.9525726, 283.381313, 592.5282434, 158.4318925, 722.0857244, 1524.855308, 261.3729014, 495.6679407, 585.3051787, 485.0507777, 472.889667, 394.5888053, 328.1134478, 209.4992859, 161.8100828, 179.8945385, 438.95502, 538.0923178, 142.1052511, 150.0340155, 170.1366692, 123.6032668, 171.7141119, 98.76667809, 235.874407, 241.7648426, 224.3868202, 167.1152772, 67.44743255, 87.79127323, 150.6129032, 476.6532019, 186.2234702, 139.4749401, 102.0916653, 69.80207399, 77.6190789, 179.4630148, 109.0492763, 165.9927234, 228.9755043, 247.591346, 262.0171879, 155.9824185, 207.1022985, 121.5681699, 178.136665, 126.386831, 74.78485864, 71.12781299, 78.21953935, 317.1932926, 219.2749766, 247.6333865, 98.85128473, 235.0537481, 90.20871946, 124.2324844, 125.3904596, 248.5174138, 148.2374205, 299.5611988, 77.88228217, 194.0273272, 117.208969, 347.7014402, 306.3749268, 313.0498324, 313.7161044, 479.518518, 314.9932, 271.3977789, 274.4941856, 391.3815061, 418.8094285, 243.0563698, 599.4055807, 169.8079415, 70.72250265, 181.7665702, 172.3945082, 95.23804572, 147.9418742, 101.8629435, 127.5025047, 236.7513256, 113.5718566, 133.5204809, 215.6790291, 72.40720193, 80.22093147, 188.9783062, 75.71888165, 100.6934031, 293.32912, 269.7824729, 772.4577258, 468.3636793, 221.4304232, 282.6939389, 250.2391573, 111.5475549, 181.1174836, 221.0176637, 206.4821957, 127.4349516, 112.25765, 198.9440625, 144.8789547, 86.23383985, 48.90437015, 107.9818848, 186.2547336, 156.2620094, 112.5252141, 349.3143126, 342.4842646, 69.19741708, 206.0086208, 211.6554678, 119.8004909, 136.4785611, 111.8184516)), class = "data.frame", row.names = c(NA, -210L))
The column time should be POSIXct to be able to use scale_x_datetime, convert it to the right type and it will work. This should work: library(scales) time_distance$time <- as.POSIXct(time_distance$time, format = "%H:%M:%S") ggplot(time_distance, aes(x=time, y=distance, group=1)) + geom_point() + geom_smooth(method=lm , color="red", se=FALSE) +theme(legend.position="none") + theme_bw()+ labs(y = "Distance [m]", x = "time [hour]")+ scale_y_continuous(limits=c(0,1600), breaks=seq(100, 1500, 100)) + scale_x_datetime(date_breaks="1 hour", labels=date_format("%H:%M"))
Incorporating an extra dimension in stat_density2d
I'm doing some single particle tracking experiments, and I've made a plot that looks like this And an excerpt of the data like this Tracks_subset <- structure(list(x = c(50.9870663039413, 51.1454673261645, 51.2438755862949, 51.2095459932038, 51.6660042216598, 50.9114140942979, 51.0024489561214, 50.8857311418507, 51.2794722111027, 51.2431331170315, 51.1686446878892, 51.6828347775235, 52.1450571292308, 52.0846161251043, 52.6398039669697, 52.8833174825244, 52.5705151101221, 47.7126603341761, 47.7043102797697, 47.7383984362003, 47.6860850023564, 47.6951829901912, 47.6699652212862, 47.7413324987855, 47.6843782373267, 47.644111020292, 47.6618622920263, 47.6541735611274, 47.6649621568611, 47.6523884564593, 47.6610279361442, 47.6512197115578, 47.6657086161767, 47.6192509459374, 47.6207586571131, 47.5774206679784, 47.6200920953012, 47.7306727739304, 47.6379509867329, 47.714775352656, 47.6550462636824, 47.6609930612728, 47.7000183950083, 47.6649651728503, 47.6539558351917, 47.6389944434634, 47.6717405874446, 47.6953802257671, 47.670156194978, 47.6659335190123, 47.6765976765644, 47.6840293686863, 47.6835120352331, 47.7206680944438, 47.6295515092937, 47.6296091556035, 47.6942899171821, 47.7250190805859, 47.5914020018512, 47.70039477981, 47.6173948003708, 47.6403690517817, 47.706224437305, 47.6278056300272, 47.7050590713087, 47.6202775363008, 47.616604053917, 47.6756516087319, 47.7266296774674, 47.6446057760854, 47.5705082443691, 47.6595393038973, 47.7454552888996, 47.6741550431176, 47.5860886147769, 47.5711827843158, 47.6032212783683, 47.6664108892973, 47.6658197925874, 48.3968244300988, 48.9904220754653, 48.8091985242839, 48.6060438195256, 47.9442329308765, 47.5918680853048, 61.7398341199382, 61.5105531711929, 61.3508575465536, 60.7780284493327, 60.9300399030291, 61.5425582885666, 61.6082571595723, 61.5298863337404, 61.7856612691648, 61.9071110539638, 62.2590343816915, 62.4185223478263, 62.9672715597194, 63.3275273995809, 63.8113387354958, 64.3231547762155, 63.7003651772996, 64.5725239608709 ), y = c(25.4839360156051, 25.6047955465986, 25.4262289850266, 25.6172499551253, 25.2123769606517, 25.0385424557608, 24.966470254684, 25.0765695667183, 25.3864947576636, 25.5754234194841, 25.362590160186, 25.7772644867977, 25.5496302761478, 25.2288998273316, 25.0558763011072, 24.9078484752662, 24.976087151153, 25.3889343715977, 25.4338531136808, 25.4429033909954, 25.4318520315956, 25.3990616319309, 25.4511690945816, 25.3200107971065, 25.481502531704, 25.4813627020502, 25.4853491195569, 25.4847769218943, 25.4575428518825, 25.4557467653372, 25.4624512146039, 25.4595647154719, 25.4563293549661, 25.4804991266559, 25.3812415821813, 25.4473050286014, 25.4196869940826, 25.4442315703239, 25.479341197772, 25.4300738230213, 25.4693914326269, 25.4567345365435, 25.4674089428837, 25.4112535933299, 25.4637733103663, 25.3956145368333, 25.4990099732346, 25.4839175917237, 25.4745320245195, 25.4005315613532, 25.469591766703, 25.4539955232768, 25.4657553925353, 25.4245323404107, 25.4361711386943, 25.3891764540008, 25.466965662987, 25.1009694093232, 25.0358862503509, 25.1789725026332, 25.42714785297, 25.4795979566479, 25.468929968642, 25.4239887143419, 25.5281893742505, 25.4093001632827, 25.4384275123265, 25.4344555350342, 25.4429352491382, 25.5199514977758, 25.4910332653875, 25.4595502380103, 25.4968060944952, 25.4827240176748, 25.3698050293595, 25.4439838950347, 25.3741755234997, 25.5563892658156, 25.6960651973953, 25.7204681252841, 26.4352696253167, 27.1441226535699, 26.6615707288907, 26.3571435264103, 26.2462341038955, 25.4044026980314, 24.9429648374475, 25.316181029454, 25.5746195865509, 25.4159071371913, 25.0535276243763, 25.1404356519081, 24.9217292382466, 25.0681095303721, 25.6848864023155, 25.7992168557371, 25.2353282682059, 24.9773329424632, 24.4822347858453, 24.268105758617, 24.6415345738956, 24.0369860755695, 23.6865149091661 ), steplength = c(0, 0.199243343862037, 0.203887720506984, 0.194081250972988, 0.610144455009775, 0.774354373270278, 0.116110930731278, 0.160452194371228, 0.501084477506786, 0.192391707241276, 0.225491734526058, 0.660565095527355, 0.515234739003464, 0.326375758563115, 0.581524445214716, 0.284975559445094, 0.320159087122816, 0, 0.0456882566850305, 0.035269107279719, 0.053468008239723, 0.0340291594490852, 0.0578888895422253, 0.14931773938219, 0.171240673444157, 0.0402674598162118, 0.0181933826629311, 0.00770999306101455, 0.0292931453981066, 0.0127013333423588, 0.0109357326776552, 0.0102241452834658, 0.0148457372554814, 0.0523688169390838, 0.0992689947975958, 0.079009874415496, 0.0508291898464581, 0.113271897260304, 0.0991464359452704, 0.091264765459093, 0.071508310668843, 0.0139843276580312, 0.0404588633537657, 0.066197822223741, 0.0536612168447871, 0.0697815280139674, 0.108457024734435, 0.0280466126241494, 0.0269135765181205, 0.0741208441744882, 0.06987872508325, 0.0172763670024788, 0.0117712428774199, 0.055496961741496, 0.0918569197986897, 0.0469947200495333, 0.101167000316718, 0.367284003435199, 0.148624834100549, 0.179869678487995, 0.261686837845756, 0.0572610653386249, 0.0667138499153149, 0.0903837688578274, 0.129714577882537, 0.146022440613998, 0.029358081257867, 0.0591809963859102, 0.0516785162644559, 0.112514101125709, 0.0795406083136827, 0.0944336305219004, 0.0936459039218517, 0.0726775751944748, 0.143200536740801, 0.0756616672734531, 0.0768093343124944, 0.192859469091138, 0.139677182309732, 0.731411842228994, 0.929139036438272, 0.731651960406345, 0.523572529910693, 0.728470709050467, 0.369407477451884, 0, 0.515261732169024, 0.405947063673079, 0.628429520644252, 0.219766065707624, 0.711686506777716, 0.108946532303133, 0.232324087684107, 0.294699860051154, 0.628620521453137, 0.37002902748092, 0.586009172701016, 0.606372233581375, 0.61229605162923, 0.529079057477617, 0.633565102904463, 0.867954936241549, 0.939942010041431)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -103L), .internal.selfref = <pointer: 0x0>, .Names = c("x", "y", "steplength")) And the code for the plot is ggplot(data = Tracks_subset, aes(x = x, y = y)) + # stat_bin2d(bins = 10, aes(fill = ..density..)) + # blocky heat map stat_density2d(aes(fill = ..level.., alpha = ..level..), bins = 20, geom = 'polygon') + xlim(-20,max(Tracks_subset$x)+20) + # Adds padding for polygons to have proper shape ylim(-20,max(Tracks_subset$y)+20) + coord_cartesian(xlim = c(0,max(Tracks_subset$x)), # Clips extra padding visually only ylim = c(0,max(Tracks_subset$y)), expand = FALSE) + scale_fill_continuous(low = "white", high = "red") + theme_classic(base_size = 12, base_family = "Droid Sans") + theme(axis.text = element_text(color = "black"), strip.text.y = element_blank(), strip.background = element_rect(color = "white"), legend.text = element_text(size = 12, family = "Droid Sans"), legend.title = element_blank(), legend.position = "none") + facet_wrap(condition + lipase ~ video, scales = "free") Now my question is, how can I somehow incorporate the steplength parameter as either an extra dimension (e.g. instead of ..level.. I could somehow use steplength) or weight in the stat_density2d? I know it's possible for the stat_bin2d, but this gives me a very blocky appearance that I don't really like.