R ggplot2用于循环绘制相同的数据 [英] R ggplot2 for loop plots same data

查看:473
本文介绍了R ggplot2用于循环绘制相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我组合了一个简单的for循环来生成一系列图,然后使用grid.arrange来绘制它们。我有两个问题:


  1. 绘图的坐标轴正确地更改为列名称,但是相同

    数据绘制在每个图上。放置了一个断点并遍历了代码后,它似乎正在递增,所以我不知道为什么。

  2. 我已将剧情审美设置为组但是,这一年会产生出现在传说中的中间.5年。这在我之前没有发生过。


使用 mtcars code>。

 库(ggplot2)
库(gridExtra)

结果< - mtcars

(i in 1:2){
nam < - paste(p,i,sep =)
赋值(
nam,ggplot (),
geom_line()+
geom_point()+
scale_colour_distiller(x,y = result, palette =Dark2,direction = -1,guide =legend)+
scale_y_continuous(name = colnames(results [i + 4]))+
scale_x_continuous(name =x)



$ b plist< - mget(paste0(p,1:2))
do.call(grid.arrange, plist)


解决方案

我想通过他们访问列 aes 映射中的编号会混淆ggplot。这样做:

  for(i in 1:2){
nam < - paste(p, i,sep =)
assign(
nam,ggplot(result,aes_string(x =disp,y = colnames(result)[i + 4],group =gear,color =gear))+
geom_line()+
geom_point()+
scale_colour_distiller(palette =Dark2,direction = -1,guide =legend)+
scale_y_continuous(name = colnames(result [i + 4]))+
scale_x_continuous(name =x)

}

我会建议迭代名称,这使得代码更清晰。这是一个可以绕过环境绕道的版本:

  plots<  -  lapply(c(drat, wt),函数(列){
ggplot(result,aes_string(x =disp,y = column,group =gear,color =gear))+
geom_line )+ geom_point()+
scale_colour_distiller(palette =Dark2,direction = -1,guide =legend)+
scale_y_continuous(name = column)+
scale_x_continuous(name = x)})%>%
do.call(grid.arrange,。)
do.call(grid.arrange,plots)
pre>

I have put together a simple for loop to generate a series of plots and then use grid.arrange to plot them. I have two problems:

  1. The axes of the plots change correctly to the column names, but the same data is plotted on each graph. Having put in a breakpoint and stepped through the code it appears to be incrementing correctly so I'm not sure why.

  2. I have set the plot aesthetic to group on year, however this produces intermediate .5 years that appear in the legend. This hasn't happened to me before.

Should all be reproducible using mtcars.

library(ggplot2)
library(gridExtra)

result <- mtcars

for(i in 1:2) {
  nam <- paste("p", i, sep = "")
  assign(
    nam, ggplot(result, aes(x = disp, y = results[i+4], group = gear, color = gear)) +
      geom_line() +
      geom_point() +
      scale_colour_distiller(palette = "Dark2", direction = -1, guide = "legend") +
      scale_y_continuous(name = colnames(results[i+4])) +
      scale_x_continuous(name = "x")
  )
}


plist <- mget(paste0("p", 1:2))
do.call(grid.arrange, plist)

解决方案

I think trying to access the columns by their number in the aes mapping is confusing ggplot. This works:

for(i in 1:2) {
  nam <- paste("p", i, sep = "")
  assign(
    nam, ggplot(result,aes_string(x="disp",y=colnames(result)[i+4], group="gear", color="gear")) +
      geom_line() +
      geom_point() +
      scale_colour_distiller(palette = "Dark2", direction=-1, guide="legend") +
      scale_y_continuous(name=colnames(result[i+4])) +
      scale_x_continuous(name="x")
  )
}

I would suggest iterating over the names though; this makes the code much clearer. Here's a version that does this and skips the detour around the environment:

plots <- lapply(c("drat", "wt"), function(column) {
  ggplot(result,aes_string(x="disp",y=column, group="gear", color="gear")) +
    geom_line() + geom_point() +
    scale_colour_distiller(palette = "Dark2", direction=-1, guide="legend") +
    scale_y_continuous(name=column) +
    scale_x_continuous(name="x")}) %>% 
  do.call(grid.arrange, .)
do.call(grid.arrange, plots)

这篇关于R ggplot2用于循环绘制相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