Try running str(**plot here**)
and taking a look at the differences.
library(ggplot2)
#> Loading required package: ggplot2
set.seed(123)
df <- data.frame(
id = rep(1:15, each = 4),
x = c(c(0.25,0.5,0.75,1), each = 4),
y = NA,
panel = 5,
var1 = 0,
var2 = 0
)
df$y <- df$id* df$x + rnorm(30, 0, 0.5)
df$panel[which(df$id >= 7)] <- 10
df$var1[which(df$id >= 10)] <- 1
df$var2[which(df$id %in% c(5,8,9))] <- 1
# Case 1: symbol/color by df$var1; df$var1 is numeric - spline lines work
c1 <- ggplot(data = df) +
aes_string(x = 'x',
y = 'y',
group = 'panel',
colour = 'var1') +
geom_point() +
geom_smooth(method = 'loess',
size = 1.5,
linetype = 'solid',
se = FALSE,
na.rm = TRUE,
span = 2/3) +
labs(title = 'case 1') +
facet_wrap('panel', scales = 'fixed', nrow = 2, ncol = 1) +
scale_colour_gradientn(colors = rainbow(4))
# Case 2: symbol/color by df$var1; df$var1 is factor - spline lines work
df$var1 <- factor(df$var1)
gplot <- ggplot(data = df) +
aes_string(x = 'x',
y = 'y',
group = 'panel',
colour = 'var1',
shape = 'var1') +
geom_point() +
geom_smooth(method = 'loess',
size = 1.5,
linetype = 'solid',
se = FALSE,
na.rm = TRUE,
span = 2/3)+
facet_wrap('panel', scales = 'fixed', nrow = 2, ncol = 1)
c2 <- gplot + labs(title = 'case 2')
str(c2)
#> List of 9
#> $ data :'data.frame': 60 obs. of 6 variables:
#> ..$ id : int [1:60] 1 1 1 1 2 2 2 2 3 3 ...
#> ..$ x : num [1:60] 0.25 0.5 0.75 1 4 0.25 0.5 0.75 1 4 ...
#> ..$ y : num [1:60] -0.0302 0.3849 1.5294 1.0353 8.0646 ...
#> ..$ panel: num [1:60] 5 5 5 5 5 5 5 5 5 5 ...
#> ..$ var1 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
#> ..$ var2 : num [1:60] 0 0 0 0 0 0 0 0 0 0 ...
#> $ layers :List of 2
#> ..$ :Classes 'LayerInstance', 'Layer', 'ggproto', 'gg' <ggproto object: Class LayerInstance, Layer, gg>
#> aes_params: list
#> compute_aesthetics: function
#> compute_geom_1: function
#> compute_geom_2: function
#> compute_position: function
#> compute_statistic: function
#> data: waiver
#> draw_geom: function
#> finish_statistics: function
#> geom: <ggproto object: Class GeomPoint, Geom, gg>
#> aesthetics: function
#> default_aes: uneval
#> draw_group: function
#> draw_key: function
#> draw_layer: function
#> draw_panel: function
#> extra_params: na.rm
#> handle_na: function
#> non_missing_aes: size shape colour
#> optional_aes:
#> parameters: function
#> required_aes: x y
#> setup_data: function
#> use_defaults: function
#> super: <ggproto object: Class Geom, gg>
#> geom_params: list
#> inherit.aes: TRUE
#> layer_data: function
#> map_statistic: function
#> mapping: NULL
#> position: <ggproto object: Class PositionIdentity, Position, gg>
#> compute_layer: function
#> compute_panel: function
#> required_aes:
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Position, gg>
#> print: function
#> show.legend: NA
#> stat: <ggproto object: Class StatIdentity, Stat, gg>
#> aesthetics: function
#> compute_group: function
#> compute_layer: function
#> compute_panel: function
#> default_aes: uneval
#> extra_params: na.rm
#> finish_layer: function
#> non_missing_aes:
#> parameters: function
#> required_aes:
#> retransform: TRUE
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Stat, gg>
#> stat_params: list
#> subset: NULL
#> super: <ggproto object: Class Layer, gg>
#> ..$ :Classes 'LayerInstance', 'Layer', 'ggproto', 'gg' <ggproto object: Class LayerInstance, Layer, gg>
#> aes_params: list
#> compute_aesthetics: function
#> compute_geom_1: function
#> compute_geom_2: function
#> compute_position: function
#> compute_statistic: function
#> data: waiver
#> draw_geom: function
#> finish_statistics: function
#> geom: <ggproto object: Class GeomSmooth, Geom, gg>
#> aesthetics: function
#> default_aes: uneval
#> draw_group: function
#> draw_key: function
#> draw_layer: function
#> draw_panel: function
#> extra_params: na.rm
#> handle_na: function
#> non_missing_aes:
#> optional_aes: ymin ymax
#> parameters: function
#> required_aes: x y
#> setup_data: function
#> use_defaults: function
#> super: <ggproto object: Class Geom, gg>
#> geom_params: list
#> inherit.aes: TRUE
#> layer_data: function
#> map_statistic: function
#> mapping: NULL
#> position: <ggproto object: Class PositionIdentity, Position, gg>
#> compute_layer: function
#> compute_panel: function
#> required_aes:
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Position, gg>
#> print: function
#> show.legend: NA
#> stat: <ggproto object: Class StatSmooth, Stat, gg>
#> aesthetics: function
#> compute_group: function
#> compute_layer: function
#> compute_panel: function
#> default_aes: uneval
#> extra_params: na.rm
#> finish_layer: function
#> non_missing_aes:
#> parameters: function
#> required_aes: x y
#> retransform: TRUE
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Stat, gg>
#> stat_params: list
#> subset: NULL
#> super: <ggproto object: Class Layer, gg>
#> $ scales :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
#> add: function
#> clone: function
#> find: function
#> get_scales: function
#> has_scale: function
#> input: function
#> n: function
#> non_position_scales: function
#> scales: list
#> super: <ggproto object: Class ScalesList, gg>
#> $ mapping :List of 5
#> ..$ group : symbol panel
#> ..$ colour: symbol var1
#> ..$ shape : symbol var1
#> ..$ x : symbol x
#> ..$ y : symbol y
#> $ theme : list()
#> $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
#> aspect: function
#> default: TRUE
#> distance: function
#> expand: TRUE
#> is_linear: function
#> labels: function
#> limits: list
#> modify_scales: function
#> range: function
#> render_axis_h: function
#> render_axis_v: function
#> render_bg: function
#> render_fg: function
#> setup_data: function
#> setup_layout: function
#> setup_panel_params: function
#> setup_params: function
#> transform: function
#> super: <ggproto object: Class CoordCartesian, Coord, gg>
#> $ facet :Classes 'FacetWrap', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetWrap, Facet, gg>
#> compute_layout: function
#> draw_back: function
#> draw_front: function
#> draw_labels: function
#> draw_panels: function
#> finish_data: function
#> init_scales: function
#> map_data: function
#> params: list
#> setup_data: function
#> setup_params: function
#> shrink: TRUE
#> train_scales: function
#> vars: function
#> super: <ggproto object: Class FacetWrap, Facet, gg>
#> $ plot_env :<environment: R_GlobalEnv>
#> $ labels :List of 6
#> ..$ title : chr "case 2"
#> ..$ group : chr "panel"
#> ..$ colour: chr "var1"
#> ..$ shape : chr "var1"
#> ..$ x : chr "x"
#> ..$ y : chr "y"
#> - attr(*, "class")= chr [1:2] "gg" "ggplot"
# Case 3: symbol/color by df$var1; df$var1 is factor + custom scale - spline lines do not work
c3 <- gplot + labs(title = 'case 3') + scale_colour_manual(values = c('blue', "red"))
str(c3)
#> List of 9
#> $ data :'data.frame': 60 obs. of 6 variables:
#> ..$ id : int [1:60] 1 1 1 1 2 2 2 2 3 3 ...
#> ..$ x : num [1:60] 0.25 0.5 0.75 1 4 0.25 0.5 0.75 1 4 ...
#> ..$ y : num [1:60] -0.0302 0.3849 1.5294 1.0353 8.0646 ...
#> ..$ panel: num [1:60] 5 5 5 5 5 5 5 5 5 5 ...
#> ..$ var1 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
#> ..$ var2 : num [1:60] 0 0 0 0 0 0 0 0 0 0 ...
#> $ layers :List of 2
#> ..$ :Classes 'LayerInstance', 'Layer', 'ggproto', 'gg' <ggproto object: Class LayerInstance, Layer, gg>
#> aes_params: list
#> compute_aesthetics: function
#> compute_geom_1: function
#> compute_geom_2: function
#> compute_position: function
#> compute_statistic: function
#> data: waiver
#> draw_geom: function
#> finish_statistics: function
#> geom: <ggproto object: Class GeomPoint, Geom, gg>
#> aesthetics: function
#> default_aes: uneval
#> draw_group: function
#> draw_key: function
#> draw_layer: function
#> draw_panel: function
#> extra_params: na.rm
#> handle_na: function
#> non_missing_aes: size shape colour
#> optional_aes:
#> parameters: function
#> required_aes: x y
#> setup_data: function
#> use_defaults: function
#> super: <ggproto object: Class Geom, gg>
#> geom_params: list
#> inherit.aes: TRUE
#> layer_data: function
#> map_statistic: function
#> mapping: NULL
#> position: <ggproto object: Class PositionIdentity, Position, gg>
#> compute_layer: function
#> compute_panel: function
#> required_aes:
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Position, gg>
#> print: function
#> show.legend: NA
#> stat: <ggproto object: Class StatIdentity, Stat, gg>
#> aesthetics: function
#> compute_group: function
#> compute_layer: function
#> compute_panel: function
#> default_aes: uneval
#> extra_params: na.rm
#> finish_layer: function
#> non_missing_aes:
#> parameters: function
#> required_aes:
#> retransform: TRUE
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Stat, gg>
#> stat_params: list
#> subset: NULL
#> super: <ggproto object: Class Layer, gg>
#> ..$ :Classes 'LayerInstance', 'Layer', 'ggproto', 'gg' <ggproto object: Class LayerInstance, Layer, gg>
#> aes_params: list
#> compute_aesthetics: function
#> compute_geom_1: function
#> compute_geom_2: function
#> compute_position: function
#> compute_statistic: function
#> data: waiver
#> draw_geom: function
#> finish_statistics: function
#> geom: <ggproto object: Class GeomSmooth, Geom, gg>
#> aesthetics: function
#> default_aes: uneval
#> draw_group: function
#> draw_key: function
#> draw_layer: function
#> draw_panel: function
#> extra_params: na.rm
#> handle_na: function
#> non_missing_aes:
#> optional_aes: ymin ymax
#> parameters: function
#> required_aes: x y
#> setup_data: function
#> use_defaults: function
#> super: <ggproto object: Class Geom, gg>
#> geom_params: list
#> inherit.aes: TRUE
#> layer_data: function
#> map_statistic: function
#> mapping: NULL
#> position: <ggproto object: Class PositionIdentity, Position, gg>
#> compute_layer: function
#> compute_panel: function
#> required_aes:
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Position, gg>
#> print: function
#> show.legend: NA
#> stat: <ggproto object: Class StatSmooth, Stat, gg>
#> aesthetics: function
#> compute_group: function
#> compute_layer: function
#> compute_panel: function
#> default_aes: uneval
#> extra_params: na.rm
#> finish_layer: function
#> non_missing_aes:
#> parameters: function
#> required_aes: x y
#> retransform: TRUE
#> setup_data: function
#> setup_params: function
#> super: <ggproto object: Class Stat, gg>
#> stat_params: list
#> subset: NULL
#> super: <ggproto object: Class Layer, gg>
#> $ scales :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
#> add: function
#> clone: function
#> find: function
#> get_scales: function
#> has_scale: function
#> input: function
#> n: function
#> non_position_scales: function
#> scales: list
#> super: <ggproto object: Class ScalesList, gg>
#> $ mapping :List of 5
#> ..$ group : symbol panel
#> ..$ colour: symbol var1
#> ..$ shape : symbol var1
#> ..$ x : symbol x
#> ..$ y : symbol y
#> $ theme : list()
#> $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
#> aspect: function
#> default: TRUE
#> distance: function
#> expand: TRUE
#> is_linear: function
#> labels: function
#> limits: list
#> modify_scales: function
#> range: function
#> render_axis_h: function
#> render_axis_v: function
#> render_bg: function
#> render_fg: function
#> setup_data: function
#> setup_layout: function
#> setup_panel_params: function
#> setup_params: function
#> transform: function
#> super: <ggproto object: Class CoordCartesian, Coord, gg>
#> $ facet :Classes 'FacetWrap', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetWrap, Facet, gg>
#> compute_layout: function
#> draw_back: function
#> draw_front: function
#> draw_labels: function
#> draw_panels: function
#> finish_data: function
#> init_scales: function
#> map_data: function
#> params: list
#> setup_data: function
#> setup_params: function
#> shrink: TRUE
#> train_scales: function
#> vars: function
#> super: <ggproto object: Class FacetWrap, Facet, gg>
#> $ plot_env :<environment: R_GlobalEnv>
#> $ labels :List of 6
#> ..$ title : chr "case 3"
#> ..$ group : chr "panel"
#> ..$ colour: chr "var1"
#> ..$ shape : chr "var1"
#> ..$ x : chr "x"
#> ..$ y : chr "y"
#> - attr(*, "class")= chr [1:2] "gg" "ggplot"