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 A
和Status of B
使用# of A
,Sum of A
,# of B
和Sum of B
数据点.
其中月份应该在Y
轴上,而其他数据点在X
轴上,图表的标题就像ABC
.
此外,我想将该图转换为Html
可以轻松通过mailR
库邮寄的格式.
我已经有2个html表,它覆盖了我的邮件正文,如下所示:
AAAA CCCC
BBBBBBBBB
Run Code Online (Sandbox Code Playgroud)
哪AAAA
一个表BBBBBBBBB
是第二个表,CCCC
是我想要安排此图表的空白区域.
我已经重新创建了表,因为你没有提供它,这就是我正在使用的(注意:我添加了一些假数字,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和Sum
s 之间的差异太大):
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)
测试电子邮件如下所示:
@clemens提供的另一种方法和扩展是将电子邮件准备为rmarkdown
.
---
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
以直接且可重现的方式呈现输出,而无需满足图像或其他内容.
然后,发送脚本是对前一个答案的一个非常小的改动.
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)
这是rmarkdown
要在电子邮件中内联发送的呈现文件的输出.