ggpmisc

How to add linear lines to a plot with multiple data sets of a data frame?

空扰寡人 提交于 2020-05-02 05:32:23
问题 I have the following data frame: expected observed group 1: 0.5371429 0.0000 1 2: 1.3428571 1.3736 1 3: 2.6857143 2.4554 1 4: 5.3714286 3.6403 1 5: 0.5294118 0.0000 2 6: 1.3235294 1.1494 2 7: 2.6470588 1.1364 2 8: 5.2941176 4.9774 2 9: 0.5201207 0.0000 3 10: 1.3003018 1.4327 3 11: 2.6006036 2.5918 3 12: 5.2012072 8.0769 3 13: 0.5155039 1.4851 4 14: 1.2887597 1.0638 4 15: 2.5775194 3.1700 4 16: 5.1550388 6.2500 4 17: 0.4976959 0.0000 5 18: 1.2442396 1.2384 5 19: 2.4884793 3.1073 5 20: 4

What is the best way to calculate and display peaks of a ggplot2::geom_density() object?

落爺英雄遲暮 提交于 2020-01-02 10:09:05
问题 I'm trying to find an easy and intuitive way to calculate and display the peaks of a ggplot2::geom_density() object. This blog explains how to do it in base R, but it is a multistep process. But it seems much more intuitive to use the stat_peaks() function of the ggpmisc package. However, when running the code below, I get the error: stat_peaks requires the following missing aesthetics: y library(tidyverse) library(ggpmisc) ggplot(iris, aes(x = Petal.Length)) + geom_density() + stat_peaks

Showing equation of nls model with ggpmisc

♀尐吖头ヾ 提交于 2019-12-23 06:57:47
问题 R package ggpmisc can be used to show equation of lm model and poly model on ggplot2 (See here for reference). Wonder how to show nls model equation results on ggplot2 using ggmisc . Below is my MWE. library(ggpmisc) args <- list(formula = y ~ k * e ^ x, start = list(k = 1, e = 2)) ggplot(mtcars, aes(wt, mpg)) + geom_point() + stat_fit_augment(method = "nls", method.args = args) 回答1: Inspired by the post you linked. Use geom_text to add the label after extracting parameters. nlsFit <- nls

Adding Regression Line Equation and R2 on SEPARATE LINES graph

非 Y 不嫁゛ 提交于 2019-12-18 04:18:20
问题 A few years ago, a poster asked how to add regression line equation and R2 on ggplot graphs at the link below. Adding Regression Line Equation and R2 on graph The top solution was this: lm_eqn <- function(df){ m <- lm(y ~ x, df); eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, list(a = format(coef(m)[1], digits = 2), b = format(coef(m)[2], digits = 2), r2 = format(summary(m)$r.squared, digits = 3))) as.character(as.expression(eq)); } p1 <- p + geom_text(x = 25, y =

Coefficients per facet with output.type=“numeric” in ggpmisc::stat_poly_eq

China☆狼群 提交于 2019-12-13 04:15:07
问题 ggpmisc::stat_poly_eq has an option output.type = "numeric" allowing to get the estimates of the parameters of the fitted model. Below is my attempt to use it with facet_wrap . I get a different R² per facet but the coefficients are the same in the two facets. Do I do something wrong, or is it a bug? library(ggpmisc) set.seed(4321) x <- 1:100 y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4) my.data <- data.frame(x = x, y = y, group = c("A", "B")) my.data[my.data$group=="A

Specifying formula for each facet using stat_poly_eq in ggplot2

痞子三分冷 提交于 2019-12-12 13:23:23
问题 I borrowed this example dataset from here: # Load library library(ggplot2) # Load data data(mtcars) # Plot data p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am) p <- p + geom_smooth(method="lm") print(p) In above code the regression methods and formulae are the same in all facets. If we want to specify formula for facet (or panel) 6 , we have the following code, from here: # Smoothing function with different behaviour depending on the panel custom.smooth <-

Adding Regression Line Equation and R2 on SEPARATE LINES graph

北战南征 提交于 2019-11-29 04:36:41
A few years ago, a poster asked how to add regression line equation and R2 on ggplot graphs at the link below. Adding Regression Line Equation and R2 on graph The top solution was this: lm_eqn <- function(df){ m <- lm(y ~ x, df); eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, list(a = format(coef(m)[1], digits = 2), b = format(coef(m)[2], digits = 2), r2 = format(summary(m)$r.squared, digits = 3))) as.character(as.expression(eq)); } p1 <- p + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE) I am using this code and it works great. However, I was

ggplot2: Problem with x axis when adding regression line equation on each facet

旧时模样 提交于 2019-11-28 12:53:38
Based on the example here Adding Regression Line Equation and R2 on graph , I am struggling to include the regression line equation for my model in each facet. However, I don't figure why is changing the limits of my x axis. library(ggplot2) library(reshape2) df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6), M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1)) df <- melt(df, id = c("year")) ggplot(data = df, mapping = aes(x = year, y = value)) + geom_point() + scale_x_continuous() + stat_smooth_func(geom = 'text', method = 'lm', hjust = 0, parse = T) + geom_smooth(method = 'lm',

ggplot2: Problem with x axis when adding regression line equation on each facet

左心房为你撑大大i 提交于 2019-11-27 07:16:29
问题 Based on the example here Adding Regression Line Equation and R2 on graph, I am struggling to include the regression line equation for my model in each facet. However, I don't figure why is changing the limits of my x axis. library(ggplot2) library(reshape2) df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6), M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1)) df <- melt(df, id = c("year")) ggplot(data = df, mapping = aes(x = year, y = value)) + geom_point() + scale_x_continuous()

Add regression line equation and R^2 on graph

为君一笑 提交于 2019-11-25 21:47:39
问题 I wonder how to add regression line equation and R^2 on the ggplot . My code is: library(ggplot2) df <- data.frame(x = c(1:100)) df$y <- 2 + 3 * df$x + rnorm(100, sd = 40) p <- ggplot(data = df, aes(x = x, y = y)) + geom_smooth(method = \"lm\", se=FALSE, color=\"black\", formula = y ~ x) + geom_point() p Any help will be highly appreciated. 回答1: Here is one solution # GET EQUATION AND R-SQUARED AS STRING # SOURCE: https://groups.google.com/forum/#!topic/ggplot2/1TgH-kG5XMA lm_eqn <- function