R ggplot2:以不同的值开始geom_线

R ggplot2:以不同的值开始geom_线,r,ggplot2,R,Ggplot2,我试图创建一个循环,以便为几个国家创建绘图。在SO的帮助下,这是成功的 现在我有了下面的情节。但是,我希望ETS线路仅在2005年开始(此时数据开始大于0)。基本上,ETS不应该仅仅从零跳到2005年的价值,而应该简单地“从空中开始” 另外,我想将x标签设置为5年一次 这是我的密码 EUETS_UN <- subset(EUETS_UN, sector != "Total") EUETS_UN$year <-as.numeric(EUETS_UN$year) names(EUETS

我试图创建一个循环,以便为几个国家创建绘图。在SO的帮助下,这是成功的

现在我有了下面的情节。但是,我希望ETS线路仅在2005年开始(此时数据开始大于0)。基本上,ETS不应该仅仅从零跳到2005年的价值,而应该简单地“从空中开始”

另外,我想将x标签设置为5年一次

这是我的密码

EUETS_UN <- subset(EUETS_UN, sector != "Total")
EUETS_UN$year <-as.numeric(EUETS_UN$year)
names(EUETS_UN) <- c("Year", "Country","iso2", "Sector", "Emissions")

cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
      "#D55E00", "#CC79A7")

country_2s <- unique(EUETS_UN$Country)

for(i in country_2s) {

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

 df_to_plot <- subset(EUETS_UN, Country == i)
 df_to_plot$Sector <- as.character(df_to_plot$Sector)

 # 
 p <- ggplot(df_to_plot) +
 geom_line(aes(x= Year,y= Emissions,group = Sector, color = Sector)) +
labs(x="Year",y="CO2 Emissions",z="",title=paste("Emissions for",i)) + 
xlim(1990,2016) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols)+
scale_y_continuous(labels = scales::comma)

print(p)

ggsave(p,filename=paste("./",i,".png",sep=""),width=6.5, 
     height=6)

}
对于这两个“相当小”的问题,我非常感谢您的帮助

无论如何谢谢你


nordsee

您只需将零值替换为
NaN

