简体   繁体   English

“错误:美学的长度必须为1或与数据相同”为什么?

[英]“Error: Aesthetics must be either length 1 or the same as the data” Why?

Wickham (2009: 164-6) gives an example of plotting multiple time series simultaneously. Wickham(2009:164-6)给出了同时绘制多个时间序列的示例。 The following code replicates his example: 以下代码复制了他的示例:

  range01 <- function(x) {
      rng <- range(x, na.rm = TRUE)
      (x - rng[1])/diff(rng)
  }

  emp <- subset(economics_long, variable %in% c("uempmed", "unemploy"))
  emp2 <- ddply(emp, .(variable), transform, value = range01(value))
  qplot(date, value, data = emp2, geom = "line", color = variable, linetype =     variable)
  # Produces a plot that looks like the one on p. 166 of the ggplot2 book.

Here range01 is used to recode variables to values within [0,1] so series with different orders of magnitude can be plotted on identical scales. 在此,range01用于将变量重新编码为[0,1]内的值,因此可以在相同的比例尺上绘制不同数量级的序列。 Wickham's original also starts with the employment data provided with ggplot2 and melts it into long form, but here I've taken the shortcut of starting with the employment_long version. 韦翰的原也开始与employment提供GGPLOT2数据,并将其融入多头形态,但在这里我已经采取了与起始的快捷employment_long版本。

But Wickham (p. 27) also points out that tapping the "full power" of ggplot2 requires manual construction of plots by layers, using the ggplot() function. 但是Wickham(第27页)也指出,利用ggplot2的“全功能”需要使用ggplot()函数逐层手动构建图。 Here is his example again but using ggplot() instead of qplot() : 这再次是他的示例,但是使用ggplot()而不是qplot()

# Now do the same thing using ggplot commands only
ggplot(data = emp2, aes(x = date)) +
  geom_line(aes(y = value, group = variable, color = variable, linetype = variable))
# Get the same results

Both examples take advantage of ggplot2 's default settings. 这两个示例都利用了ggplot2的默认设置。 But suppose we want greater control over the aesthetics. 但是,假设我们希望对美学有更好的控制。 Perhaps some variables lend themselves to particular color schemes (eg, green might be used for environmentally friendly variables and black, for detrimental ones); 也许某些变量适合特定的配色方案(例如,绿色可用于环保变量,黑色可用于有害变量); or perhaps in a long monograph with many plots we just want to ensure consistency. 或在包含许多情节的长篇专着中,我们只想确保一致性。 Furthermore, if the plots will be used both in presentations and printed black-and-white text, we may also want to associate specific line types with particular series; 此外,如果在演示文稿和印刷的黑白文本中都将使用这些图,则我们可能还希望将特定的线型与特定的系列相关联。 this could also be the case if we are concerned about viewers with color blindness. 如果我们关注色盲观众,也可能是这种情况。 Finally, variable names usually are poor descriptors of what variables really are, so we want to associate variable labels with the individual time series. 最后,变量名通常不能很好地描述变量的真正含义,因此我们希望将变量标签与各个时间序列相关联。

So we define the following for the economics dataset: 因此,我们为经济学数据集定义以下内容:

# Try to control the look a bit more
economics_colors = c("pce" = "red", "pop" = "orange", "psavert" = "yellow",
    "uempmed" = "green", "unemploy" = "blue")
economics_linetypes = c("pce" = "solid", "pop" = "dashed", "psavert" = "dotted",
    "uempmed" = "dotdash", "unemploy" = "longdash")
economics_labels = c(
    "pce" = "Personal consumption expenditures",
    "pop" = "Total population",
    "psavert" = "Personal savings rate",
    "uempmed" = "Median duration of unemployment",
    "unemploy" = "Number of unemployed"
)

Now construct the plot by adding separate layers (Wickham 2009: 164-5) for each variable: 现在,通过为每个变量添加单独的图层(Wickham 2009:164-5)来构建图:

# First do it line-by-line
employment.plot <- ggplot(emp2) + aes(x = date)  +
  scale_linetype_manual(values = economics_linetypes, labels = economics_labels)
employment.plot <- employment.plot +
  geom_line(data = subset(emp2, variable == "uempmed"),
      aes(y = value, linetype = "uempmed"), color = economics_colors["uempmed"])
employment.plot <- employment.plot +
  geom_line(data = subset(emp2, variable == "unemploy"),
            aes(x = date, y = value, linetype = "unemploy"), color = economics_colors["unemploy"])
employment.plot
# Except for the specific line colors, produces the same plot as before.

Notice two things here. 注意这里两件事。 First, line types are mapped but colors are set (see Wickham 2009: 47-49). 首先, 绘制线型,但设置颜色(请参见Wickham 2009:47-49)。 This produces the desired result of a single legend with distinct color-linetype combinations for each series. 这会产生单个图例的期望结果,并为每个系列提供不同的颜色-线型组合。

