gpt4 book ai didi

r - 使用 ggplot 绘制大量时间序列。可以加速吗?

转载 作者:行者123 更新时间:2023-12-04 11:23:56 24 4
gpt4 key购买 nike

我正在处理数以千计的气象时间序列数据(样本数据可以从这里下载)
https://dl.dropboxusercontent.com/s/bxioonfzqa4np6y/timeSeries.txt

在我的 Linux Mint PC(64 位,8GB RAM,双核 2.6 GHz)上使用 ggplot2 绘制这些数据需要很多时间。我想知道是否有办法加快速度或绘制这些数据的更好方法?非常感谢您的任何建议!

这是我现在使用的代码

##############################################################################
#### load required libraries
library(RCurl)
library(reshape2)
library(dplyr)
library(ggplot2)

##############################################################################
#### Read data from URL
dataURL <- "https://dl.dropboxusercontent.com/s/bxioonfzqa4np6y/timeSeries.txt"
tmp <- getURL(dataURL)
df <- tbl_df(read.table(text = tmp, header = TRUE))
df

##############################################################################
#### Plot time series using ggplot2
# Melt the data by date first
df_melt <- melt(df, id = "date")
str(df_melt)

df_plot <- ggplot(data = df_melt, aes(x = date, y = value, color = variable)) +
geom_point() +
scale_colour_discrete("Station #") +
xlab("Date") +
ylab("Daily Precipitation [mm]") +
ggtitle("Daily precipitation from 1915 to 2011") +
theme(plot.title = element_text(size = 16, face = "bold", vjust = 2)) + # Change size & distance of the title
theme(axis.text.x = element_text(angle = 0, size = 12, vjust = 0.5)) + # Change size of tick text
theme(axis.text.y = element_text(angle = 0, size = 12, vjust = 0.5)) +
theme( # Move x- & y-axis lables away from the axises
axis.title.x = element_text(size = 14, color = "black", vjust = -0.35),
axis.title.y = element_text(size = 14, color = "black", vjust = 0.35)) +
theme(legend.title = element_text(colour = "chocolate", size = 14, face = "bold")) + # Change Legend text size
guides(colour = guide_legend(override.aes = list(size = 4))) + # Change legend symbol size
guides(fill = guide_legend(ncols = 2))
df_plot

最佳答案

您的问题的一部分要求“绘制这些数据的更好方法”。

本着这种精神,您似乎有两个问题,首先,您希望沿 x 轴绘制 >35,000 个点,正如某些评论指出的那样,这将导致像素重叠,但非常大的高分辨率除外监视器。其次,也是更重要的 IMO,您试图在同一个图上绘制 69 个时间序列(站)。在这种情况下,热图可能是更好的方法。

library(data.table)
library(ggplot2)
library(reshape2) # for melt(...)
library(RColorBrewer) # for brewer.pal(...)
url <- "http://dl.dropboxusercontent.com/s/bxioonfzqa4np6y/timeSeries.txt"
dt <- fread(url)
dt[,Year:=year(as.Date(date))]

dt.melt <- melt(dt[,-1,with=F],id="Year",variable.name="Station")
dt.agg <- dt.melt[,list(y=sum(value)),by=list(Year,Station)]
dt.agg[,Station:=factor(Station,levels=rev(levels(Station)))]
ggplot(dt.agg,aes(x=Year,y=Station)) +
geom_tile(aes(fill=y)) +
scale_fill_gradientn("Annual\nPrecip. [mm]",
colours=rev(brewer.pal(9,"Spectral")))+
scale_x_continuous(expand=c(0,0))+
coord_fixed()



注意使用 data.tables .您的数据集相当大(因为所有列;35,000 行并不是那么大)。在这种情况下 data.tables将大大加快处理速度,尤其是 fread(...)这比基本 R 中的文本导入函数快得多。

关于r - 使用 ggplot 绘制大量时间序列。可以加速吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25273358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com