R:在一页上打印多种类型的地块

时间:2015-11-09 15:26:50

标签: r plot ggplot2 venn-diagram

我试图在一个页面中绘制多个图。我知道像gridExtra::grid.arrange这样的函数可以绘制由ggplot2包生成的图。我面临的问题是我有bar.plot包生成的两个图(density.plotggplot2)以及使用limma::vennDiagram函数生成的一个图。我已经尝试过以下但是它无法正常工作:

output <- paste('summary.pdf')
pdf(output,width = 25,height = 20)
par(mfrow = c(3, 3))
plot(bar.plot)
plot(density.plot)
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)))
invisible(dev.off())

dat.venn是VennCounts类型的数据:

 I-H-10003-T1-D1 I-H-10003-T2-D1 I-H-10003-T3-D1 Counts
               0               0               0      0
               0               0               1     41
               0               1               0     81
               0               1               1     66
               1               0               0     10
               1               0               1      2
               1               1               0      4
               1               1               1     56
attr(,"class")
[1] "VennCounts"

我无法找到与grid.arrange功能兼容的维恩图包。我认为VennCounts无法使用grid.arrange函数打印出来ggplot2par图可以使用pdf(output,width = 25,height = 20) # Create layout : nrow = 2, ncol = 2 pushViewport(viewport(layout = grid.layout(2, 2))) # A helper function to define a region on the layout define_region <- function(row, col){ viewport(layout.pos.row = row, layout.pos.col = col) } # Arrange the plots print(bar.plot, vp = define_region(1, 1:2)) print(density.plot, vp = define_region(2, 1)) print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)), vp = define_region(2, 2)) dev.off() 函数打印出来。

更新: 我尝试使用pushViewport但它仍然在下一页打印维恩图:

//This program will allow a user to input a year, any year, 
//and will pull the correct days of the year and output them
//into a file

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

//Function prototypes
bool IsLeap(int); //Checks for leap year
int JanFirst(int); //Pulls the correct January first to line up the days
int DaysInMonth(int, bool); //Takes the number of the month and a flag    stating if it is a leap
int Header(int, int); //Takes the number of the month, the first day of said month, and prints a header
void PrintMonth(int, int&); //The first day of the following month
int Skip(int); //Prints the correct spaces
int SkipDays(int); //Prints leading spaces

int main()
{
    ofstream fout;
    fout.open("CalendarProgram.txt");

{
    int year, dayone, month, numdays, print;
    bool leap;

    do {
        cout << "What year would you like a calendar for? " << endl;
        cin >> year;
        cout << endl;
        if (year < 1582 || year > 9999)
            cout << "ERROR! That year was not recorded in the Gregorian Calendar System!"
            << "\nPope Gregory XVIII introduced the Gregorian Calendar System in 1582.\n\n";
    } while (year < 1582 || year > 9999);

    dayone = JanFirst(year);
    leap = IsLeap(year);
    Skip(9);

    month = 1;

    while (month <= 12)
    {
        numdays = DaysInMonth(month, leap);
        Header(month, year);
        PrintMonth(numdays, dayone);
        cout << endl << endl << endl;
        month = month + 1;
    }
    cout << endl;
}
}

//Function definition: IsLeap
//Preconditions: The user has given a year they want to view a calendar of
//Postconditions: This function will check whether the year is a leap year or not
bool IsLeap(int year)
{
bool days;

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
    days = true;
}
else
{
    days = false;
}

return days;
}

//Function definition: JanFirst
//Preconditions: The user has input the year they wish to view a calendar for and IsLeap checked for leap year
//Postconditions: This function will output the correct days in their respective spots on the calendar
int JanFirst(int year)
{
int daystart;

year = year - 1;

daystart = (1 + year + (year / 4) - (year / 100) + (year / 400)) % 7;

return daystart;
}

//Function definition: DaysInMonth
//Preconditions: The year has been checked for leap
//Postconditions: Will output the number of days in the month
int DaysInMonth(int month, bool leap)
{
if (month == 1)
    return(31);
else if (month == 2)
    if (leap)
        return(29);
    else
        return(28);
else if (month == 3)
    return (31);
else if (month == 4)
    return (30);
else if (month == 5)
    return (31);
else if (month == 6)
    return (30);
else if (month == 7)
    return (31);
else if (month == 8)
    return (31);
else if (month == 9)
    return (30);
else if (month == 10)
    return (31);
else if (month == 11)
    return (30);
else if (month == 12)
    return (31);
}