Second, even though the data are organized in "long" format, we used subset to select out individual series. 其次,即使数据是以“长”格式组织的,我们也使用子集来选择单个序列。 This is not the best solution. 这不是最佳解决方案。 As Wickham (164-5) says: 正如威克汉姆(164-5)所说:

... a better alternative is to melt the data into a long format and then visualize that. ...更好的选择是将数据融合为长格式,然后将其可视化。 In the molten data the time series have their value stored in the value variable and we can distinguish between them with the variable variable. 在熔融数据中,时间序列将其值存储在value变量中,我们可以使用变量变量区分它们。

So let's try this approach: 因此,让我们尝试这种方法:

# Now try it the automatic way
employment.plot <- ggplot(data = emp2, aes(x = date))  +
  scale_linetype_manual(values = economics_linetypes, labels = economics_labels)
employment.plot <- employment.plot +
  geom_line(aes(y = value, group = variable, linetype = economics_linetypes), color = economics_colors)
employment.plot
# Throws "Error: Aesthetics must be either length 1 or the same as the data (1148) ..."

As the comment indicates, this code throws an error regarding the aesthetics. 如注释所示,此代码引发有关美观的错误。 Why? 为什么?

Also, is there another way to accomplish the multiple goals of using melted data with the single variable variable triggering separate lines, controlling which colors and line types are associated with each series, and using code to standardize such conventions across multiple plots? 另外,还有另一种方法可以实现多个目标,即使用具有单个变量变量的熔融数据触发单独的线条,控制与每个系列关联的颜色和线条类型,以及使用代码在多个图中标准化这些约定吗?

References 参考文献

Wickham, Hadley. 威克姆,哈德利。 2009. ggplot2: Elegant Graphics for Data Analysis . 2009。ggplot2:用于数据分析的精美图形 Springer. 施普林格。

The aesthetics should always be mapped to a dimension of the dataset. 美学应该始终映射到数据集的维度。

What you are saying with the last command is: "For each 'data point' (or group in this case) assign a linetype equal to its economics_linetypes ." 您在最后一条命令中所说的是:“为每个'数据点'(在本例中为组)分配一个等于其economics_linetypes的线型。

But there is not (yet) information on how to map each record (group) to any value in economics_linetypes . 但是(目前)还没有关于如何将每个记录(组)映射到economics_linetypes任何值的信息。 So it rightly return an error. 因此它正确地返回一个错误。

What you should do is map the linetype to the dimension that controls it. 您应该做的是将linetype映射到控制它的尺寸。 That is: "for each value in this dimension, use a different value of linetype " ie: 也就是说:“对于此维度中的每个值,请使用不同的linetype值”,即:

geom_line(aes(y = value, group = variable, linetype = variable)

Once we have that defined we can map the value of variable to a specific linetype with the definition of a scale: 一旦定义好了,就可以使用scale的定义将变量的值映射到特定的linetype

scale_linetype_manual(values = economics_linetypes, labels = economics_labels)

All of this appplies to color as well of course, so at the end we have: 当然,所有这些都适用于颜色,因此最后我们有了:

employment.plot <- ggplot(data = emp2, aes(x = date))  +
    geom_line(aes(y = value, group = variable, linetype = variable, color = variable)) +
    scale_linetype_manual(values = economics_linetypes, labels = economics_labels) +
    scale_color_manual(values = economics_colors, labels = economics_labels)

在此处输入图片说明

Hope this is clear enough. 希望这足够清楚。

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

相关问题 错误:美学的长度必须为1或与数据相同(4) - Error: Aesthetics must be either length 1 or the same as the data (4) 错误:Aesthetics 必须是长度为 1 或与数据 (13) 相同:ymax - Error: Aesthetics must be either length 1 or the same as the data (13): ymax 错误:美学必须是长度1或与数据相同(n):填充 - Error: Aesthetics must be either length 1 or the same as the data (n): fill 错误:美学必须是长度 1 或与数据 (5) 相同:y - Error: Aesthetics must be either length 1 or the same as the data (5): y ggplot2错误:美学长度必须为1或与数据相同(24) - ggplot2 error: Aesthetics must be either length 1 or the same as the data (24) 与“错误:美学必须长度为1或与数据相同”相关的问题 - Questions associated with “Error: Aesthetics must be either length 1 or the same as the data” 错误:美学必须是长度1或与数据相同(9):label - Error: Aesthetics must be either length 1 or the same as the data (9): label 错误:美学长度必须为 1 或与数据 (2190) 相同:x - Error: Aesthetics must be either length 1 or the same as the data (2190): x Shiny 应用程序错误:美学必须是长度 1 或与数据 (1) 相同:x - Shiny App Error: Aesthetics must be either length 1 or the same as the data (1): x 错误:美学必须是长度为1或与数据(1)相同:x和y - Error: Aesthetics must be either length 1 or the same as the data (1): x and y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM