简体   繁体   English

R将ggplot与数据帧列表一起使用

[英]R Using ggplot with a list of dataframes

I have a list of dataframes (df_list) which I want to plot with ggplot. 我有一个要用ggplot绘制的数据帧(df_list)列表。 I want to display the Date on the x-scale (its always the same of course) and then 2 different geo_lines. 我想在x刻度上显示日期(当然,它总是相同),然后在2个不同的geo_lines上显示。 The first geoline out of the df1-3 and the 2nd line from abc. df1-3中的第一个地线和abc中的第二个地线。 Here is an examle code: 这是一个代码示例:

library(lubridate)
v1 = seq(ymd('2000-05-01'),ymd('2000-05-10'),by='day')
v2 = seq(2,20, length = 10)
v3 = seq(-2,7, length = 10)
v4 = seq(-6,3, length = 10)
df1 = data.frame(Date = v1, df1_Tmax = v2, df1_Tmean = v3, df1_Tmin = v4)

v1 = seq(ymd('2000-05-01'),ymd('2000-05-10'),by='day')
v2 = seq(3,21, length = 10)
v3 = seq(-3,8, length = 10)
v4 = seq(-7,4, length = 10)
df2 = data.frame(Date = v1, df2_Tmax = v2, df2_Tmean = v3, df2_Tmin = v4)

v1 = seq(ymd('2000-05-01'),ymd('2000-05-10'),by='day')
v2 = seq(4,22, length = 10)
v3 = seq(-4,9, length = 10)
v4 = seq(-8,5, length = 10)
df3 = data.frame(Date = v1, df3_Tmax = v2, df3_Tmean = v3, df3_Tmin = v4)

v1 = seq(ymd('2000-05-01'),ymd('2000-05-10'),by='day')
v2 = seq(2,20, length = 10)
v3 = seq(-2,8, length = 10)
v4 = seq(-6,3, length = 10)
abc = data.frame(Date = v1, ABC_Tmax = v2, ABC_Tmean = v3, ABC_Tmin = v4)

df_list = list(df1, df2, df3, abc)
names(df_list) = c("df1", "df2", "df3", "abc")

For example I want to plot df_list$df1$df1_Tmax together with df_list$abc$ABC_Tmax . 例如,我想将df_list$df1$df1_Tmaxdf_list$abc$ABC_Tmax一起df_list$abc$ABC_Tmax

I tried this: 我尝试了这个:

ggplot(data = df_list, aes(x = df1$Date)) +
  geom_line(aes(y = df1$df1_Tmax), color = "darkgreen", size = 1) +
  geom_line(aes(y = abc$ABC_Tmax), color = "grey27", size = 1) +
  labs(title="Title", 
       subtitle="subtitle", 
       x = "day", 
       y = "T") +
  scale_x_date(date_labels="%d",date_breaks  ="1 day")

But I get this error message: 但我收到此错误消息:

Error: `data` must be a data frame, or other object coercible by `fortify()`, not a list

Any ideas how to solve this problem? 任何想法如何解决这个问题?

You can try this. 你可以试试看

library(ggplot2)
ggplot(data = cbind(df_list$df1, df_list$abc), aes(x = Date)) +
  geom_line(aes(y = df1_Tmax), color = "darkgreen", size = 1) +
  geom_line(aes(y = ABC_Tmax), color = "grey27", size = 1) +
  labs(title="Title", 
       subtitle="subtitle", 
       x = "day", 
       y = "T") +
  scale_x_date(date_labels="%d",date_breaks  ="1 day")

Just generate a new data frame using cbind , ie, cbind(df_list$df1, df_list$abc) within ggplot function. 只需使用生成一个新的数据帧cbind ,即cbind(df_list$df1, df_list$abc)ggplot功能。

Caution: cbind(df_list$df1, df_list$abc) leads to the following error. 警告: cbind(df_list$df1, df_list$abc)导致以下错误。

Error: data must be uniquely named but has duplicate columns 错误: data必须具有唯一的名称,但具有重复的列

Please try this since you also have the same Date variable in these two data frames. 请尝试执行此操作,因为在这两个数据框中还具有相同的Date变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM