根据R中的数据框绘制合适的图形并将其转换为HTML

Vec*_* JX 7 r html-table ggplot2 dataframe plotly

我有下面提到的数据帧:

DF_1>

    Month  # of A  M  Sum of A  M Med A  Mean A   # of B  M  Sum of B M  Median B  Mean B

    Mar-17 10      -  100000    -        -        6       -  15000    -  -         -      
    Feb-17 22      -  150000    -        -        8       -  22000    -  -         -
    Jan-17 25      -  200000    -        -        3       -  23000    -  -         -
Run Code Online (Sandbox Code Playgroud)

将其他字段标记为-因为我不使用Plot Graph的值,下面是html格式的相同Dataframe的屏幕截图.(不包括上面的标题和数据帧的2行其余部分相同)

在此输入图像描述

与这给底部有一个附加行Total,如何绘制两个一个图形Status of AStatus of B使用# of A,Sum of A,# of BSum of B数据点.

其中月份应该在Y轴上,而其他数据点在X轴上,图表的标题就像ABC.

此外,我想将该图转换为Html可以轻松通过mailR库邮寄的格式.

我已经有2个html表,它覆盖了我的邮件正文,如下所示:

AAAA CCCC

BBBBBBBBB
Run Code Online (Sandbox Code Playgroud)

AAAA一个表BBBBBBBBB是第二个表,CCCC是我想要安排此图表的空白区域.

cle*_*ens 6

我已经重新创建了表,因为你没有提供它,这就是我正在使用的(注意:我添加了一些假数字,sum of B因为它在文本中缺失):

status <- tibble::tibble(Month = factor(c('Jan-17', 'Feb-17', 'Mar-17'),
                                        levels = c('Jan-17', 'Feb-17', 'Mar-17')),
                         '# of A' = c(100000, 150000, 200000),
                         'Sum of A' = c(6, 8, 3),
                         '# of B' = c(150000, 22000, 23000),
                         'Sum of B' = c(2, 4, 6))
Run Code Online (Sandbox Code Playgroud)

然后我使用data.table::melt()以下方法将其从宽格式转换为长格式:

status_m <- data.table::melt(status, 
                             id.vars = "Month")
Run Code Online (Sandbox Code Playgroud)

然后我用它ggplot2来创建一个折线图(注意:# of A并且# of B是在类似的尺度上,Sum of A并且Sum of B是相似的尺度,但#s和Sums 之间的差异太大):

p <- ggplot(data = status_m) +
  geom_line(aes(x = Month,
                y = value,
                group = variable,
                color = variable)) +
  theme(legend.title=element_blank())
Run Code Online (Sandbox Code Playgroud)

保存该图使用ggsave():

ggsave("plot.png",
       plot = p,
       width = 10,
       height = 10)
Run Code Online (Sandbox Code Playgroud)

接下来,我创建一个'虚拟' tableHTML:

th <- status %>% 
  tableHTML(rownames = FALSE)
Run Code Online (Sandbox Code Playgroud)

然后我创建一些HTML重新创建你指定的布局,有2行,其中第一行有2列(注意:<div>s和CSS flex,见这里).

mail_html <-
  htmltools::HTML(paste("<!DOCTYPE html>\n<html>\n<body>", 
                        '<div style="display:flex;">',
                        '<div style="flex:50%;">',
                        th,
                        '</div>',
                        '<div style="flex:50%;alig">',
                        # change this path to the plot you saved using ggsave()
                        '<img src="/path/to/plot.png" height = "800" width = "800";">',
                        '</div>',
                        '</div>',
                        '<div>',
                        th,
                        '</div>',
                        "</body>\n</html>", sep = "\n"))
Run Code Online (Sandbox Code Playgroud)

最后一步是使用发送电子邮件mailR(注意:相应地更改邮件设置):

library(mailR)

send.mail(from = "someone@gmail.com",
          to = "someone.elser@mail.com",
          subject = "test report",
          body = mail_html,
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "smtp.gmail.com", 
                      port = 465, 
                      user.name = "...", 
                      passwd = "...", 
                      ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)
Run Code Online (Sandbox Code Playgroud)

测试电子邮件如下所示:

邮件


Kev*_*eau 5

@clemens提供的另一种方法和扩展是将电子邮件准备为rmarkdown.

例如.Rmd

---
title: example
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
library(tidyverse)
library(data.table)
```

```{r status, echo=FALSE}
status <- tibble(
  Month = factor(
    c('Jan-17', 'Feb-17', 'Mar-17'),
    levels = c('Jan-17', 'Feb-17', 'Mar-17')
  ),
  '# of A' = c(100000, 150000, 200000),
  'Sum of A' = c(6, 8, 3),
  '# of B' = c(150000, 22000, 23000),
  'Sum of B' = c(2, 4, 6)
)
kable(status, format = "html") %>% kable_styling
```

```{r plot, echo=FALSE}
status_m <- melt(status, id.vars = "Month")

ggplot(status_m, aes(x = Month, y = value, group = variable, color = variable)) +
  geom_line() +
  theme(legend.title = element_blank())
```

```{r status2, echo=FALSE}
kable(status, format = "html") %>% kable_styling
```
Run Code Online (Sandbox Code Playgroud)

这样,您可以html以直接且可重现的方式呈现输出,而无需满足图像或其他内容.

example.R

然后,发送脚本是对前一个答案的一个非常小的改动.

library(mailR)

send.mail(from = "someone@gmail.com",
          to = "someone.elser@mail.com",
          subject = "test report",
          body = rmarkdown::render("example.Rmd"), # note we render here
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "smtp.gmail.com", 
                      port = 465, 
                      user.name = "...", 
                      passwd = "...", 
                      ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)
Run Code Online (Sandbox Code Playgroud)

HTML输出

这是rmarkdown要在电子邮件中内联发送的呈现文件的输出.

在此输入图像描述