问题
I am a newbie to R. I have climate data, as presented in few lines below. The data indicate temperature change with respect to historical climate. I want to create a box plot (ggplot) which looks like this:
I am trying to figure out how to create this kind of plot. Any help will be highly appreciated
DATE TMAX TMIN
1/1/2010 2.927586207 0.627586207
1/2/2010 0.827586207 0.824137931
1/3/2010 2.031034483 0.55862069
1/4/2010 2.648275862 0.7
1/5/2010 2.196551724 0.393103448
1/6/2010 1.220689655 0.5
1/7/2010 1.224137931 0.813793103
1/8/2010 1.94137931 1.05862069
1/9/2010 2.648275862 0.875862069
1/10/2010 0.637931034 0.817241379
1/11/2010 2.444827586 0.827586207
1/12/2010 0.072413793 0.634482759
1/13/2010 -0.320689655 0.755172414
1/14/2010 0.606896552 1.113793103
1/15/2010 1.544827586 1.151724138
1/16/2010 1.017241379 0.775862069
1/17/2010 1.237931034 0.986206897
1/18/2010 1.086206897 0.689655172
1/19/2010 -0.324137931 0.548275862
1/20/2010 -0.410344828 0.451724138
1/21/2010 0.624137931 0.837931034
1/22/2010 -1.424137931 0.789655172
1/23/2010 0.917241379 0.6
1/24/2010 -1.144827586 0.751724138
1/25/2010 0.289655172 0.365517241
1/26/2010 0.313793103 0.110344828
1/27/2010 -1.54137931 0.251724138
1/28/2010 0.162068966 0.251724138
1/29/2010 -0.04137931 0.268965517
1/30/2010 -1.120689655 0.293103448
1/31/2010 1.386206897 0.582758621
2/1/2010 1.062068966 0.906896552
2/2/2010 0.503448276 0.979310345
2/3/2010 0.462068966 0.817241379
2/4/2010 0.455172414 0.817241379
2/5/2010 0.662068966 0.424137931
2/6/2010 0.272413793 0.74137931
2/7/2010 -0.020689655 0.348275862
2/8/2010 0.175862069 -0.05862069
2/9/2010 0.362068966 0.331034483
2/10/2010 0.744827586 0.617241379
2/11/2010 0.427586207 0.796551724
2/12/2010 0.348275862 0.903448276
2/13/2010 0.765517241 0.920689655
2/14/2010 -0.05862069 0.424137931
2/15/2010 0.055172414 0.610344828
2/16/2010 0.372413793 0.424137931
2/17/2010 0.934482759 0.479310345
2/18/2010 0.244827586 0.210344828
2/19/2010 0.831034483 0.486206897
2/20/2010 0.572413793 0.648275862
2/21/2010 0.65862069 0.331034483
2/22/2010 0.34137931 0.393103448
2/23/2010 0.4 0.668965517
2/24/2010 0.7 0.272413793
2/25/2010 1.231034483 0.593103448
2/26/2010 0.648275862 1.103448276
2/27/2010 0.220689655 1.193103448
2/28/2010 -0.14137931 1.027586207
3/1/2010 0.924137931 1.286206897
3/2/2010 -0.224137931 0.127586207
3/3/2010 0.696551724 0.279310345
3/4/2010 -0.513793103 0.237931034
3/5/2010 -0.924137931 0.306896552
3/6/2010 -0.327586207 0.4
3/7/2010 -1.334482759 0.910344828
3/8/2010 -1.627586207 0.84137931
3/9/2010 0.093103448 0.668965517
3/10/2010 0.589655172 0.762068966
3/11/2010 0.282758621 0.751724138
3/12/2010 0.575862069 1.134482759
3/13/2010 -0.003448276 1.403448276
3/14/2010 0.103448276 2.617241379
3/15/2010 0.5 3.389655172
3/16/2010 -0.017241379 1.220689655
3/17/2010 -0.103448276 0.434482759
3/18/2010 -0.110344828 0.362068966
3/19/2010 0.289655172 3.55E-15
3/20/2010 0.603448276 1.044827586
3/21/2010 1.189655172 1.096551724
3/22/2010 0.375862069 0.631034483
3/23/2010 0.475862069 0.831034483
3/24/2010 0.279310345 1.234482759
3/25/2010 -0.034482759 0.85862069
3/26/2010 0.286206897 0.886206897
3/27/2010 0.789655172 0.896551724
3/28/2010 0.789655172 0.734482759
3/29/2010 0.989655172 1.05862069
3/30/2010 0.572413793 0.737931034
3/31/2010 0.982758621 0.455172414
4/1/2010 0.372413793 0.586206897
4/2/2010 0.35862069 1.055172414
4/3/2010 0.35862069 0.855172414
4/4/2010 0.979310345 1.079310345
4/5/2010 0.568965517 0.968965517
4/6/2010 0.572413793 1.075862069
4/7/2010 0.768965517 0.862068966
4/8/2010 0.689655172 0.679310345
4/9/2010 0.979310345 0.572413793
4/10/2010 1.265517241 0.651724138
4/11/2010 0.979310345 0.468965517
4/12/2010 0.262068966 1.048275862
4/13/2010 0.862068966 1.048275862
4/14/2010 0.675862069 0.962068966
4/15/2010 0.975862069 0.872413793
4/16/2010 1.572413793 0.762068966
4/17/2010 2.375862069 0.968965517
4/18/2010 -0.248275862 1.14137931
4/19/2010 1.486206897 0.593103448
4/20/2010 0.868965517 0.762068966
4/21/2010 -0.813793103 0.2
4/22/2010 0.879310345 0.382758621
4/23/2010 1.079310345 0.703448276
4/24/2010 0.851724138 1.044827586
4/25/2010 1.6 0.696551724
4/26/2010 0.586206897 0.486206897
4/27/2010 1.079310345 0.665517241
4/28/2010 1.089655172 0.889655172
4/29/2010 0.886206897 0.596551724
4/30/2010 1.079310345 0.496551724
5/1/2010 1.293103448 0.789655172
5/2/2010 0.796551724 0.427586207
5/3/2010 1.206896552 0.34137931
5/4/2010 0.748275862 0.479310345
5/5/2010 1.006896552 0.355172414
5/6/2010 1.106896552 0.348275862
5/7/2010 0.889655172 0.427586207
5/8/2010 1.020689655 0.262068966
5/9/2010 0.934482759 0.368965517
5/10/2010 1.017241379 0.54137931
5/11/2010 0.817241379 0.644827586
5/12/2010 1.34137931 0.382758621
5/13/2010 0.913793103 0.35862069
5/14/2010 0.903448276 0.25862069
5/15/2010 0.527586207 0.679310345
5/16/2010 0.893103448 0.220689655
5/17/2010 0.806896552 0.35862069
5/18/2010 1.017241379 0.248275862
5/19/2010 0.717241379 -0.117241379
5/20/2010 0.631034483 0.482758621
5/21/2010 0.613793103 0.268965517
5/22/2010 0.331034483 0.193103448
5/23/2010 0.54137931 0.3
5/24/2010 0.720689655 -0.8
5/25/2010 0.789655172 -0.027586207
5/26/2010 0.934482759 -0.903448276
5/27/2010 0.927586207 0.265517241
5/28/2010 1.127586207 0.765517241
5/29/2010 1.027586207 0.565517241
5/30/2010 1.027586207 0.386206897
5/31/2010 0.924137931 -0.175862069
6/1/2010 0.603448276 0.193103448
6/2/2010 0.644827586 0.486206897
6/3/2010 0.727586207 0.375862069
6/4/2010 1.14137931 0.506896552
6/5/2010 1.034482759 0.255172414
6/6/2010 0.631034483 -0.289655172
6/7/2010 0.74137931 -0.265517241
6/8/2010 0.644827586 -0.082758621
6/9/2010 1.14137931 0.793103448
6/10/2010 0.827586207 0.937931034
6/11/2010 1.434482759 0.744827586
6/12/2010 1.248275862 0.979310345
6/13/2010 0.637931034 0.193103448
6/14/2010 0.644827586 -0.065517241
6/15/2010 0.731034483 -0.196551724
6/16/2010 0.951724138 0.120689655
6/17/2010 0.851724138 1.427586207
6/18/2010 0.737931034 -0.124137931
6/19/2010 1.04137931 -0.017241379
6/20/2010 0.734482759 -0.189655172
6/21/2010 0.931034483 0.331034483
6/22/2010 0.851724138 0.962068966
6/23/2010 1.444827586 0.465517241
6/24/2010 1.165517241 -0.24137931
6/25/2010 1.162068966 0.334482759
6/26/2010 0.951724138 0.451724138
6/27/2010 1.051724138 0.331034483
6/28/2010 0.931034483 0.1
6/29/2010 1.424137931 1.034482759
6/30/2010 1.648275862 0.668965517
7/1/2010 1.031034483 0.168965517
7/2/2010 0.331034483 -0.844827586
7/3/2010 0.562068966 -0.617241379
7/4/2010 0.265517241 -0.713793103
7/5/2010 0.448275862 0.079310345
7/6/2010 0.837931034 -0.368965517
7/7/2010 0.662068966 0.455172414
7/8/2010 0.548275862 0.244827586
7/9/2010 0.575862069 0.382758621
7/10/2010 0.651724138 0.210344828
7/11/2010 0.472413793 0.444827586
7/12/2010 0.665517241 0.079310345
7/13/2010 0.851724138 0.686206897
7/14/2010 0.772413793 0.824137931
7/15/2010 1.148275862 -0.034482759
7/16/2010 1.365517241 -0.006896552
7/17/2010 1.155172414 -0.486206897
7/18/2010 1.634482759 0.117241379
7/19/2010 0.868965517 0.637931034
7/20/2010 0.965517241 0.54137931
7/21/2010 1.072413793 0.272413793
7/22/2010 1.134482759 0.110344828
7/23/2010 1.562068966 0.451724138
7/24/2010 0.575862069 1.248275862
7/25/2010 0.648275862 0.751724138
7/26/2010 1.25862069 0.793103448
7/27/2010 1.05862069 1.013793103
7/28/2010 0.75862069 1.055172414
7/29/2010 1.265517241 0.682758621
7/30/2010 0.637931034 0.451724138
7/31/2010 1.251724138 0.817241379
8/1/2010 1.44137931 0.344827586
8/2/2010 0.862068966 0.479310345
8/3/2010 0.610344828 -0.031034483
8/4/2010 0.84137931 0.25862069
8/5/2010 0.748275862 0.065517241
8/6/2010 0.217241379 0.420689655
8/7/2010 1.055172414 0.489655172
8/8/2010 0.717241379 0.520689655
8/9/2010 0.827586207 0.524137931
8/10/2010 1.010344828 0.3
8/11/2010 1.024137931 0.351724138
8/12/2010 1.05862069 0.589655172
8/13/2010 1.975862069 0.05862069
8/14/2010 1.465517241 0.306896552
8/15/2010 1.475862069 0.672413793
8/16/2010 1.534482759 0.55862069
8/17/2010 0.934482759 -0.124137931
8/18/2010 0.906896552 1.103448276
8/19/2010 0.720689655 0.737931034
8/20/2010 1.227586207 0.765517241
8/21/2010 1.051724138 0.982758621
8/22/2010 1.14137931 0.434482759
8/23/2010 1.265517241 0.255172414
8/24/2010 1.355172414 0.651724138
8/25/2010 1.793103448 0.613793103
8/26/2010 1.362068966 0.572413793
8/27/2010 1.262068966 0.362068966
8/28/2010 1.482758621 0.306896552
8/29/2010 1.079310345 0.286206897
8/30/2010 1.286206897 0.610344828
8/31/2010 1.382758621 0.379310345
9/1/2010 1.672413793 0.803448276
9/2/2010 0.817241379 -0.320689655
9/3/2010 1.720689655 0.406896552
9/4/2010 1.296551724 0.203448276
9/5/2010 1.296551724 0.424137931
9/6/2010 1.879310345 0.179310345
9/7/2010 1.579310345 0.393103448
9/8/2010 1.468965517 0.172413793
9/9/2010 1.468965517 0.268965517
9/10/2010 1.182758621 0.482758621
9/11/2010 1.106896552 0.503448276
9/12/2010 1.503448276 0.403448276
9/13/2010 0.903448276 0.2
9/14/2010 1.179310345 0.25862069
9/15/2010 1.175862069 -0.003448276
9/16/2010 1.075862069 0.306896552
9/17/2010 1.082758621 0.317241379
9/18/2010 1.165517241 -0.224137931
9/19/2010 1.551724138 0.734482759
9/20/2010 1.9 -0.272413793
9/21/2010 0.962068966 0.175862069
9/22/2010 0.765517241 0.293103448
9/23/2010 0.951724138 0.444827586
9/24/2010 1.455172414 0.027586207
9/25/2010 0.95862069 0.762068966
9/26/2010 0.693103448 0.703448276
9/27/2010 0.786206897 0.182758621
9/28/2010 1.355172414 0.124137931
9/29/2010 1.451724138 0.513793103
9/30/2010 1.134482759 0.9
10/1/2010 1.420689655 0.613793103
10/2/2010 0.755172414 -0.193103448
10/3/2010 0.148275862 -0.413793103
10/4/2010 0.65862069 0.106896552
10/5/2010 0.872413793 -0.282758621
10/6/2010 0.668965517 -0.279310345
10/7/2010 0.775862069 0.203448276
10/8/2010 0.968965517 -0.010344828
10/9/2010 0.903448276 0.224137931
10/10/2010 0.879310345 -0.086206897
10/11/2010 1.203448276 -0.051724138
10/12/2010 0.575862069 -0.072413793
10/13/2010 0.689655172 0.327586207
10/14/2010 0.603448276 -0.162068966
10/15/2010 1.096551724 0.313793103
10/16/2010 1.2 0.634482759
10/17/2010 1.103448276 0.437931034
10/18/2010 1.520689655 -0.368965517
10/19/2010 1.537931034 0.586206897
10/20/2010 1.1 0.651724138
10/21/2010 1.589655172 0.648275862
10/22/2010 1.027586207 0.162068966
10/23/2010 1.213793103 0.551724138
10/24/2010 1.444827586 0.668965517
10/25/2010 1.227586207 0.579310345
10/26/2010 1.5 0.231034483
10/27/2010 0.762068966 0.293103448
10/28/2010 1.651724138 -0.224137931
10/29/2010 0.010344828 -0.25862069
10/30/2010 1.724137931 0.365517241
10/31/2010 0.589655172 0.424137931
11/1/2010 0.9 -0.472413793
11/2/2010 1.286206897 0.034482759
11/3/2010 1.15862069 0.179310345
11/4/2010 1.568965517 0.403448276
11/5/2010 1.168965517 -0.003448276
11/6/2010 1.472413793 0.4
11/7/2010 1.165517241 0.696551724
11/8/2010 1.572413793 0.472413793
11/9/2010 1.317241379 0.144827586
11/10/2010 0.931034483 -0.137931034
11/11/2010 1.355172414 -0.017241379
11/12/2010 1.055172414 0.4
11/13/2010 1.668965517 0.220689655
11/14/2010 1.889655172 0.810344828
11/15/2010 2.275862069 0.172413793
11/16/2010 1.451724138 0.365517241
11/17/2010 2.544827586 0.44137931
11/18/2010 1.05862069 0.479310345
11/19/2010 0.737931034 0.255172414
11/20/2010 1.162068966 -7.11E-15
11/21/2010 1.562068966 0.193103448
11/22/2010 1.448275862 0.410344828
11/23/2010 1.951724138 0.679310345
11/24/2010 0.248275862 0.282758621
11/25/2010 1.648275862 0.196551724
11/26/2010 2.365517241 -0.172413793
11/27/2010 1.562068966 -0.131034483
11/28/2010 1.462068966 -0.120689655
11/29/2010 1.362068966 0.568965517
11/30/2010 1.293103448 0.710344828
12/1/2010 2.803448276 0.231034483
12/2/2010 4.34137931 0.717241379
12/3/2010 1.234482759 0.003448276
12/4/2010 1.486206897 -0.044827586
12/5/2010 1.162068966 0.220689655
12/6/2010 0.286206897 0.293103448
12/7/2010 1.175862069 0.189655172
12/8/2010 1.124137931 0.248275862
12/9/2010 0.486206897 -0.172413793
12/10/2010 0.996551724 0.420689655
12/11/2010 0.082758621 -0.248275862
12/12/2010 0.724137931 -0.093103448
12/13/2010 0.993103448 -0.006896552
12/14/2010 1.379310345 -0.020689655
12/15/2010 0.275862069 0.189655172
12/16/2010 0.772413793 0.068965517
12/17/2010 1.482758621 0.175862069
12/18/2010 1.15862069 0.548275862
12/19/2010 -0.137931034 0.562068966
12/20/2010 2.006896552 0.037931034
12/21/2010 0.424137931 0.293103448
12/22/2010 0.517241379 0.303448276
12/23/2010 0.817241379 0.027586207
12/24/2010 0.620689655 0.210344828
12/25/2010 1.703448276 0.368965517
12/26/2010 0.034482759 0.3
12/27/2010 0.713793103 0.027586207
12/28/2010 1.131034483 0.068965517
12/29/2010 2.806896552 -0.168965517
12/30/2010 1.117241379 0.296551724
12/31/2010 0.848275862 0.420689655
1/1/2011 1.810344828 0.086206897
1/2/2011 0.248275862 0.168965517
1/3/2011 2.027586207 0.903448276
1/4/2011 1.448275862 0.634482759
1/5/2011 1.013793103 0.186206897
1/6/2011 1.455172414 0.103448276
1/7/2011 0.820689655 0.65862069
1/8/2011 4.762068966 0.010344828
1/9/2011 1.986206897 0.724137931
1/10/2011 0.231034483 0.65862069
1/11/2011 2.220689655 0.45862069
1/12/2011 2.248275862 0.472413793
1/13/2011 1.137931034 0.479310345
1/14/2011 1.555172414 0.510344828
1/15/2011 1.268965517 1.255172414
1/16/2011 1.186206897 1.134482759
1/17/2011 0.772413793 0.713793103
1/18/2011 1.337931034 0.575862069
1/19/2011 -0.351724138 0.417241379
1/20/2011 0.586206897 0.724137931
1/21/2011 1.117241379 0.682758621
1/22/2011 -0.151724138 0.596551724
1/23/2011 -0.420689655 -0.010344828
1/24/2011 0.486206897 0.096551724
1/25/2011 -1.05862069 0.517241379
1/26/2011 0.275862069 0.613793103
1/27/2011 1.317241379 0.165517241
1/28/2011 0.44137931 0.568965517
1/29/2011 -1.120689655 0.362068966
1/30/2011 1.689655172 0.693103448
1/31/2011 -0.337931034 0.889655172
2/1/2011 -1.444827586 0.372413793
2/2/2011 -0.044827586 -0.075862069
2/3/2011 1.231034483 0.593103448
2/4/2011 0.796551724 0.565517241
2/5/2011 0.110344828 0.682758621
2/6/2011 0.84137931 0.496551724
2/7/2011 -0.55862069 0.720689655
2/8/2011 0.862068966 0.720689655
2/9/2011 0.117241379 0.382758621
2/10/2011 0.065517241 0.637931034
2/11/2011 0.8 1.072413793
2/12/2011 0.424137931 0.793103448
2/13/2011 1 0.85862069
2/14/2011 1.006896552 0.85862069
2/15/2011 -0.906896552 0.548275862
2/16/2011 0.324137931 0.503448276
2/17/2011 0.817241379 0.486206897
2/18/2011 0.137931034 0.706896552
2/19/2011 0.396551724 0.84137931
2/20/2011 -0.144827586 1.024137931
2/21/2011 0.9 1.162068966
2/22/2011 1.293103448 0.34137931
2/23/2011 1.603448276 -0.324137931
回答1:
I borrowed some parts from the other answers and pasted them together. The unique part is the facet_grid
. Therefore I replicated the data using an index column gr
which is used for the facet function.
df %>%
as.tibble() %>%
mutate(Month = as.numeric(format(as.Date(DATE, format = "%m/%d/%Y"), "%m"))) %>%
mutate(Season = case_when(Month %in% 10:12 ~ "OND",
Month %in% 1:3 ~ "JFM",
Month %in% 4:6 ~ "AMJ",
TRUE ~ "JAS")) %>%
select(Month, Season, TMAX) %>%
mutate(gr=1) %>%
bind_rows(.,mutate(.,gr=2)) %>%
bind_rows(.,filter(., gr==1) %>% mutate(gr=3)) %>%
mutate(X=factor(ifelse(gr==1, Month, ifelse(gr==2, Season, "Ya")),
levels = c(1:12, "JFM", "AMJ", "JAS", "OND", "Ya"))) %>%
mutate(gr=ifelse(gr==3,2,gr)) %>%
ggplot(aes(x = X, y = TMAX)) +
geom_boxplot(outlier.colour = "grey60", outlier.shape = 1) +
stat_boxplot(geom = "errorbar", width = 0.5) +
stat_summary(fun.y = mean, geom = "point", size = 2, color = "red", shape = 0) +
stat_summary(data=. %>% filter(gr==1),fun.y = mean, geom = "line", color = "red",aes(group = 1)) +
facet_grid(~gr, scales = "free_x", space = "free") +
theme(strip.background = element_blank(),
strip.text = element_blank())
回答2:
This is pretty close to your desired output
library(magrittr)
library(lubridate)
library(tidyverse)
# Create DATE, Month, Year and Season
df1 <- df %>%
as.tibble() %>%
mutate(DATE = as.Date(DATE, "%m/%d/%Y"),
Month = month(DATE),
Year = year(DATE)) %>%
mutate(Season = case_when(Month %in% 10:12 ~ "OND",
Month %in% 1:3 ~ "JFM",
Month %in% 4:6 ~ "AMJ",
TRUE ~ "JAS"
))
df1
#> # A tibble: 419 x 6
#> DATE TMAX TMIN Month Year Season
#> <date> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 2010-01-01 2.93 0.628 1 2010 JFM
#> 2 2010-01-02 0.828 0.824 1 2010 JFM
#> 3 2010-01-03 2.03 0.559 1 2010 JFM
#> 4 2010-01-04 2.65 0.7 1 2010 JFM
#> 5 2010-01-05 2.20 0.393 1 2010 JFM
#> 6 2010-01-06 1.22 0.5 1 2010 JFM
#> 7 2010-01-07 1.22 0.814 1 2010 JFM
#> 8 2010-01-08 1.94 1.06 1 2010 JFM
#> 9 2010-01-09 2.65 0.876 1 2010 JFM
#> 10 2010-01-10 0.638 0.817 1 2010 JFM
#> # ... with 409 more rows
# Calculate Seasonal and annual average
tmax_season <- df1 %>%
group_by(Year, Season) %>%
summarise_if(is.numeric, funs(mean(., na.rm = TRUE))) %>%
ungroup()
tmax_season
#> # A tibble: 5 x 5
#> Year Season TMAX TMIN Month
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 2010 AMJ 0.894 0.463 5
#> 2 2010 JAS 1.08 0.355 7.99
#> 3 2010 JFM 0.479 0.725 2
#> 4 2010 OND 1.18 0.202 11
#> 5 2011 JFM 0.734 0.556 1.43
tmax_annual <- df1 %>%
group_by(Year) %>%
summarise_if(is.numeric, funs(mean(., na.rm = TRUE))) %>%
ungroup() %>%
mutate(Season = "Ya")
tmax_annual
#> # A tibble: 2 x 5
#> Year TMAX TMIN Month Season
#> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 2010 0.912 0.434 6.53 Ya
#> 2 2011 0.734 0.556 1.43 Ya
# Merge together & set the correct order of the season
tmax_ave <- bind_rows(tmax_season, tmax_annual) %>%
mutate(Season = factor(Season, levels = c("JFM", "AMJ", "JAS", "OND", "Ya")))
# Plot mean monthly
plt1 <- ggplot(df1, aes(x = factor(Month), y = TMAX)) +
labs(title = "Changes of Max temperature") +
ylab(expression("T"[max]~(degree*C))) +
xlab("Month") +
scale_y_continuous(limits = c(0, 5)) +
geom_boxplot(outlier.colour = "grey60", outlier.shape = 1) +
stat_boxplot(geom = "errorbar", width = 0.5) +
stat_summary(fun.y = mean, geom = "point", size = 2, color = "red", shape = 0) +
stat_summary(fun.y = mean, geom = "line", aes(group = 1)) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.85))
# Plot Seasonal and annual average
plt2 <- ggplot(tmax_ave, aes(x = Season, y = TMAX)) +
ylab(NULL) +
scale_y_continuous(limits = c(0, 5)) +
geom_boxplot(outlier.colour = "grey60", outlier.shape = 1) +
stat_boxplot(geom = "errorbar", width = 0.5) +
stat_summary(fun.y = mean, geom = "point", size = 2, color = "red", shape = 0) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
axis.text.y = element_blank())
# Combine plots
cowplot::plot_grid(plt1 + theme(plot.margin = margin(t = 5.5, r = 0,
l = 5.5, b = 5.5)),
plt2 + theme(plot.margin = margin(t = 5.5, r = 5.5,
l = 0, b = 5.5)),
ncol = 2,
align = "h",
axis = "tb",
rel_widths = c(2.0, 1.0))
#> Warning: Removed 36 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 36 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 36 rows containing non-finite values (stat_summary).
#> Warning: Removed 36 rows containing non-finite values (stat_summary).
Created on 2018-06-08 by the reprex package (v0.2.0).
Data used:
df <- read.table(text = "DATE TMAX TMIN
1/1/2010 2.927586207 0.627586207
1/2/2010 0.827586207 0.824137931
1/3/2010 2.031034483 0.55862069
1/4/2010 2.648275862 0.7
1/5/2010 2.196551724 0.393103448
1/6/2010 1.220689655 0.5
1/7/2010 1.224137931 0.813793103
1/8/2010 1.94137931 1.05862069
1/9/2010 2.648275862 0.875862069
1/10/2010 0.637931034 0.817241379
1/11/2010 2.444827586 0.827586207
1/12/2010 0.072413793 0.634482759
1/13/2010 -0.320689655 0.755172414
1/14/2010 0.606896552 1.113793103
1/15/2010 1.544827586 1.151724138
1/16/2010 1.017241379 0.775862069
1/17/2010 1.237931034 0.986206897
1/18/2010 1.086206897 0.689655172
1/19/2010 -0.324137931 0.548275862
1/20/2010 -0.410344828 0.451724138
1/21/2010 0.624137931 0.837931034
1/22/2010 -1.424137931 0.789655172
1/23/2010 0.917241379 0.6
1/24/2010 -1.144827586 0.751724138
1/25/2010 0.289655172 0.365517241
1/26/2010 0.313793103 0.110344828
1/27/2010 -1.54137931 0.251724138
1/28/2010 0.162068966 0.251724138
1/29/2010 -0.04137931 0.268965517
1/30/2010 -1.120689655 0.293103448
1/31/2010 1.386206897 0.582758621
2/1/2010 1.062068966 0.906896552
2/2/2010 0.503448276 0.979310345
2/3/2010 0.462068966 0.817241379
2/4/2010 0.455172414 0.817241379
2/5/2010 0.662068966 0.424137931
2/6/2010 0.272413793 0.74137931
2/7/2010 -0.020689655 0.348275862
2/8/2010 0.175862069 -0.05862069
2/9/2010 0.362068966 0.331034483
2/10/2010 0.744827586 0.617241379
2/11/2010 0.427586207 0.796551724
2/12/2010 0.348275862 0.903448276
2/13/2010 0.765517241 0.920689655
2/14/2010 -0.05862069 0.424137931
2/15/2010 0.055172414 0.610344828
2/16/2010 0.372413793 0.424137931
2/17/2010 0.934482759 0.479310345
2/18/2010 0.244827586 0.210344828
2/19/2010 0.831034483 0.486206897
2/20/2010 0.572413793 0.648275862
2/21/2010 0.65862069 0.331034483
2/22/2010 0.34137931 0.393103448
2/23/2010 0.4 0.668965517
2/24/2010 0.7 0.272413793
2/25/2010 1.231034483 0.593103448
2/26/2010 0.648275862 1.103448276
2/27/2010 0.220689655 1.193103448
2/28/2010 -0.14137931 1.027586207
3/1/2010 0.924137931 1.286206897
3/2/2010 -0.224137931 0.127586207
3/3/2010 0.696551724 0.279310345
3/4/2010 -0.513793103 0.237931034
3/5/2010 -0.924137931 0.306896552
3/6/2010 -0.327586207 0.4
3/7/2010 -1.334482759 0.910344828
3/8/2010 -1.627586207 0.84137931
3/9/2010 0.093103448 0.668965517
3/10/2010 0.589655172 0.762068966
3/11/2010 0.282758621 0.751724138
3/12/2010 0.575862069 1.134482759
3/13/2010 -0.003448276 1.403448276
3/14/2010 0.103448276 2.617241379
3/15/2010 0.5 3.389655172
3/16/2010 -0.017241379 1.220689655
3/17/2010 -0.103448276 0.434482759
3/18/2010 -0.110344828 0.362068966
3/19/2010 0.289655172 3.55E-15
3/20/2010 0.603448276 1.044827586
3/21/2010 1.189655172 1.096551724
3/22/2010 0.375862069 0.631034483
3/23/2010 0.475862069 0.831034483
3/24/2010 0.279310345 1.234482759
3/25/2010 -0.034482759 0.85862069
3/26/2010 0.286206897 0.886206897
3/27/2010 0.789655172 0.896551724
3/28/2010 0.789655172 0.734482759
3/29/2010 0.989655172 1.05862069
3/30/2010 0.572413793 0.737931034
3/31/2010 0.982758621 0.455172414
4/1/2010 0.372413793 0.586206897
4/2/2010 0.35862069 1.055172414
4/3/2010 0.35862069 0.855172414
4/4/2010 0.979310345 1.079310345
4/5/2010 0.568965517 0.968965517
4/6/2010 0.572413793 1.075862069
4/7/2010 0.768965517 0.862068966
4/8/2010 0.689655172 0.679310345
4/9/2010 0.979310345 0.572413793
4/10/2010 1.265517241 0.651724138
4/11/2010 0.979310345 0.468965517
4/12/2010 0.262068966 1.048275862
4/13/2010 0.862068966 1.048275862
4/14/2010 0.675862069 0.962068966
4/15/2010 0.975862069 0.872413793
4/16/2010 1.572413793 0.762068966
4/17/2010 2.375862069 0.968965517
4/18/2010 -0.248275862 1.14137931
4/19/2010 1.486206897 0.593103448
4/20/2010 0.868965517 0.762068966
4/21/2010 -0.813793103 0.2
4/22/2010 0.879310345 0.382758621
4/23/2010 1.079310345 0.703448276
4/24/2010 0.851724138 1.044827586
4/25/2010 1.6 0.696551724
4/26/2010 0.586206897 0.486206897
4/27/2010 1.079310345 0.665517241
4/28/2010 1.089655172 0.889655172
4/29/2010 0.886206897 0.596551724
4/30/2010 1.079310345 0.496551724
5/1/2010 1.293103448 0.789655172
5/2/2010 0.796551724 0.427586207
5/3/2010 1.206896552 0.34137931
5/4/2010 0.748275862 0.479310345
5/5/2010 1.006896552 0.355172414
5/6/2010 1.106896552 0.348275862
5/7/2010 0.889655172 0.427586207
5/8/2010 1.020689655 0.262068966
5/9/2010 0.934482759 0.368965517
5/10/2010 1.017241379 0.54137931
5/11/2010 0.817241379 0.644827586
5/12/2010 1.34137931 0.382758621
5/13/2010 0.913793103 0.35862069
5/14/2010 0.903448276 0.25862069
5/15/2010 0.527586207 0.679310345
5/16/2010 0.893103448 0.220689655
5/17/2010 0.806896552 0.35862069
5/18/2010 1.017241379 0.248275862
5/19/2010 0.717241379 -0.117241379
5/20/2010 0.631034483 0.482758621
5/21/2010 0.613793103 0.268965517
5/22/2010 0.331034483 0.193103448
5/23/2010 0.54137931 0.3
5/24/2010 0.720689655 -0.8
5/25/2010 0.789655172 -0.027586207
5/26/2010 0.934482759 -0.903448276
5/27/2010 0.927586207 0.265517241
5/28/2010 1.127586207 0.765517241
5/29/2010 1.027586207 0.565517241
5/30/2010 1.027586207 0.386206897
5/31/2010 0.924137931 -0.175862069
6/1/2010 0.603448276 0.193103448
6/2/2010 0.644827586 0.486206897
6/3/2010 0.727586207 0.375862069
6/4/2010 1.14137931 0.506896552
6/5/2010 1.034482759 0.255172414
6/6/2010 0.631034483 -0.289655172
6/7/2010 0.74137931 -0.265517241
6/8/2010 0.644827586 -0.082758621
6/9/2010 1.14137931 0.793103448
6/10/2010 0.827586207 0.937931034
6/11/2010 1.434482759 0.744827586
6/12/2010 1.248275862 0.979310345
6/13/2010 0.637931034 0.193103448
6/14/2010 0.644827586 -0.065517241
6/15/2010 0.731034483 -0.196551724
6/16/2010 0.951724138 0.120689655
6/17/2010 0.851724138 1.427586207
6/18/2010 0.737931034 -0.124137931
6/19/2010 1.04137931 -0.017241379
6/20/2010 0.734482759 -0.189655172
6/21/2010 0.931034483 0.331034483
6/22/2010 0.851724138 0.962068966
6/23/2010 1.444827586 0.465517241
6/24/2010 1.165517241 -0.24137931
6/25/2010 1.162068966 0.334482759
6/26/2010 0.951724138 0.451724138
6/27/2010 1.051724138 0.331034483
6/28/2010 0.931034483 0.1
6/29/2010 1.424137931 1.034482759
6/30/2010 1.648275862 0.668965517
7/1/2010 1.031034483 0.168965517
7/2/2010 0.331034483 -0.844827586
7/3/2010 0.562068966 -0.617241379
7/4/2010 0.265517241 -0.713793103
7/5/2010 0.448275862 0.079310345
7/6/2010 0.837931034 -0.368965517
7/7/2010 0.662068966 0.455172414
7/8/2010 0.548275862 0.244827586
7/9/2010 0.575862069 0.382758621
7/10/2010 0.651724138 0.210344828
7/11/2010 0.472413793 0.444827586
7/12/2010 0.665517241 0.079310345
7/13/2010 0.851724138 0.686206897
7/14/2010 0.772413793 0.824137931
7/15/2010 1.148275862 -0.034482759
7/16/2010 1.365517241 -0.006896552
7/17/2010 1.155172414 -0.486206897
7/18/2010 1.634482759 0.117241379
7/19/2010 0.868965517 0.637931034
7/20/2010 0.965517241 0.54137931
7/21/2010 1.072413793 0.272413793
7/22/2010 1.134482759 0.110344828
7/23/2010 1.562068966 0.451724138
7/24/2010 0.575862069 1.248275862
7/25/2010 0.648275862 0.751724138
7/26/2010 1.25862069 0.793103448
7/27/2010 1.05862069 1.013793103
7/28/2010 0.75862069 1.055172414
7/29/2010 1.265517241 0.682758621
7/30/2010 0.637931034 0.451724138
7/31/2010 1.251724138 0.817241379
8/1/2010 1.44137931 0.344827586
8/2/2010 0.862068966 0.479310345
8/3/2010 0.610344828 -0.031034483
8/4/2010 0.84137931 0.25862069
8/5/2010 0.748275862 0.065517241
8/6/2010 0.217241379 0.420689655
8/7/2010 1.055172414 0.489655172
8/8/2010 0.717241379 0.520689655
8/9/2010 0.827586207 0.524137931
8/10/2010 1.010344828 0.3
8/11/2010 1.024137931 0.351724138
8/12/2010 1.05862069 0.589655172
8/13/2010 1.975862069 0.05862069
8/14/2010 1.465517241 0.306896552
8/15/2010 1.475862069 0.672413793
8/16/2010 1.534482759 0.55862069
8/17/2010 0.934482759 -0.124137931
8/18/2010 0.906896552 1.103448276
8/19/2010 0.720689655 0.737931034
8/20/2010 1.227586207 0.765517241
8/21/2010 1.051724138 0.982758621
8/22/2010 1.14137931 0.434482759
8/23/2010 1.265517241 0.255172414
8/24/2010 1.355172414 0.651724138
8/25/2010 1.793103448 0.613793103
8/26/2010 1.362068966 0.572413793
8/27/2010 1.262068966 0.362068966
8/28/2010 1.482758621 0.306896552
8/29/2010 1.079310345 0.286206897
8/30/2010 1.286206897 0.610344828
8/31/2010 1.382758621 0.379310345
9/1/2010 1.672413793 0.803448276
9/2/2010 0.817241379 -0.320689655
9/3/2010 1.720689655 0.406896552
9/4/2010 1.296551724 0.203448276
9/5/2010 1.296551724 0.424137931
9/6/2010 1.879310345 0.179310345
9/7/2010 1.579310345 0.393103448
9/8/2010 1.468965517 0.172413793
9/9/2010 1.468965517 0.268965517
9/10/2010 1.182758621 0.482758621
9/11/2010 1.106896552 0.503448276
9/12/2010 1.503448276 0.403448276
9/13/2010 0.903448276 0.2
9/14/2010 1.179310345 0.25862069
9/15/2010 1.175862069 -0.003448276
9/16/2010 1.075862069 0.306896552
9/17/2010 1.082758621 0.317241379
9/18/2010 1.165517241 -0.224137931
9/19/2010 1.551724138 0.734482759
9/20/2010 1.9 -0.272413793
9/21/2010 0.962068966 0.175862069
9/22/2010 0.765517241 0.293103448
9/23/2010 0.951724138 0.444827586
9/24/2010 1.455172414 0.027586207
9/25/2010 0.95862069 0.762068966
9/26/2010 0.693103448 0.703448276
9/27/2010 0.786206897 0.182758621
9/28/2010 1.355172414 0.124137931
9/29/2010 1.451724138 0.513793103
9/30/2010 1.134482759 0.9
10/1/2010 1.420689655 0.613793103
10/2/2010 0.755172414 -0.193103448
10/3/2010 0.148275862 -0.413793103
10/4/2010 0.65862069 0.106896552
10/5/2010 0.872413793 -0.282758621
10/6/2010 0.668965517 -0.279310345
10/7/2010 0.775862069 0.203448276
10/8/2010 0.968965517 -0.010344828
10/9/2010 0.903448276 0.224137931
10/10/2010 0.879310345 -0.086206897
10/11/2010 1.203448276 -0.051724138
10/12/2010 0.575862069 -0.072413793
10/13/2010 0.689655172 0.327586207
10/14/2010 0.603448276 -0.162068966
10/15/2010 1.096551724 0.313793103
10/16/2010 1.2 0.634482759
10/17/2010 1.103448276 0.437931034
10/18/2010 1.520689655 -0.368965517
10/19/2010 1.537931034 0.586206897
10/20/2010 1.1 0.651724138
10/21/2010 1.589655172 0.648275862
10/22/2010 1.027586207 0.162068966
10/23/2010 1.213793103 0.551724138
10/24/2010 1.444827586 0.668965517
10/25/2010 1.227586207 0.579310345
10/26/2010 1.5 0.231034483
10/27/2010 0.762068966 0.293103448
10/28/2010 1.651724138 -0.224137931
10/29/2010 0.010344828 -0.25862069
10/30/2010 1.724137931 0.365517241
10/31/2010 0.589655172 0.424137931
11/1/2010 0.9 -0.472413793
11/2/2010 1.286206897 0.034482759
11/3/2010 1.15862069 0.179310345
11/4/2010 1.568965517 0.403448276
11/5/2010 1.168965517 -0.003448276
11/6/2010 1.472413793 0.4
11/7/2010 1.165517241 0.696551724
11/8/2010 1.572413793 0.472413793
11/9/2010 1.317241379 0.144827586
11/10/2010 0.931034483 -0.137931034
11/11/2010 1.355172414 -0.017241379
11/12/2010 1.055172414 0.4
11/13/2010 1.668965517 0.220689655
11/14/2010 1.889655172 0.810344828
11/15/2010 2.275862069 0.172413793
11/16/2010 1.451724138 0.365517241
11/17/2010 2.544827586 0.44137931
11/18/2010 1.05862069 0.479310345
11/19/2010 0.737931034 0.255172414
11/20/2010 1.162068966 -7.11E-15
11/21/2010 1.562068966 0.193103448
11/22/2010 1.448275862 0.410344828
11/23/2010 1.951724138 0.679310345
11/24/2010 0.248275862 0.282758621
11/25/2010 1.648275862 0.196551724
11/26/2010 2.365517241 -0.172413793
11/27/2010 1.562068966 -0.131034483
11/28/2010 1.462068966 -0.120689655
11/29/2010 1.362068966 0.568965517
11/30/2010 1.293103448 0.710344828
12/1/2010 2.803448276 0.231034483
12/2/2010 4.34137931 0.717241379
12/3/2010 1.234482759 0.003448276
12/4/2010 1.486206897 -0.044827586
12/5/2010 1.162068966 0.220689655
12/6/2010 0.286206897 0.293103448
12/7/2010 1.175862069 0.189655172
12/8/2010 1.124137931 0.248275862
12/9/2010 0.486206897 -0.172413793
12/10/2010 0.996551724 0.420689655
12/11/2010 0.082758621 -0.248275862
12/12/2010 0.724137931 -0.093103448
12/13/2010 0.993103448 -0.006896552
12/14/2010 1.379310345 -0.020689655
12/15/2010 0.275862069 0.189655172
12/16/2010 0.772413793 0.068965517
12/17/2010 1.482758621 0.175862069
12/18/2010 1.15862069 0.548275862
12/19/2010 -0.137931034 0.562068966
12/20/2010 2.006896552 0.037931034
12/21/2010 0.424137931 0.293103448
12/22/2010 0.517241379 0.303448276
12/23/2010 0.817241379 0.027586207
12/24/2010 0.620689655 0.210344828
12/25/2010 1.703448276 0.368965517
12/26/2010 0.034482759 0.3
12/27/2010 0.713793103 0.027586207
12/28/2010 1.131034483 0.068965517
12/29/2010 2.806896552 -0.168965517
12/30/2010 1.117241379 0.296551724
12/31/2010 0.848275862 0.420689655
1/1/2011 1.810344828 0.086206897
1/2/2011 0.248275862 0.168965517
1/3/2011 2.027586207 0.903448276
1/4/2011 1.448275862 0.634482759
1/5/2011 1.013793103 0.186206897
1/6/2011 1.455172414 0.103448276
1/7/2011 0.820689655 0.65862069
1/8/2011 4.762068966 0.010344828
1/9/2011 1.986206897 0.724137931
1/10/2011 0.231034483 0.65862069
1/11/2011 2.220689655 0.45862069
1/12/2011 2.248275862 0.472413793
1/13/2011 1.137931034 0.479310345
1/14/2011 1.555172414 0.510344828
1/15/2011 1.268965517 1.255172414
1/16/2011 1.186206897 1.134482759
1/17/2011 0.772413793 0.713793103
1/18/2011 1.337931034 0.575862069
1/19/2011 -0.351724138 0.417241379
1/20/2011 0.586206897 0.724137931
1/21/2011 1.117241379 0.682758621
1/22/2011 -0.151724138 0.596551724
1/23/2011 -0.420689655 -0.010344828
1/24/2011 0.486206897 0.096551724
1/25/2011 -1.05862069 0.517241379
1/26/2011 0.275862069 0.613793103
1/27/2011 1.317241379 0.165517241
1/28/2011 0.44137931 0.568965517
1/29/2011 -1.120689655 0.362068966
1/30/2011 1.689655172 0.693103448
1/31/2011 -0.337931034 0.889655172
2/1/2011 -1.444827586 0.372413793
2/2/2011 -0.044827586 -0.075862069
2/3/2011 1.231034483 0.593103448
2/4/2011 0.796551724 0.565517241
2/5/2011 0.110344828 0.682758621
2/6/2011 0.84137931 0.496551724
2/7/2011 -0.55862069 0.720689655
2/8/2011 0.862068966 0.720689655
2/9/2011 0.117241379 0.382758621
2/10/2011 0.065517241 0.637931034
2/11/2011 0.8 1.072413793
2/12/2011 0.424137931 0.793103448
2/13/2011 1 0.85862069
2/14/2011 1.006896552 0.85862069
2/15/2011 -0.906896552 0.548275862
2/16/2011 0.324137931 0.503448276
2/17/2011 0.817241379 0.486206897
2/18/2011 0.137931034 0.706896552
2/19/2011 0.396551724 0.84137931
2/20/2011 -0.144827586 1.024137931
2/21/2011 0.9 1.162068966
2/22/2011 1.293103448 0.34137931
2/23/2011 1.603448276 -0.324137931",
header = TRUE)
回答3:
Do you mean something like this?
Note: I assume df
is the data.frame
containing your data.
library(tidyverse);
df %>%
mutate(
month = format(as.Date(DATE, format = "%m/%d/%Y"), "%m")) %>%
ggplot(aes(x = month, y = TMAX)) +
geom_boxplot()
来源:https://stackoverflow.com/questions/50755312/x-axis-with-more-than-one-factor-groups-ggplot