Home > Mobile >  Enter a point's value on the graph
Enter a point's value on the graph

Time:09-27

The code below generates a graph with dots and a line. Also, it has a red dot that is right on the axis. I would like to input the value corresponding to that point in red on the graph. How can I do this?

Thanks!

library(dplyr)
library(lubridate)
library(tidyverse)

df1 <- structure(
  list(date1 = c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
                 "2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-04-02","2021-04-03","2021-04-08","2021-04-09","2021-04-10","2021-07-01","2021-07-02","2021-07-03"),
       Week= c("Friday","Saturday","Thursday","Friday","Saturday","Thursday","Friday","Monday"),
       DR01 = c(14,11,14,13,13,14,13,16), DR02= c(14,12,16,17,13,12,17,14),DR03= c(19,15,14,13,13,12,11,15),
       DR04 = c(15,14,13,13,16,12,11,19),DR05 = c(15,14,15,13,16,12,11,19),
       DR06 = c(21,14,13,13,15,16,17,18),DR07 = c(12,15,14,14,19,14,17,18)),
  class = "data.frame", row.names = c(NA, -8L))

#Generate graph

dmda<-"2021-07-01"

datas<-df1 %>%
  filter(date2 == ymd(dmda)) %>%
  summarize(across(starts_with("DR"), sum)) %>%
  pivot_longer(everything(), names_pattern = "DR(. )", values_to = "val") %>%
  mutate(name = as.numeric(name))
colnames(datas)<-c("Days","Numbers")

plot(Numbers ~ Days, xlim=c(0,8), ylim=c(0,20), data = datas,xaxs='i')
model <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0), data = datas)
new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
new.data <- rbind(0, new.data)
lines(new.data$Days, predict(model, newdata=new.data))
points(0, coef(model)[2], col="red", pch=19, cex=1.2, xpd=TRUE)

enter image description here

CodePudding user response:

You can use the function text.

plot(Numbers ~ Days, xlim=c(0,8), ylim=c(0,20), data = datas,xaxs='i')
model <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0), data = datas)
new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
new.data <- rbind(0, new.data)
lines(new.data$Days, predict(model, newdata=new.data))
points(0, coef(model)[2], col="red", pch=19, cex=1.2, xpd=TRUE)

text

text(.2,13,round(coef(model)[2],2))

enter image description here

legend

An alternative is to use legend

legend(
  "bottomright",
  legend = round(coef(model)[2],2),
  col = "red",
  pch = 20,
  bty = "n"
)

enter image description here

CodePudding user response:

I think the text suggestion by @Vinícius Félix is correct. To make the y-position dynamic you can use coef(model)[2] 1.

plot(Numbers ~ Days, xlim=c(0,8), ylim=c(0,20), data = datas,xaxs='i')
model <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0), data = datas)
new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
new.data <- rbind(0, new.data)
lines(new.data$Days, predict(model, newdata=new.data))
tmp <- coef(model)[2]
points(0, tmp, col="red", pch=19, cex=1.2, xpd=TRUE)

text(.5,tmp   1,round(tmp,2))

enter image description here

  •  Tags:  
  • r
  • Related