![]() ![]() Stat_poly_eq(formula = y ~ poly(x, 2, raw = TRUE), use_label("eq")) + Stat_poly_line(formula = y ~ poly(x, 2, raw = TRUE)) + # adding separate labels with equation and R2 # assembling a single label with R2, its confidence interval, and n # assembling a single label with equation, adjusted R2, F-value, n, P-value # assembling a single label with equation and R2 # using default formula, label and methods ![]() I have omitted in all code examples the additional arguments to stat_poly_line() as they are irrelevant to the question of adding labels. In the examples I use stat_poly_line() instead of stat_smooth() as it has the same defaults as stat_poly_eq() for method and formula. Although use of aes() and after_stat() remains unchanged, use_label() makes coding of mappings and assembly of labels simpler. The main change is the assembly of labels and their mapping using function use_label() added in 'ggpmisc' (=0.5.0). I have updated this answer for 'ggpmisc' (>= 0.5.0) and 'ggplot2' (>= 3.4.0) on. Each eq stat has a matching line drawing stat.) ![]() (Statistics stat_ma_eq() and stat_quant_eq() work similarly and support major axis regression and quantile regression, respectively. shape = c( rep( 21, 5), NA) ) ) ) + # Minimal grid theme that only draws horizontal lines theme_minimal_hgrid( 12, rel_small = 1) + # Customize aspects of the legend theme( legend.position = "top", legend.justification = "right", legend.text = element_text( size = 9), stat_poly_eq() in my package ggpmisc makes it possible to add text labels to plots based on a linear model fit. nrow = 1, override.aes = list( # 0 means no line, 1 is a solid line # The result is 5 keys with no line and 1 with a line linetype = c( rep( 0, 5), 1), # Now, 5 keys with the marker number 21 (the one used in the plot) # and 1 key without this marker. ) + # Add auto-positioned text geom_text_repel( aes( label = label), color = "black", size = 9 /.pt, # font size 9 pt point.padding = 0.1, box.padding = 0.6, = 0, max.overlaps = 1000, seed = 7654 # For reproducibility reasons ) + scale_color_manual( name = NULL, # it's one way to omit the legend title values = darken(region_cols, 0.3) # dot borders are a darker than the fill ) + scale_fill_manual( name = NULL, values = region_cols ) + # Add labels and customize axes scale_x_continuous( name = "Corruption Perceptions Index, 2015 (100 = least corrupt)", limits = c( 10, 95), breaks = c( 20, 40, 60, 80, 100), expand = c( 0, 0) # This removes the default padding on the ends of the axis ) + scale_y_continuous( name = "Human Development Index, 2015 \n (1.0 = most developed)", limits = c( 0.3, 1.05), breaks = c( 0.2, 0.4, 0.6, 0.8, 1.0), # Manually set axis breaks expand = c( 0, 0) ) + # Override default legend appearance guides( color = guide_legend( # All keys go in the same row. geom_smooth( aes( color = "y ~ log(x)", fill = "y ~ log(x)"), method = "lm", formula = y ~ log(x), se = FALSE, # Plot the line only (without confidence bands) fullrange = TRUE # The fit spans the full range of the horizontal axis ) + geom_point( aes( color = region, fill = region), size = 2.5, alpha = 0.5, shape = 21 # This is a dot with both border (color) and fill. # Okabe Ito colors # The last color is used for the regression fit. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |