[ggplot2添加矩形时速度很慢

问题描述 投票:0回答:1

我正在使用geom_rect向ggplot(geom_raster)添加一个矩形。与没有矩形的绘图相比,执行时间要长得多。

我正在尝试使用geom_raster绘制黑白图像。我需要在该图像上方显示一个框。对于简单图像,它的速度相当快(128 * 72图像为0.2秒; 1280 * 720图像为2秒)。当我使用geom_rect添加矩形时,时间要长得多(128 * 72为0.6秒; 1280 * 720图像为30秒)[rd5.xlarge EC2实例的时间]我在基本R中有合理的经验,但对tidyverse还是陌生的。这是预期的行为还是我做错了什么?我知道我可以使用成像器直接显示图像,但是我想要geom_raster。

## make dummy data
test_df<-t(sapply(1:1280,function (x) abs(sort(rnorm(720,mean=1280/2, sd=1280/2*0.34))-(1280/2) )) )

## rearrange data to be tidy
df_gs_tidy<-test_df %>%
  as.data.frame() %>%
  pivot_longer(everything()) %>%
  mutate( xval= rep(1:nrow(test_img_gs), each=ncol(test_img_gs))) %>%
  mutate(yval= rep(ncol(test_img_gs):1, nrow(test_img_gs))) %>%
  rename(zval=value)

## measure time [system.time() does not accurately show the time it takes]
st.time<- Sys.time()
p<-ggplot(df_gs_tidy, aes(x=xval, y=yval)) +
  geom_raster(aes(fill = zval)) + 
  scale_fill_gradientn(colours = grey(seq(0,1,l=20))) +
  theme_bw() + ggtitle(i) 
en.time1<- Sys.time()
p
en.time2<- Sys.time()
paste ("time1: ",round(en.time1-st.time,4),"sec")
paste ("time2: ",round(en.time2-st.time,4),"sec")
## around 2 sec on my machine

## Now with the rectangle
st.time<- Sys.time()
p<-p +
  geom_rect(aes(xmin = 140, xmax = 1140,
                ymin = 300, ymax = 420), 
            fill=NA, color="red", size=0.5)
en.time1<- Sys.time()
p
en.time2<- Sys.time()
paste ("time1: ",round(en.time1-st.time,4),"sec")
paste ("time2: ",round(en.time2-st.time,4),"sec")
## around 22 sec on my machine
###################
## We can reduce the dataset, but the effect remains
xval_red<- seq(1,max(df_gs_tidy$xval), by =10)
yval_red<- seq(1,max(df_gs_tidy$yval), by =10)
df_gs_tidy_plot<-df_gs_tidy %>%
  filter(xval %in% xval_red, yval %in% yval_red)

## measure time for reduced data
st.time<- Sys.time()
p<-ggplot(df_gs_tidy_plot, aes(x=xval, y=yval)) +
  geom_raster(aes(fill = zval)) + 
  scale_fill_gradientn(colours = grey(seq(0,1,l=20))) +
  theme_bw() + ggtitle(i) 
en.time1<- Sys.time()
p
en.time2<- Sys.time()
paste ("time1: ",round(en.time1-st.time,4),"sec")
paste ("time2: ",round(en.time2-st.time,4),"sec")
## around 0.2 sec on my machine

## reduced data with the rectangle
st.time<- Sys.time()
p<-p +
  geom_rect(aes(xmin = 140, xmax = 1140,
                ymin = 300, ymax = 420), 
            fill=NA, color="red", size=0.5)
en.time1<- Sys.time()
p
en.time2<- Sys.time()
paste ("time1: ",round(en.time1-st.time,4),"sec")
paste ("time2: ",round(en.time2-st.time,4),"sec")
## around 0.6 sec on my machine

感谢您的输入-希望收到所有反馈。

r ggplot2
1个回答
1
投票

使用当前设置,您将要绘制近一百万个栅格图块,ggplot正在绘制另一个矩形,从数据中获取细节,丢弃它们,并将另一个矩形映射到您指定的坐标。

您只需要一个矩形,因此更适合使用:

p<-p +
  annotate("rect", xmin = 140, xmax = 1140, ymin = 300, ymax = 420, 
                   fill=NA, color="red", size=0.5)

而不是

p<-p +
  geom_rect(aes(xmin = 140, xmax = 1140,
                ymin = 300, ymax = 420), 
            fill=NA, color="red", size=0.5)

在我的机器上,它的运行速度要快得多,应该几乎与没有矩形的情况相同。

© www.soinside.com 2019 - 2024. All rights reserved.