Dear all I know I have seen the answer somewhere but I am not able to find it. Please help> plot(1,1) > res <- shapiro.test(rnorm(100)) > resShapiro-Wilk normality test data: rnorm(100) W = 0.98861, p-value = 0.5544 I would like to add whole res object to the plot. I can do it one by one> text(locator(1), res$method) > text(locator(1), as.character(res$p.value))... But it is quite inconvenient I could find some way in ggplot world but not in plain plot world. Best regards Petr
res is a list of class "htest" . You can only add text strings to a plot via text(). I don't know what ggplot does. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Sep 16, 2021 at 7:22 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:> > Dear all > > I know I have seen the answer somewhere but I am not able to find it. Please > help > > > plot(1,1) > > res <- shapiro.test(rnorm(100)) > > res > > Shapiro-Wilk normality test > > data: rnorm(100) > W = 0.98861, p-value = 0.5544 > > I would like to add whole res object to the plot. > > I can do it one by one > > text(locator(1), res$method) > > text(locator(1), as.character(res$p.value)) > ... > But it is quite inconvenient > > I could find some way in ggplot world but not in plain plot world. > > Best regards > Petr > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi! Maybe with this: text(x=0.6, y=1.2, paste0(capture.output(res), collapse="\n"), adj=0) HTH, Kimmo to, 2021-09-16 kello 14:12 +0000, PIKAL Petr kirjoitti:> Virhe vahvistaessa allekirjoitusta: Virhe tulkittaessa > Dear all > > I know I have seen the answer somewhere but I am not able to find it. > Please > help > > > plot(1,1) > > res <- shapiro.test(rnorm(100)) > > res > > Shapiro-Wilk normality test > > data: rnorm(100) > W = 0.98861, p-value = 0.5544 > > I would like to add whole res object to the plot. > > I can do it one by one > > text(locator(1), res$method) > > text(locator(1), as.character(res$p.value)) > ... > But it is quite inconvenient > > I could find some way in ggplot world but not in plain plot world. > > Best regards > Petr > > ------=_NextPart_000_00C9_01D7AB15.A6E04EE0-- > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Petr, The hard part is the names for the data frame that addtable2plot requires: set.seed(753) res <- shapiro.test(rnorm(100)) library(plotrix) plot(0,0,type="n",axes=FALSE) addtable2plot(0,0,data.frame(element=names(res)[1:2], value=round(as.numeric(res[1:2]),3)),xjust=0.5, title=res$method) There is probably a way to get blank names with data.frame(), but I gave up. Jim On Fri, Sep 17, 2021 at 12:22 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:> > Dear all > > I know I have seen the answer somewhere but I am not able to find it. Please > help > > > plot(1,1) > > res <- shapiro.test(rnorm(100)) > > res > > Shapiro-Wilk normality test > > data: rnorm(100) > W = 0.98861, p-value = 0.5544 > > I would like to add whole res object to the plot. > > I can do it one by one > > text(locator(1), res$method) > > text(locator(1), as.character(res$p.value)) > ... > But it is quite inconvenient > > I could find some way in ggplot world but not in plain plot world. > > Best regards > Petr > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hello,
*.test functions in base R return a list of class "htest", with its
own
print method.
The method text.htest for objects of class "htest" below is a hack. I
adapted the formating part of the code of print.htest to plot text().
I find it maybe too complicated but it seems to work.
Warning: Not debugged at all.
text.htest <- function (ht, x, y = NULL, digits =
getOption("digits"),
prefix = "", adj = NULL, ...) {
out <- list()
i_out <- 1L
out[[i_out]] <- paste(strwrap(ht$method, prefix = prefix), sep =
"\n")
i_out <- i_out + 1L
out[[i_out]] <- paste0("data: ", ht$data.name)
stat_line <- NULL
i_stat_line <- 0L
if (!is.null(ht$statistic)) {
i_stat_line <- i_stat_line + 1L
stat_line[[i_stat_line]] <- paste(names(ht$statistic), "=",
format(ht$statistic, digits =
max(1L, digits - 2L)))
}
if (!is.null(ht$parameter)) {
i_stat_line <- i_stat_line + 1L
stat_line[[i_stat_line]] <- paste(names(ht$parameter), "=",
format(ht$parameter, digits =
max(1L, digits - 2L)))
}
if (!is.null(ht$p.value)) {
fp <- format.pval(ht$p.value, digits = max(1L, digits - 3L))
i_stat_line <- i_stat_line + 1L
stat_line[[i_stat_line]] <- paste("p-value",
if (startsWith(fp, "<")) fp
else
paste("=", fp))
}
if(!is.null(stat_line)){
i_out <- i_out + 1L
#out[[i_out]] <- strwrap(paste(stat_line, collapse = ", "))
out[[i_out]] <- paste(stat_line, collapse = ", ")
}
if (!is.null(ht$alternative)) {
alt <- NULL
i_alt <- 1L
alt[[i_alt]] <- "alternative hypothesis: "
if (!is.null(ht$null.value)) {
if (length(ht$null.value) == 1L) {
alt.char <- switch(ht$alternative, two.sided = "not equal
to",
less = "less than", greater =
"greater than")
i_alt <- i_alt + 1L
alt[[i_alt]] <- paste0("true ", names(ht$null.value),
" is ",
alt.char,
" ", ht$null.value)
}
else {
i_alt <- i_alt + 1L
alt[[i_alt]] <- paste0(ht$alternative, "\nnull values:\n")
}
}
else {
i_alt <- i_alt + 1L
alt[[i_alt]] <- ht$alternative
}
i_out <- i_out + 1L
out[[i_out]] <- paste(alt, collapse = " ")
}
if (!is.null(ht$conf.int)) {
i_out <- i_out + 1L
out[[i_out]] <- paste0(format(100 * attr(ht$conf.int,
"conf.level")),
" percent confidence interval:\n", "
",
paste(format(ht$conf.int[1:2], digits =
digits), collapse = " "))
}
if (!is.null(ht$estimate)) {
i_out <- i_out + 1L
out[[i_out]] <- paste("sample estimates:", round(ht$estimate,
digits = digits), sep = "\n")
}
i_out <- i_out + 1L
out[[i_out]] <- "\n"
names(out)[i_out] <- "sep"
out <- do.call(paste, out)
if(is.null(adj)) adj <- 0L
text(x, y, labels = out, adj = adj, ...)
invisible(out)
}
res <- shapiro.test(rnorm(100))
plot(1,1, ylim = c(0, length(res) + 1L))
text(res, 0.6, length(res) - 1)
res
res2 <- t.test(rnorm(100))
plot(1,1, ylim = c(0, length(res2) + 1L))
text(res2, 0.6, length(res2) - 1L)
res2
Hope this helps,
Rui Barradas
?s 15:12 de 16/09/21, PIKAL Petr escreveu:> Dear all
>
> I know I have seen the answer somewhere but I am not able to find it.
Please
> help
>
>> plot(1,1)
>> res <- shapiro.test(rnorm(100))
>> res
>
> Shapiro-Wilk normality test
>
> data: rnorm(100)
> W = 0.98861, p-value = 0.5544
>
> I would like to add whole res object to the plot.
>
> I can do it one by one
>> text(locator(1), res$method)
>> text(locator(1), as.character(res$p.value))
> ...
> But it is quite inconvenient
>
> I could find some way in ggplot world but not in plain plot world.
>
> Best regards
> Petr
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Hallo Rui. I finally tested your function and it seems to me that it should propagate to the core R or at least to the stats package. Although it is a bit overkill for my purpose, its use is straightforward and simple. I checked it for several *test functions and did not find any problem. Thanks and best regards. Petr> -----Original Message----- > From: Rui Barradas <ruipbarradas at sapo.pt> > Sent: Friday, September 17, 2021 9:56 PM > To: PIKAL Petr <petr.pikal at precheza.cz>; r-help <r-help at r-project.org> > Subject: Re: [R] adding results to plot > > Hello, > > *.test functions in base R return a list of class "htest", with its own > print method. > The method text.htest for objects of class "htest" below is a hack. I > adapted the formating part of the code of print.htest to plot text(). > I find it maybe too complicated but it seems to work. > > Warning: Not debugged at all. > > > > text.htest <- function (ht, x, y = NULL, digits = getOption("digits"), > prefix = "", adj = NULL, ...) { > out <- list() > i_out <- 1L > out[[i_out]] <- paste(strwrap(ht$method, prefix = prefix), sep = "\n") > i_out <- i_out + 1L > out[[i_out]] <- paste0("data: ", ht$data.name) > > stat_line <- NULL > i_stat_line <- 0L > if (!is.null(ht$statistic)) { > i_stat_line <- i_stat_line + 1L > stat_line[[i_stat_line]] <- paste(names(ht$statistic), "=", > format(ht$statistic, digits > max(1L, digits - 2L))) > } > if (!is.null(ht$parameter)) { > i_stat_line <- i_stat_line + 1L > stat_line[[i_stat_line]] <- paste(names(ht$parameter), "=", > format(ht$parameter, digits > max(1L, digits - 2L))) > } > if (!is.null(ht$p.value)) { > fp <- format.pval(ht$p.value, digits = max(1L, digits - 3L)) > i_stat_line <- i_stat_line + 1L > stat_line[[i_stat_line]] <- paste("p-value", > if (startsWith(fp, "<")) fp else > paste("=", fp)) > } > if(!is.null(stat_line)){ > i_out <- i_out + 1L > #out[[i_out]] <- strwrap(paste(stat_line, collapse = ", ")) > out[[i_out]] <- paste(stat_line, collapse = ", ") > } > if (!is.null(ht$alternative)) { > alt <- NULL > i_alt <- 1L > alt[[i_alt]] <- "alternative hypothesis: " > if (!is.null(ht$null.value)) { > if (length(ht$null.value) == 1L) { > alt.char <- switch(ht$alternative, two.sided = "not equal to", > less = "less than", greater = "greater than") > i_alt <- i_alt + 1L > alt[[i_alt]] <- paste0("true ", names(ht$null.value), " is ", > alt.char, > " ", ht$null.value) > } > else { > i_alt <- i_alt + 1L > alt[[i_alt]] <- paste0(ht$alternative, "\nnull values:\n") > } > } > else { > i_alt <- i_alt + 1L > alt[[i_alt]] <- ht$alternative > } > i_out <- i_out + 1L > out[[i_out]] <- paste(alt, collapse = " ") > } > if (!is.null(ht$conf.int)) { > i_out <- i_out + 1L > out[[i_out]] <- paste0(format(100 * attr(ht$conf.int, "conf.level")), > " percent confidence interval:\n", " ", > paste(format(ht$conf.int[1:2], digits > digits), collapse = " ")) > } > if (!is.null(ht$estimate)) { > i_out <- i_out + 1L > out[[i_out]] <- paste("sample estimates:", round(ht$estimate, > digits = digits), sep = "\n") > } > i_out <- i_out + 1L > out[[i_out]] <- "\n" > names(out)[i_out] <- "sep" > out <- do.call(paste, out) > if(is.null(adj)) adj <- 0L > text(x, y, labels = out, adj = adj, ...) > invisible(out) > } > > > res <- shapiro.test(rnorm(100)) > plot(1,1, ylim = c(0, length(res) + 1L)) > text(res, 0.6, length(res) - 1) > res > > res2 <- t.test(rnorm(100)) > plot(1,1, ylim = c(0, length(res2) + 1L)) > text(res2, 0.6, length(res2) - 1L) > res2 > > > Hope this helps, > > Rui Barradas > > > > ?s 15:12 de 16/09/21, PIKAL Petr escreveu: > > Dear all > > > > I know I have seen the answer somewhere but I am not able to find it. > Please > > help > > > >> plot(1,1) > >> res <- shapiro.test(rnorm(100)) > >> res > > > > Shapiro-Wilk normality test > > > > data: rnorm(100) > > W = 0.98861, p-value = 0.5544 > > > > I would like to add whole res object to the plot. > > > > I can do it one by one > >> text(locator(1), res$method) > >> text(locator(1), as.character(res$p.value)) > > ... > > But it is quite inconvenient > > > > I could find some way in ggplot world but not in plain plot world. > > > > Best regards > > Petr > > > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > > and provide commented, minimal, self-contained, reproducible code. > >