//Function definition: Header
//Preconditions: The year has been selected and checked for leap
//Postconditions: Will output the header
int Header(int month, int year)
{
cout << endl;
switch (month)
{
case 1: Skip(3);
    cout << "January " << year << endl << endl;
    break;
case 2:  Skip(3);
    cout << "February " << year << endl << endl;
    break;
case 3:  Skip(3);
    cout << "March " << year << endl << endl;
    break;
case 4:  Skip(3);
    cout << "April " << year << endl << endl;
    break;
case 5:  Skip(3);
    cout << "May " << year << endl << endl;
    break;
case 6:  Skip(3);
    cout << "June " << year << endl << endl;
    break;
case 7:  Skip(3);
    cout << "July " << year << endl << endl;
    break;
case 8:  Skip(3);
    cout << "August " << year << endl << endl;
    break;
case 9: Skip(3);
    cout << "September " << year << endl << endl;
    break;
case 10: Skip(3);
    cout << "October " << year << endl << endl;
    break;
case 11:  Skip(3);
    cout << "November " << year << endl << endl;
    break;
case 12:  Skip(3);
    cout << "December " << year << endl << endl;
    break;
default: month = 1;
}
cout << "---------------------------" << endl;
cout << " S   M   T   W   R   F   S " << endl;
cout << "---------------------------" << endl << endl;
return month;
}

//Function definition: PrintMonth
//Preconditions: The year has been selected and checked for leap
//Postconditions: This will align the days
void PrintMonth(int numdays, int&weekday)
{
int day = 1;
SkipDays(weekday);
while (day <= numdays)
{
    cout << setw(2) << day << "  ";
    if (weekday == 6)
    {
        cout << endl;
        weekday = 0;
    }
    else
        weekday = weekday + 1;
    day = day + 1;
}
}

//Function definition: Skip
//Preconditions: The year has been selected and days figured out
//Postconditions: Will place the days of the months in the correct positions
int Skip(int i)
{
while (i > 0)
{
    cout << "  ";
    i = i - 1;
}
return i;
}

//Function definition: SkipDay
//Preconditions: PrintMonth has been called
//Postconditions: This should align the first day of every month correctly
int SkipDays(int day)
{
return Skip(2 * day);
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

首先,将每个文件保存为.png文件,例如save(yourfile, file = "yourname.png")

其次,使用网格包的read.PNG函数加载它们,例如yours.png <- readPNG("yourfile.PNG")

之后,使用ra g1 <- rasterGrob(yours.png, interpolate=TRUE)中的rasterGrob函数转换它们。

一旦所有图表的格式都相当,grid.arrange()就可以了。它可能看起来像这样:

grid.arrange(g1, g2, g3, nrow=1)  # one row
dev.copy(png,'threeplots.png')   # to save the array of plots     
dev.off()                         # to close the device

答案 1 :(得分:2)

gridGraphics包与gridBase一样,使得网格,ggplot,网格和基本图形的混合相当简单。 grid.echo()函数将基本图形转换为网格图形。 (使用gridGraphics的优势在于基础图形可以通过grid进行编辑和修改;请参阅gridGraphics.pdf。)

在下文中,我绘制了两个ggplots和一个维恩图(针对vennDiagram包中的limma帮助页面),我将三个图放在视口中,确保维恩图情节需要grid.echo处理。

更新至ggplot2 2.0.0 stat的默认geom_barstat = "count"

  library(ggplot2)
  library(gridGraphics)
  library(grid)
  library(limma)  # From bioC software repository

  grid.newpage()

  # Position 1st ggplot
  pushViewport(viewport(y = 1, height = 1/3, just = "top"))
  gg1 <- ggplot(mtcars, aes(x = mpg)) + geom_density()  
  print(gg1, newpage = FALSE)
  upViewport()

  # Position 2nd ggplot
  pushViewport(viewport(y = .5, height = 1/3, just = "centre"))
  gg2 <- ggplot(mtcars, aes(x = factor(carb, levels = 1:8))) + 
          geom_bar() + scale_x_discrete(drop = FALSE)
  print(gg2, newpage = FALSE)
  upViewport()

  # Function to draw venn diagram - the venn diagram is taken from ?vennDiagram
  plotfun <- function() {   
   Y <- matrix(rnorm(100*6),100,6)
   Y[1:10,3:4] <- Y[1:10,3:4]+3
   Y[1:20,5:6] <- Y[1:20,5:6]+3
   design <- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))
   fit <- eBayes(lmFit(Y,design))
   results <- decideTests(fit)
   a <- vennCounts(results)
   print(a)
   mfrow.old <- par()$mfrow
   par(mfrow=c(1,2))
   vennDiagram(a)
   vennDiagram(results, 
       include=c("up", "down"),
       counts.col=c("red", "blue"),
       circle.col = c("red", "blue", "green3"))
  }

  # Position the venn diagram
  pushViewport(viewport(y = 0, height = 1/3, just = "bottom"))
  grid.echo(plotfun, newpage = FALSE)
  upViewport(0)

enter image description here