簡體   English   中英

在ggplot2中創建用於繪圖的循環會導致錯誤

[英]Creating loop for plotting in ggplot2 leads to error

以前已經以類似的樣式發布了此消息,但是我現在已經調整了一些內容,希望(!)現在已經清楚了

我試圖創建一個循環以便為多個國家/地區創建地塊。

我的數據Plot_dfPlot_df

這里是數據摘錄:

year country iso2      sector emissions
1990 Belgium   BE         ETS         0
1990 Belgium   BE   Regulated  78614107
1990 Belgium   BE Unregulated  41870292
1991 Belgium   BE         ETS         0
1991 Belgium   BE   Regulated  79811521
1991 Belgium   BE Unregulated  43733190
...
2011 Belgium   BE         ETS  46203056
2011 Belgium   BE   Regulated  61319344
2011 Belgium   BE Unregulated  42839297
2012 Belgium   BE         ETS  43006980
2012 Belgium   BE   Regulated  58934979
2012 Belgium   BE Unregulated  42459997
2013 Belgium   BE         ETS  45231176
2013 Belgium   BE   Regulated  58383554
2013 Belgium   BE Unregulated  43586891
2014 Belgium   BE         ETS  43853144
2014 Belgium   BE   Regulated  56010346
2014 Belgium   BE Unregulated  40380694
2015 Belgium   BE         ETS  44713916
2015 Belgium   BE   Regulated  57375031
2015 Belgium   BE Unregulated  42854461
2016 Belgium   BE         ETS  43655728
2016 Belgium   BE   Regulated  56702848
2016 Belgium   BE Unregulated  43540863

dput(head(Plot_df, 15))傳送了這個

structure(list(year = c("1990", "1990", "1990", "1990", "1990", 
"1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990", 
"1990", "1990"), country = c("Austria", "Austria", "Austria", 
"Belgium", "Belgium", "Belgium", "Bulgaria", "Bulgaria", "Bulgaria", 
"Croatia", "Croatia", "Croatia", "Cyprus", "Cyprus", "Cyprus"
), iso2 = c("AT", "AT", "AT", "BE", "BE", "BE", "BG", "BG", "BG", 
"HR", "HR", "HR", "CY", "CY", "CY"), sector = c("ETS", "Regulated", 
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated", 
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated", 
"Unregulated"), emissions = c(0, 38264402.6689529, 24027827.7997971, 
0, 78614106.9221497, 41870291.5153503, 0, 69103153.6445618,         
9569791.66793823, 
0, 17530229.1374207, 5911735.70632935, 0, 3135556.17528036, 1507499.48878214
)), row.names = c("378", "2836", "3100", "813", "8310", "8410", 
"558", "16410", "16510", "438", "24510", "24610", "783", "3261", 
"3271"), class = "data.frame")

我只顯示了一個國家/地區的全部數據,因為每個國家/地區看起來都一樣(除了不同的數字Plot_df$emissions

我想做的是(大多數您將從下面的代碼中看到):

  • 創建循環,因此將為所有國家/地區創建地塊
  • x軸=年; y =排放量(某國家/地區,如Plot_df$country
  • 3組(曲線)應來自“ Plot_df $ sector”:受監管,不受監管,ETS受監管和不受監管的時間應在1990:2016之間; 2005:2017之間的ETS。 (這些年份是具有數據可用性的年份)

這是我嘗試的:

# Sets up the loop to run from i=1 through a list of countries from vector 
`Plot_df$country`
for(i in (1:length(unique(Plot_df$country)))){

# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
        "#D55E00", "#CC79A7")

# Plotting code where DATA, YEAR, etc need to be handed the right vectors
p <- ggplot() +
geom_line(Plot_df,aes(x=year,y=emissions,group=sector),
          color=cols[1]) +
labs(x="Year",y="CO2 emissions",z="",title=paste("Emissions for", 
country[i])) + 
xlim(1990, 2016) +
ylim(-50,50) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm"))
p

# Save plot, where the file name automatically gets a country name suffix
 ggsave(p,filename=paste("./FILENAME",country[i],".png",sep=""),width=6.5, 
     height=6)
}

我收到此錯誤,我不知道為什么

Error: `data` must be a data frame, or other object coercible by 
`fortify()`, not an S3 object with class uneval
Did you accidentally pass `aes()` to the `data` argument?

任何想法,為什么發生這種情況?

無論如何謝謝

FWIW一小段代碼樣式可以幫助您。

for(country in unique(Plot_df$country)) {

  # YOU NEVER *REALLY* USE THIS VECTOR JUST ONE ELEMENT FROM IT

  # Color settings: colorblind-friendly palette
  c(
    "#999999", "#E69F00", "#56B4E9", "#009E73", 
    "#F0E442", "#0072B2", "#D55E00", "#CC79A7"
  ) -> cols

  # carve out the data for the plot
  country_df <- Plot_df[Plot_df$country == country,]

  # Plotting code where DATA, YEAR, etc need to be handed the right vectors
  ggplot() +
    geom_line(
      data = country_df, # THIS IS WHAT YOU FORGOT
      aes(year, emissions, group = sector), 
      color = cols[1] # WHY [1] IF YOU DEFINED A VECTOR
    ) +
    xlim(1990, 2016) + # SHOULD LIKELY USE scale_x_… and set limits there + expand=c(0,0) insteasd
    ylim(-50, 50) + # SAME
    labs(
      x = "Year", y = "CO2 emissions", 
      title = sprintf("Emissions for %s", country)
    ) + 
    theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p # THERE IS A margin() function

  print(p) # it won't print without print()

  # Save plot, where the file name automatically gets a country name suffix
  ggsave(
    plot = p, 
    filename = sprintf("./FILENAME-%s.png", country), # I PREFER sprintf
    width = 6.5, 
    height = 6
  )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM