【问题标题】:How to resolve "Error: Discrete value supplied to continuous scale" in ggplot2 / R?如何解决ggplot2 / R中的“错误:提供给连续比例的离散值”?
【发布时间】:2018-12-24 14:23:31
【问题描述】:

我试图通过类似的主题找到解决方案,但没有找到合适的解决方案。这可能是由于我使用的搜索词。如果我遗漏了什么,请接受我的歉意。

我正在尝试按国家和部门随着时间的推移绘制ETSemissionsUNemissions。我以前使用过相同的代码(在下面提供),现在我不知道问题出在哪里。

这是数据的摘录(请忽略这两个国家的数据相同):

country   iso2   year sector      UNemissions          ETSemissions
Austria   AT     2005 1 - Energy  16194772.33          16539659
Austria   AT     2006 1 - Energy  15039192.77          15275065
Austria   AT     2007 1 - Energy  13757091.05          14124646
Austria   AT     2008 1 - Energy  13582006.99          14572511
Austria   AT     2009 1 - Energy  12526267.29          12767555
Austria   AT     2010 1 - Energy  13852187.50          15506112
Austria   AT     2011 1 - Energy  13666544.68          15131551
Austria   AT     2012 1 - Energy  12256272.25          13121434
Austria   AT     2013 1 - Energy  11224625.46          8074514
Austria   AT     2014 1 - Energy   9499544.19          6426135
Austria   AT     2015 1 - Energy  10623550.19          7514263
Austria   AT     2016 1 - Energy  10448925.88          7142937
Austria   AT     2017 1 - Energy   9255425.88          7795277  
Belgium   BE     2005 1 - Energy  16194772.33          16539659
Belgium   BE     2006 1 - Energy  15039192.77          15275065
Belgium   BE     2007 1 - Energy  13757091.05          14124646
Belgium   BE     2008 1 - Energy  13582006.99          14572511
Belgium   BE     2009 1 - Energy  12526267.29          12767555
Belgium   BE     2010 1 - Energy  13852187.50          15506112
Belgium   BE     2011 1 - Energy  13666544.68          15131551
Belgium   BE     2012 1 - Energy  12256272.25          13121434
Belgium   BE     2013 1 - Energy  11224625.46          8074514
Belgium   BE     2014 1 - Energy   9499544.19          6426135
Belgium   BE     2015 1 - Energy  10623550.19          7514263
Belgium   BE     2016 1 - Energy  10448925.88          7142937
Belgium   BE     2017 1 - Energy   9255425.88          7795277  

我已经检查过的内容:

  • data_plot$UNemissionsdata_plot$ETSemissions 都是数字
  • 不存在 NA 值
  • 不是色标
  • 不是UNemissions有小数位

我在代码行中的labs(color="Datasets")p 之后直接得到Error: Discrete value supplied to continuous scale

这段代码过去可以工作(相同的数据),但我必须创建一个具有不同设计的新数据框。

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

for(i in (1:length(ctry))){

  plot.df <- data_plot[data_plot$country==ctry[i],]
  ets.initial <- min(plot.df$year)
  x <- plot.df$UNemissions[plot.df$year>=ets.initial & plot.df$year<2017]
  y <- plot.df$ETSemissions[plot.df$year>=ets.initial & plot.df$year<2017]
  m1 <- round(summary(lm(y~x))$r.squared,3)
  m2 <- round(lm(y~x-1)$coef,3)

  p <- ggplot() +
    geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$UNemissions, color='UN 1.A.1'), na.rm=TRUE) +
    geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$ETSemissions, color='ETS 20')) +
    annotate(geom='text',label=paste0("R^2==",m1),x=2014,y=Inf,vjust=2,hjust=0,parse=TRUE,cex=3) +
    annotate(geom='text',label=paste0("beta==",m2),x=2014,y=Inf,vjust=4,hjust=0,parse=TRUE,cex=3)+
    labs(x="Year",y="CO2 Emissions (metric tons)",z="",title=paste("Energy sector emissions for",ctry[i])) + 
    theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
    scale_color_manual(values = cols) +
    scale_y_continuous(labels = scales::comma) +
    scale_x_continuous(breaks = seq(2005, 2017, by = 5)) +
    labs(color="Datasets")
  p

  ggsave(p,filename=paste("./figures_energy/",ctry[i],".png",sep=""),width=6.5, height=6)
}

结果将如下所示(同一数据集的不同扇区)

非常感谢您的帮助!!

最好的,

康斯坦丁

【问题讨论】:

  • 据我所知,您并没有在任何地方说明您想要实现的目标,而且您在标题中提到的 NA 似乎也没有出现问题。
  • @ChrisRuehlemann 感谢您指出这一点。已添加绘图(在不同数据框中使用相同的代码)
  • 数据中的列有什么名称?而且,再一次,即使是编辑也没有明确你的目标是什么(嗯,可能可以猜到,即消除错误)以及 NA 的作用是什么。
  • @ChrisRuehlemann 抱歉,我希望现在更清楚了。所有具有 NA 值的行都已被删除。因此,NA 不再发挥任何作用。

标签: r ggplot2


【解决方案1】:

我已经运行了代码,使其可重现后,仅针对一个国家,在我的模拟数据中为“C”,并且运行顺利:

# Data:
df <- data.frame(
  country =   c(sample(LETTERS[1:5], 50, replace = T)),
  year = c(sample(2005:2015, 50, replace= T)),
  UNemissions = c(rnorm(50, 10000000, 100)),
  ETSemissions = c(rnorm(50, 10000000, 500))
)

跳过for 循环,我选择了要绘制的国家“C”:

# just country C:  
  plot.df <- df[df$country=="C",]
  ets.initial <- min(plot.df$year)
  x <- plot.df$UNemissions[plot.df$year>=ets.initial & plot.df$year<2017]
  y <- plot.df$ETSemissions[plot.df$year>=ets.initial & plot.df$year<2017]
  m1 <- round(summary(lm(y~x))$r.squared,3)
  m2 <- round(lm(y~x-1)$coef,3)

  p <- ggplot() +
    geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$UNemissions, color='UN 1.A.1'), na.rm=TRUE) +
geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$ETSemissions, color='ETS 20')) +
    annotate(geom='text',label=paste0("R^2==",m1),x=2014,y=Inf,vjust=2,hjust=0,parse=TRUE,cex=3) +
annotate(geom='text',label=paste0("beta==",m2),x=2014,y=Inf,vjust=4,hjust=0,parse=TRUE,cex=3) +
labs(x="Year",y="CO2 Emissions (metric tons)",z="",title=paste("Energy sector emissions for",ctry[i])) + 
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols) +
scale_y_continuous(labels = scales::comma) +
scale_x_continuous(breaks = seq(2005, 2017, by = 5)) +
labs(color="Datasets")
p

因此,问题可能出在forloop!

【讨论】:

  • 谢谢你。我将进一步查看导致错误的原因,因为代码用于与另一个数据帧一起使用。
猜你喜欢
  • 2014-11-14
  • 2015-11-09
  • 2018-06-20
  • 2023-03-06
  • 2014-05-27
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
相关资源
最近更新 更多