Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在同一图形上绘制多行-在同一页面上绘制少数图形_R_Plot - Fatal编程技术网

R 在同一图形上绘制多行-在同一页面上绘制少数图形

R 在同一图形上绘制多行-在同一页面上绘制少数图形,r,plot,R,Plot,这是我之前的一点后续: 这次我想将我的图表导出为pdf格式,我希望在同一页上有6个图表。为什么是6,因为我有6个不同的数据集: df1 <- data.frame(matrix(rnorm(50), 10, 15)) df2 <- data.frame(matrix(rnorm(30), 10, 15)) df3 <- data.frame(matrix(rnorm(20), 10, 5)) df4 <- data.frame(matrix(rnorm(70), 10,

这是我之前的一点后续:

这次我想将我的图表导出为pdf格式,我希望在同一页上有6个图表。为什么是6,因为我有6个不同的数据集:

df1 <- data.frame(matrix(rnorm(50), 10, 15))
df2 <- data.frame(matrix(rnorm(30), 10, 15))
df3 <- data.frame(matrix(rnorm(20), 10, 5))
df4 <- data.frame(matrix(rnorm(70), 10, 5))
df5 <- data.frame(matrix(rnorm(110), 10, 10))
df6 <- data.frame(matrix(rnorm(70), 10, 10))
这听起来很复杂,但对于有R经验的人来说,这应该是一项非常简单的任务

编辑:


df1$numb我已经尝试更好地匹配您的示例

library(dplyr)
library(tidyr)
library(ggplot2)
library(grid)

df1 <- data.frame(matrix(rnorm(50), 10, 10))
df2 <- data.frame(matrix(rnorm(30), 10, 10))
df3 <- data.frame(matrix(rnorm(20), 10, 5))
df4 <- data.frame(matrix(rnorm(70), 10, 5))
df5 <- data.frame(matrix(rnorm(110), 10, 15))
df6 <- data.frame(matrix(rnorm(70), 10, 15))

data <- list(df1, df2, df3, df4, df5, df6)

all_names <- c("Mark", "Tere", "Marcus", "Heidi", "Georg", "Tieme", "Joan", "Tytus", "Julius", "Crater")

for (i in seq_along(data)){
  # Adding number of columns
  data[[i]]$n_col <- ncol(data[[i]])

  # Adding the random order row names
  data[[i]]$id <-sample(all_names, 10) 

  # Adding a data frame number column
  data[[i]] <- cbind(data[[i]], frame = i)
}

df <- plyr::rbind.fill(data) %>%
  gather(time, value, -id, -frame, -n_col) %>%
  filter(!is.na(value)) %>%
  mutate(time = gsub('X', '', time) %>% as.numeric)

plot_name <- function(name){
  df %>% 
    filter(id %in% c('Heidi', 'Joan', name)) %>%
    ggplot() +
    aes(x = time, y = value, group = id, colour = id) +
    geom_line() +
    geom_point() +
    facet_wrap(frame ~ n_col, scales = 'free_x', nrow = 3, ncol = 2) +
    theme(strip.background = element_blank(),
          strip.text.x = element_blank())
}

pdf(file = sprintf("Df_plots.pdf", df1), paper='A4r')

for (name in setdiff(all_names, c('Heidi', 'Joan'))){
  print(plot_name(name))
  #grid.newpage()
}

dev.off()
库(dplyr)
图书馆(tidyr)
图书馆(GG2)
图书馆(网格)

df1什么是df_图,tbl_图,数据_图?稍微编辑代码。这只是一个可以使用/编辑的代码示例。测试数据看起来不错。我将在一分钟内用我的真实数据进行测试。你有pdf格式的每一页都是空白的吗?没有,是我的每一页。它对真实数据有效吗?有效。我不得不稍微修改我的数据,但代码可以正常工作。还是有空白页的问题。当然我可以试一试。给我点时间!另外,我现在得到的是空白页——但这是因为情节足够大,足以占据整个页面。删除grid.newpage()将修复此问题
pdf(file = sprintf("Df_plots.pdf", df1), paper='A4r')

lapply(1:length(df1$numb), function (i) {
  df1_melted.i <- df1_melted[df1_melted$numb %in% c(i, Heidi, Joan),]
  ggplot(data_plot2.i) +
    geom_line(aes(x = as.numeric(variable), y = value, colour = factor(id)))
})

dev.off()
df1$numb <- 1:length(row.names(df1))
df1_melted <- melt(df1, id.vars = c("id", "numb))
library(dplyr)
library(tidyr)
library(ggplot2)
library(grid)

df1 <- data.frame(matrix(rnorm(50), 10, 10))
df2 <- data.frame(matrix(rnorm(30), 10, 10))
df3 <- data.frame(matrix(rnorm(20), 10, 5))
df4 <- data.frame(matrix(rnorm(70), 10, 5))
df5 <- data.frame(matrix(rnorm(110), 10, 15))
df6 <- data.frame(matrix(rnorm(70), 10, 15))

data <- list(df1, df2, df3, df4, df5, df6)

all_names <- c("Mark", "Tere", "Marcus", "Heidi", "Georg", "Tieme", "Joan", "Tytus", "Julius", "Crater")

for (i in seq_along(data)){
  # Adding number of columns
  data[[i]]$n_col <- ncol(data[[i]])

  # Adding the random order row names
  data[[i]]$id <-sample(all_names, 10) 

  # Adding a data frame number column
  data[[i]] <- cbind(data[[i]], frame = i)
}

df <- plyr::rbind.fill(data) %>%
  gather(time, value, -id, -frame, -n_col) %>%
  filter(!is.na(value)) %>%
  mutate(time = gsub('X', '', time) %>% as.numeric)

plot_name <- function(name){
  df %>% 
    filter(id %in% c('Heidi', 'Joan', name)) %>%
    ggplot() +
    aes(x = time, y = value, group = id, colour = id) +
    geom_line() +
    geom_point() +
    facet_wrap(frame ~ n_col, scales = 'free_x', nrow = 3, ncol = 2) +
    theme(strip.background = element_blank(),
          strip.text.x = element_blank())
}

pdf(file = sprintf("Df_plots.pdf", df1), paper='A4r')

for (name in setdiff(all_names, c('Heidi', 'Joan'))){
  print(plot_name(name))
  #grid.newpage()
}

dev.off()