ggplot2图表在使用for循环创建时不打印

时间:2016-10-10 17:24:56

标签: r ggplot2 latex knitr

我使用knitr withR来构建图表,显示我在一个软件上执行的各种测试的指标。数据存储在SQL Server表中。这些测试产生相同类别的数据,但是在不同的文件中,所以我使用循环来构建图表。

这是我的设置:

SQLConn_remote是一个包含在内部使用的R包中的函数。请相信我,db连接不是问题。)

<<setup, echo=FALSE>>=
db <- SQLConn_remote(DBName = "DATABASE", ServerName = "SERVER")
df <- sqlQuery(db, "select * from TABLE", stringsAsFactors = FALSE)

versions<-unique(df[order(df$Number), ][,2])
df$Version <- factor(df$Version, levels = versions)

files <- unique(df$FileName)
@

这是循环:

for (i in 1:6){
g <- ggplot(subset(df, FileName == files[i]), aes(x=Version, y=Value, group=FileName))
g <- g + geom_line(size=.25) + geom_point(aes(shape = Build), size = 1.2, colour = 'red') + 
scale_shape_manual(values=c(1,15)) + ggtitle(files[i]) + 
facet_grid(Category ~ ., scales = "free", space = "fixed", labeller = label_value) + 
ylab("Value") + xlab("Version") + expand_limits(y=0)
g
}

但是,当我编译PDF时,不会生成任何图表。如果我最后拿g并将其放在决赛的另一边&#34; }&#34;然后它会为最后一个文件打印一个图表(如预期的那样)。

关于为什么图表不能在for循环中打印的任何想法或想法?

EXTRAS :我并不认为包含SQL数据示例非常相关,但如果您有不同的想法,请发表评论,我会添加一个基本示例

编辑:这是一个用于创建表格的脚本,您可以将其与图表脚本一起使用。

FileName <- c('File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2')
Version <- c('1.0.1', '1.0.12', '1.0.12', '1.0.1', '1.0.1', '1.0.1', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.21', '1.0.21')
Category <- c('Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3',  'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3')
Value <- c(10, 11, 12, 18, 19, 20, 12, 11, 10, 20, 18, 19, 10, 11, 12, 18, 19, 20, 12, 11, 10, 20, 18, 19)
Date <- c('2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00')
Number <- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4)
Build <- c('Release', 'Release', 'Release', 'Release', 'Release', 'Release', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration')
df <- data.frame(FileName, Version, Category, Value, Date, Number, Build)

这里是块

opts_chuck$set(fig.path='figure/minimal-',fig.align='center', fig.show='hold',fig.width=10,fig.height=5.5)
options(replace.assign=TRUE,width=90)

1 个答案:

答案 0 :(得分:0)

使用dplyrlazyeval对数据进行分组,并构建list ggplot个可以轻松绘制的--- title: ggplot2 charts --- We'll use the `mpg` data from the `ggplot2` package. ```{r} library(ggplot2) library(dplyr) library(lazyeval) mpg ``` Let's have `r n_distinct(mpg$manufacturer)` graphics, on for each manufacturer. ```{r} graphics <- lapply(unique(mpg$manufacturer), function(m) { dat <- dplyr::filter_(mpg, lazyeval::interp(~ manufacturer == mm, mm = m)) ggplot2::ggplot(dat) + ggplot2::aes_string(x = "model", y = "cty") + ggplot2::geom_boxplot() }) ``` To print _all_ the graphics just call for the `graphics` object in a chunk. ```{r, fig.width = 3, fig.height = 3} graphics ``` With your data ```{r} FileName <- c('File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2', 'File1', 'File1', 'File1', 'File2', 'File2', 'File2') Version <- c('1.0.1', '1.0.12', '1.0.12', '1.0.1', '1.0.1', '1.0.1', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.17', '1.0.15', '1.0.15', '1.0.15', '1.0.15', '1.0.21', '1.0.21') Category <- c('Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3', 'Category1', 'Category2', 'Category3') Value <- c(10, 11, 12, 18, 19, 20, 12, 11, 10, 20, 18, 19, 10, 11, 12, 18, 19, 20, 12, 11, 10, 20, 18, 19) Date <- c('2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00', '2016-05-19 10:00:00') Number <- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4) Build <- c('Release', 'Release', 'Release', 'Release', 'Release', 'Release', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration', 'Iteration') df <- data.frame(FileName, Version, Category, Value, Date, Number, Build) ``` ```{r} graphics <- lapply(unique(df$FileName), function(m) { dat <- dplyr::filter_(df, lazyeval::interp(~ FileName == mm, mm = m)) ggplot2::ggplot(dat) + ggplot2::aes_string(x = "Version", y = "Value", group = "FileName") + ggplot2::geom_line(size = 0.25) + ggplot2::geom_point(mapping = ggplot2::aes_string(shape = "Build"), size = 1.2, colour = 'red') + scale_shape_manual(values=c(1,15)) + ggtitle(m) + facet_grid(Category ~ ., scales = "free", space = "fixed", labeller = label_value) + ylab("Value") + xlab("Version") + expand_limits(y=0) }) ``` ```{r} graphics ``` 个对象。

这是一个与.pdf

相关的示例.Rmd文件
    public void info(String userName){

    String sql = "SELECT name, surname, age, username FROM member WHERE username = ?";

    try (
            PreparedStatement stmt  = connection.prepareStatement(sql);
            stmt.setString(1, userName);
            ResultSet rs    = stmt.executeQuery(sql)){

        // loop through the result set
        while (rs.next()) {
            String fname = rs.getString("name");           
            String sname = rs.getString("surname"); 
            int age = rs.getInt("age");       
            String uname = rs.getString("username");

            lblFName.setText(fname);
            lblSName.setText(sname);
            lblAge.setText(String.valueOf(age));
            lblUName.setText(uname);
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    }

生成的pdf文件是一个带有缩略图的小屏幕截图,显示所有图形都已打印到页面上。 enter image description here