df_to_plot <- read.table(text = "
 year country iso2      sector emissions
1990 Belgium   BE         ETS         0
1990 Belgium   BE       Total 120484398
1990 Belgium   BE   Regulated  78614107
1990 Belgium   BE Unregulated  41870292
1991 Belgium   BE         ETS         0
1991 Belgium   BE       Total 123544711
1991 Belgium   BE   Regulated  79811521
1991 Belgium   BE Unregulated  43733190
1992 Belgium   BE         ETS         0
1992 Belgium   BE       Total 122657813
1992 Belgium   BE   Regulated  78283962
1992 Belgium   BE Unregulated  44373851
1993 Belgium   BE         ETS         0
1993 Belgium   BE       Total 121557281
1993 Belgium   BE   Regulated  76752290
1993 Belgium   BE Unregulated  44804991
1994 Belgium   BE         ETS         0
1994 Belgium   BE       Total 124938188
1994 Belgium   BE   Regulated  80647991
1994 Belgium   BE Unregulated  44290197
1995 Belgium   BE         ETS         0
1995 Belgium   BE       Total 126082961
1995 Belgium   BE   Regulated  80518704
1995 Belgium   BE Unregulated  45564257
1996 Belgium   BE         ETS         0
1996 Belgium   BE       Total 129583625
1996 Belgium   BE   Regulated  79513349
1996 Belgium   BE Unregulated  50070276
1997 Belgium   BE         ETS         0
1997 Belgium   BE       Total 124046828
1997 Belgium   BE   Regulated  77308936
1997 Belgium   BE Unregulated  46737892
1998 Belgium   BE         ETS         0
1998 Belgium   BE       Total 130285109
1998 Belgium   BE   Regulated  82610050
1998 Belgium   BE Unregulated  47675059
1999 Belgium   BE         ETS         0
1999 Belgium   BE       Total 124745703
1999 Belgium   BE   Regulated  77595053
1999 Belgium   BE Unregulated  47150650
2000 Belgium   BE         ETS         0
2000 Belgium   BE       Total 126794789
2000 Belgium   BE   Regulated  80435088
2000 Belgium   BE Unregulated  46359701
2001 Belgium   BE         ETS         0
2001 Belgium   BE       Total 126129008
2001 Belgium   BE   Regulated  77255899
2001 Belgium   BE Unregulated  48873109
2002 Belgium   BE         ETS         0
2002 Belgium   BE       Total 126444625
2002 Belgium   BE   Regulated  77914653
2002 Belgium   BE Unregulated  48529972
2003 Belgium   BE         ETS         0
2003 Belgium   BE       Total 127953188
2003 Belgium   BE   Regulated  78464806
2003 Belgium   BE Unregulated  49488382
2004 Belgium   BE         ETS         0
2004 Belgium   BE       Total 129040883
2004 Belgium   BE   Regulated  79430967
2004 Belgium   BE Unregulated  49609916
2005 Belgium   BE         ETS  55363232
2005 Belgium   BE       Total 125638203
2005 Belgium   BE   Regulated  77343444
2005 Belgium   BE Unregulated  48294759
2006 Belgium   BE         ETS  54775328
2006 Belgium   BE       Total 124030891
2006 Belgium   BE   Regulated  75869846
2006 Belgium   BE Unregulated  48161044
2007 Belgium   BE         ETS  52795332
2007 Belgium   BE       Total 120611398
2007 Belgium   BE   Regulated  73189198
2007 Belgium   BE Unregulated  47422201
2008 Belgium   BE         ETS  55462028
2008 Belgium   BE       Total 120659008
2008 Belgium   BE   Regulated  71854823
2008 Belgium   BE Unregulated  48804185
2009 Belgium   BE         ETS  46206936
2009 Belgium   BE       Total 107642367
2009 Belgium   BE   Regulated  61048912
2009 Belgium   BE Unregulated  46593455
2010 Belgium   BE         ETS  50103980
2010 Belgium   BE       Total 113582031
2010 Belgium   BE   Regulated  66648934
2010 Belgium   BE Unregulated  46933097
2011 Belgium   BE         ETS  46203056
2011 Belgium   BE       Total 104158641
2011 Belgium   BE   Regulated  61319344
2011 Belgium   BE Unregulated  42839297
2012 Belgium   BE         ETS  43006980
2012 Belgium   BE       Total 101394977
2012 Belgium   BE   Regulated  58934979
2012 Belgium   BE Unregulated  42459997
2013 Belgium   BE         ETS  45231176
2013 Belgium   BE       Total 101970445
2013 Belgium   BE   Regulated  58383554
2013 Belgium   BE Unregulated  43586891
2014 Belgium   BE         ETS  43853144
2014 Belgium   BE       Total  96391039
2014 Belgium   BE   Regulated  56010346
2014 Belgium   BE Unregulated  40380694
2015 Belgium   BE         ETS  44713916
2015 Belgium   BE       Total 100229492
2015 Belgium   BE   Regulated  57375031
2015 Belgium   BE Unregulated  42854461
2016 Belgium   BE         ETS  43655728
2016 Belgium   BE       Total 100243711
2016 Belgium   BE   Regulated  56702848
2016 Belgium   BE Unregulated  43540863  
  ", header = T)


head(df_to_plot)


df_to_plot <- subset(df_to_plot, sector != "Total")
df_to_plot$year <-as.numeric(df_to_plot$year)
names(df_to_plot) <- c("Year", "Country","iso2", "Sector", "Emissions")

cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
      "#D55E00", "#CC79A7")

df_to_plot$Sector <- as.character(df_to_plot$Sector)
df_to_plot$Emissions[df_to_plot$Emissions==0] <- NaN


p <- ggplot(df_to_plot) +
geom_line(aes(x= Year,y= Emissions,group = Sector, color = Sector)) +
labs(x="Year",y="CO2 Emissions",z="",title=paste("Emissions for","Belgium")) + 
xlim(1990,2016) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols)+
scale_y_continuous(labels = scales::comma)

print(p)

