R:ggplot找不到对象

时间:2016-09-14 10:44:37

标签: r ggplot2

我正在尝试将图例名称添加到ggplot中,但我总是得到一个"对象未找到错误"。

  mycol = pft$colour
  myname = pft$name
  #mycol "#E5E503" "#9FFF8C" "#44CC29" "#137300" "#B2B224" "#0066CC" "#99CCFF" "#00407F" "#FF999B" "#E5171A" "#990003" "#A38FCC" "#7F40FF" "#A1E5CF" "#6B998A" "#F2F291" "#BF60A7" "#404040"
  #myname "C4 grass" "Early tropical" "Mid tropical" "Late tropical" "Temperate C3 Grass" "North Pine" "South Pine" "Late conifer" "Early hardwood" "Mid hardwood" "Late hardwood" "C3 crop" "C3 pasture" "C4 crop" "C4 pasture" "C3 grass" "Lianas" "Total"             
  test_data_long = melt(szpft[[vnam]][,ndbh+1,],
                        varnames = c("year","mpft"), na.rm = T)
  ggplot(data=test_data_long,aes(x=year,y=value, colour = mycol[mpft])) +
    geom_line(aes(group = mpft)) +
    scale_colour_identity(guide = "legend") +
    scale_fill_continuous(name="PFT", labels = myname[mpft])

test_data_longdata.frame,看起来像这样。

    year mpft     value
20  2004    2  2.294562
21  2005    2  2.415901
22  2006    2  2.532214
23  2007    2  2.649968
24  2008    2  2.760934
25  2009    2  2.849097
26  2010    2  2.967846
27  2011    2  3.102287
28  2012    2  3.244338
29  2013    2  3.386014
30  2014    2  3.528662
31  2015    2  3.675095
32  2016    2  3.828054
33  2017    2  3.976928
34  2018    2  4.133859
35  2019    2  4.305039
36  2020    2  4.488999
37  2021    2  4.673952
38  2022    2  4.861845
39  2004    3  4.518262
40  2005    3  4.668800
41  2006    3  4.821924
42  2007    3  4.973597

我想使用mpft列作为索引来定义分组,颜色,标题ecc。

mycolmyname是包含颜色(十六进制)的矢量,以及与要绘制的不同mpft行相对应的名称。我得到的确切错误是

Error in check_breaks_labels(breaks, labels) : object 'mpft' not found

删除脚本的最后一行会生成下一个数字,因此问题出在最后一行。为什么mpft在之前而不是之后被识别?

修改

更清楚,

  mycol = pft$colour
  myname = pft$name
  test_data_long = melt(szpft[[vnam]][,ndbh+1,],
                        varnames = c("year","mpft"), na.rm = T)
  ggplot(data=test_data_long,aes(x=year,y=value, colour = mycol[mpft])) +
    geom_line(aes(group = mpft)) +
    scale_colour_identity(guide = "legend") 

生成图中的图形并且没有错误。

修改

我将提供一个简化的,可重现的例子,说明我想在这里实现的目标。

mycol = c("#A38FCC","#7F40FF")
mynam = c("random_line1", "random_line2")
set.seed(123)
df=data.frame(month = month.abb, 
          mpft  = c(rep(1,6),rep(2,6)), 
          ran = runif(12,0.,10.))

这会生成包含monthmpftran值的数据框。我希望ggplot在x轴上有month,在y轴上有ran。此外,如果mycol[1]我想要用mynam[1]颜色("#A38FCC"颜色)绘制点,并使用图例显示mpft = 1作为标题(random_line1作为标题)

2 个答案:

答案 0 :(得分:3)

这与其他答案类似,但没有不必要的dplyr mumbo-jumbo,并指出了一些重要的细节。关键是如果你想要手动颜色,你应该使用手动色标。

mycol = c("#A38FCC","#7F40FF")
mynam = c("random_line1", "random_line2")
set.seed(123)
df=data.frame(
              #month needs to be an ordered factor to get correct order in the plot
              #an unordered factor would be ordered alphabetically by ggplot2
              month = ordered(month.abb, levels = month.abb), 
              mpft  = c(rep(1,6),rep(2,6)), 
              ran = runif(12,0.,10.))

library(ggplot2)

ggplot(df, aes(x=month,y=ran, 
               colour = factor(mpft) #you want a discrete color scale
               )) +
  geom_line(aes(group = mpft), size = 1) +
  scale_colour_manual(name = "mynam",
                      #always pass named character vectors here to ensure correct mapping
                      values = setNames(mycol, unique(df$mpft)), 
                      labels = setNames(mynam, unique(df$mpft))) 

enter image description here

答案 1 :(得分:0)

编辑:使用问题编辑中给出的可重现示例,提供以下代码。

A(1, 1)

如果所有行都在图中,此代码将只提供正确的颜色,如果不是,那么颜色将改变以防止使用以下代码。

library(dplyr)
df %>%  ggplot(.,aes(x=month,y=ran, colour= as.factor(mpft))) +
    geom_line(aes(group = mpft)) +
  scale_color_manual(guide = guide_legend(title = "Legend"),
                     values = mycol,
                     labels = mynam)

上面的代码块将保持您的配色方案,即使由于某种原因并非所有类型都在图中