Chris Brunsdon
ggplot
ggplot
library(tidyverse) buoy <- read_csv('weather_buoy_daily.csv') head(buoy)
## # A tibble: 6 x 11 ## date station_id AtmosphericPres… WindSpeed Gust WaveHeight WavePeriod ## <date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 2001-02-06 M1 976. 14.8 23.3 NA NA ## 2 2001-02-07 M1 992. 15.7 24.7 NA NA ## 3 2001-02-08 M1 1007. 8.69 15.3 NA NA ## 4 2001-02-09 M1 1009. 25.5 35.7 NA NA ## 5 2001-02-10 M1 1004. 19.4 27.9 NA NA ## 6 2001-02-11 M1 1012. 17.9 27.0 NA NA ## # … with 4 more variables: AirTemperature <dbl>, DewPoint <dbl>, ## # SeaTemperature <dbl>, RelativeHumidity <dbl>
@marineinstitute
AtmosphericPressure
AirTemperature
DewPoint
WindSpeed
Gust
SeaTemperature
WavePeriod
WaveHeight
RelativeHumidity
ggplot
explorationggplot(buoy,aes(x=AirTemperature,y=AtmosphericPressure)) + geom_point()
ggplot(buoy,aes(...))
buoy
aes(x=...,y=...,...)
x
is AirTemperature
and y
is AtmosphericPressure
geom_point()
x
and y
aesthetics to a visual entity (here to points)+
is used here to build up the specification of the graphic.ggplot(buoy,aes(x=AirTemperature,y=AtmosphericPressure)) + geom_density_2d_filled()
ggplot(buoy,aes(x=AirTemperature,y=AtmosphericPressure)) + geom_point(size=0.3) + geom_density_2d_filled(alpha=0.5)
Painter | Designer | |
---|---|---|
Code Type | Standard R |
ggplot
|
Method | Issue commands directly | Specify design of graphic |
Example |
plot(xvar,yvar) abline(lm(y~x))
|
ggplot(dat,aes(x=xvar,y=yvar))+geom_point()+ geom_smooth(method='lm')
|
Approach |
Think about what to draw on the graph as a set of steps |
Think about how you want variables to be represented in the graphic |
ggplot(buoy,aes(x=date,y=AirTemperature)) + geom_line()
- Note - This incorporates figures for all buoys.
ggplot(buoy,aes(x=date,y=AirTemperature, col=station_id)) + geom_point()
## Warning: Removed 169 rows containing missing values (geom_point).
ggplot(buoy,aes(x=date,y=AirTemperature)) + geom_line() + facet_wrap(~station_id)
ggplot(buoy,aes(x=date,y=station_id)) + geom_point()
y
aesthetic was categorical, not a numberx
aesthetic is a <date>
not an ordinary numberhead(buoy)
## # A tibble: 6 x 11 ## date station_id AtmosphericPres… WindSpeed Gust WaveHeight WavePeriod ## <date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 2001-02-06 M1 976. 14.8 23.3 NA NA ## 2 2001-02-07 M1 992. 15.7 24.7 NA NA ## 3 2001-02-08 M1 1007. 8.69 15.3 NA NA ## 4 2001-02-09 M1 1009. 25.5 35.7 NA NA ## 5 2001-02-10 M1 1004. 19.4 27.9 NA NA ## 6 2001-02-11 M1 1012. 17.9 27.0 NA NA ## # … with 4 more variables: AirTemperature <dbl>, DewPoint <dbl>, ## # SeaTemperature <dbl>, RelativeHumidity <dbl>
<chr>
: Character (can be used for categorical)<dbl>
: Numeric (from double precision)ggplot(buoy,aes(x=WindSpeed,y=station_id)) + geom_boxplot()
## Warning: Removed 2144 rows containing non-finite values (stat_boxplot).
ggplot(buoy,aes(x=AirTemperature,y=SeaTemperature)) + geom_point(size=0.2) + geom_smooth()
ggplot(buoy,aes(x=AirTemperature,y=SeaTemperature)) + geom_point(size=0.2) + geom_smooth(method='lm')
ggplot(buoy,aes(x=AirTemperature,y=SeaTemperature,group=station_id)) + geom_point(size=0.2) + geom_smooth(method='lm')
ggplot(buoy,aes(x=AirTemperature, y=SeaTemperature,group=station_id,col=station_id)) + geom_point(size=0.2) + geom_smooth(method='lm')
ggplot(buoy,aes(x=AirTemperature, y=SeaTemperature)) + geom_point(size=0.2) + geom_smooth(method='lm') + facet_wrap(~station_id)
ggplot(buoy,aes(x=AirTemperature,y=SeaTemperature)) + geom_hex() + facet_wrap(~station_id)
hexbin
package firstggplot(buoy,aes(x=AirTemperature,y=SeaTemperature)) + geom_hex() + facet_wrap(~station_id) + scale_fill_viridis_c()
ggplot(buoy,aes(x=AirTemperature,y=SeaTemperature)) + geom_hex() + facet_wrap(~station_id) + scale_x_reverse()
scale
but
x
and y
ggplot(buoy,aes(x=AirTemperature,y=SeaTemperature)) + geom_hex() + facet_wrap(~station_id) + coord_equal()
coord
characteristiclibrary(lubridate) buoy <- buoy %>% mutate(Month=month(date,label=TRUE)) ggplot(buoy,aes(y=AirTemperature,x=Month,group=Month) )+ geom_boxplot() + coord_polar()
library(lubridate) buoy <- buoy %>% mutate(Month=month(date,label=TRUE)) ggplot(buoy,aes(y=AirTemperature,x=Month,group=Month)) + geom_boxplot() + coord_polar() + scale_y_reverse()
ggplot(buoy,aes(y=AirTemperature,x=Month,group=Month)) + geom_boxplot() + labs(x="Month of Year",y="Temperature (°C)",title="Air Temperature")
ggplot(buoy,aes(y=AirTemperature,x=Month,group=Month)) + geom_boxplot() + labs(x="Month of Year",y="Temperature (°C)",title="Air Temperature") + theme_minimal()
ggthemes
ggthemes
library(ggthemes) ggplot(buoy,aes(y=AirTemperature,x=Month,group=Month)) + geom_boxplot() + labs(x="Month of Year",y="Temperature (°C)",title="Air Temperature") + theme_economist()
buoy %>% select(date,station_id,WindSpeed,Month)
## # A tibble: 26,352 x 4 ## date station_id WindSpeed Month ## <date> <chr> <dbl> <ord> ## 1 2001-02-06 M1 14.8 Feb ## 2 2001-02-07 M1 15.7 Feb ## 3 2001-02-08 M1 8.69 Feb ## 4 2001-02-09 M1 25.5 Feb ## 5 2001-02-10 M1 19.4 Feb ## 6 2001-02-11 M1 17.9 Feb ## 7 2001-02-12 M1 9.00 Feb ## 8 2001-02-13 M1 18.1 Feb ## 9 2001-02-14 M1 20.5 Feb ## 10 2001-02-15 M1 6.79 Feb ## # … with 26,342 more rows
<dbl>
: Ordinary numeric variable eg. speed, height, etc.<date>
: Date variable<chr>
: Categorical variable eg Place name, station_id, colour<ord>
: Categorical variable with an implicit order (eg month, day)ggplot
Considers types when choosing format of plot<dttm>
(date+time)New general ideas
New techniques
ggplot
aes
geom
, scale
, coord
, lab
, theme
Practical issues
ggplot
syntaxNext lecture - Time Series
This link may be useful for some of the ideas.