df_to_plot我同意-没有理由的否决票是没有骨气的。也就是说,你应该让你的例子在将来完全可复制;e、 g.您的代码与示例数据集不匹配。谢谢@marcinebox。我插入了错误的数据部分。我现在已经调整好了
df_to_plot <- read.table(text = "
 year country iso2      sector emissions
1990 Belgium   BE         ETS         0
1990 Belgium   BE       Total 120484398
1990 Belgium   BE   Regulated  78614107
1990 Belgium   BE Unregulated  41870292
1991 Belgium   BE         ETS         0
1991 Belgium   BE       Total 123544711
1991 Belgium   BE   Regulated  79811521
1991 Belgium   BE Unregulated  43733190
1992 Belgium   BE         ETS         0
1992 Belgium   BE       Total 122657813
1992 Belgium   BE   Regulated  78283962
1992 Belgium   BE Unregulated  44373851
1993 Belgium   BE         ETS         0
1993 Belgium   BE       Total 121557281
1993 Belgium   BE   Regulated  76752290
1993 Belgium   BE Unregulated  44804991
1994 Belgium   BE         ETS         0
1994 Belgium   BE       Total 124938188
1994 Belgium   BE   Regulated  80647991
1994 Belgium   BE Unregulated  44290197
1995 Belgium   BE         ETS         0
1995 Belgium   BE       Total 126082961
1995 Belgium   BE   Regulated  80518704
1995 Belgium   BE Unregulated  45564257
1996 Belgium   BE         ETS         0
1996 Belgium   BE       Total 129583625
1996 Belgium   BE   Regulated  79513349
1996 Belgium   BE Unregulated  50070276
1997 Belgium   BE         ETS         0
1997 Belgium   BE       Total 124046828
1997 Belgium   BE   Regulated  77308936
1997 Belgium   BE Unregulated  46737892
1998 Belgium   BE         ETS         0
1998 Belgium   BE       Total 130285109
1998 Belgium   BE   Regulated  82610050
1998 Belgium   BE Unregulated  47675059
1999 Belgium   BE         ETS         0
1999 Belgium   BE       Total 124745703
1999 Belgium   BE   Regulated  77595053
1999 Belgium   BE Unregulated  47150650
2000 Belgium   BE         ETS         0
2000 Belgium   BE       Total 126794789
2000 Belgium   BE   Regulated  80435088
2000 Belgium   BE Unregulated  46359701
2001 Belgium   BE         ETS         0
2001 Belgium   BE       Total 126129008
2001 Belgium   BE   Regulated  77255899
2001 Belgium   BE Unregulated  48873109
2002 Belgium   BE         ETS         0
2002 Belgium   BE       Total 126444625
2002 Belgium   BE   Regulated  77914653
2002 Belgium   BE Unregulated  48529972
2003 Belgium   BE         ETS         0
2003 Belgium   BE       Total 127953188
2003 Belgium   BE   Regulated  78464806
2003 Belgium   BE Unregulated  49488382
2004 Belgium   BE         ETS         0
2004 Belgium   BE       Total 129040883
2004 Belgium   BE   Regulated  79430967
2004 Belgium   BE Unregulated  49609916
2005 Belgium   BE         ETS  55363232
2005 Belgium   BE       Total 125638203
2005 Belgium   BE   Regulated  77343444
2005 Belgium   BE Unregulated  48294759
2006 Belgium   BE         ETS  54775328
2006 Belgium   BE       Total 124030891
2006 Belgium   BE   Regulated  75869846
2006 Belgium   BE Unregulated  48161044
2007 Belgium   BE         ETS  52795332
2007 Belgium   BE       Total 120611398
2007 Belgium   BE   Regulated  73189198
2007 Belgium   BE Unregulated  47422201
2008 Belgium   BE         ETS  55462028
2008 Belgium   BE       Total 120659008
2008 Belgium   BE   Regulated  71854823
2008 Belgium   BE Unregulated  48804185
2009 Belgium   BE         ETS  46206936
2009 Belgium   BE       Total 107642367
2009 Belgium   BE   Regulated  61048912
2009 Belgium   BE Unregulated  46593455
2010 Belgium   BE         ETS  50103980
2010 Belgium   BE       Total 113582031
2010 Belgium   BE   Regulated  66648934
2010 Belgium   BE Unregulated  46933097
2011 Belgium   BE         ETS  46203056
2011 Belgium   BE       Total 104158641
2011 Belgium   BE   Regulated  61319344
2011 Belgium   BE Unregulated  42839297
2012 Belgium   BE         ETS  43006980
2012 Belgium   BE       Total 101394977
2012 Belgium   BE   Regulated  58934979
2012 Belgium   BE Unregulated  42459997
2013 Belgium   BE         ETS  45231176
2013 Belgium   BE       Total 101970445
2013 Belgium   BE   Regulated  58383554
2013 Belgium   BE Unregulated  43586891
2014 Belgium   BE         ETS  43853144
2014 Belgium   BE       Total  96391039
2014 Belgium   BE   Regulated  56010346
2014 Belgium   BE Unregulated  40380694
2015 Belgium   BE         ETS  44713916
2015 Belgium   BE       Total 100229492
2015 Belgium   BE   Regulated  57375031
2015 Belgium   BE Unregulated  42854461
2016 Belgium   BE         ETS  43655728
2016 Belgium   BE       Total 100243711
2016 Belgium   BE   Regulated  56702848
2016 Belgium   BE Unregulated  43540863  
  ", header = T)


head(df_to_plot)


df_to_plot <- subset(df_to_plot, sector != "Total")
df_to_plot$year <-as.numeric(df_to_plot$year)
names(df_to_plot) <- c("Year", "Country","iso2", "Sector", "Emissions")

cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
      "#D55E00", "#CC79A7")

df_to_plot$Sector <- as.character(df_to_plot$Sector)
df_to_plot$Emissions[df_to_plot$Emissions==0] <- NaN


p <- ggplot(df_to_plot) +
geom_line(aes(x= Year,y= Emissions,group = Sector, color = Sector)) +
labs(x="Year",y="CO2 Emissions",z="",title=paste("Emissions for","Belgium")) + 
xlim(1990,2016) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols)+
scale_y_continuous(labels = scales::comma)

print(p)