简体   繁体   English

初始化和自定义自动绘图r-project对象

[英]Initializing and customizing an autoplot r-project object

My head is getting sore from me banging it so much. 我的脑袋撞得很厉害。

I have a time-series that I've converted into an xts object w/ 7 variables. 我有一个已转换为带有7个变量的xts对象的时间序列。 Now I'm trying to plot 4 of them, all price indices, on the same graph. 现在,我试图在同一张图上绘制其中的4个价格指数。 I used autoplot (from the ggfortify package) to initialize the graph, and this is where the trouble begins. 我使用了自动绘图(来自ggfortify包)来初始化图形,这就是麻烦的开始。

Autoplot doesn't seem to work unless I give it at least one variable to plot. 除非我给它至少一个要绘制的变量,否则自动绘图似乎不起作用。 That's fine, but the two customizations I want for the variable -- its color and line type -- seem to have no effect. 很好,但是我想要的两个自定义变量(颜色和线条类型)似乎没有效果。

But once I create the plot this way, I have little trouble adding the other 3 variables by adding geom_lines. 但是,一旦我以这种方式创建了绘图,就可以通过添加geom_lines添加其他3个变量。 Here's sort of what the code looks like: 下面是代码的样子:

p <- autoplot(foo.xts,xlab="Year",
         ylab="Price Index",
         columns="Variable1",linetype=4) # the linetype accomplishes nothing
p <- p + geom_line(aes(y="Variable2", color="green", linetype="solid"
       # etc. for the other 2 variables
p # The 3 added variables do get the selected colors & line types.

But how can I customize the line for the first variable? 但是,如何为第一个变量自定义行?

Then there's another problem in that I can't get a legend to appear. 然后还有另一个问题,就是我无法显示图例。 Here's how I'm trying to do that: 这就是我要这样做的方式:

p <- p + scale_color_discrete(
     name="Price Indices",
     breaks=c("Variable1", "Variable2", "Variable3", "Variable4"),
     labels=c("Index 1", "Index 2", "Index 3", "Index 4"))

This seems to accomplish nothing. 这似乎无济于事。

One thing I'd add is that in my various experiments trying to get the legend to work, I've sometimes gotten two sets of keys: one for colors and one for line types. 我要添加的一件事是,在各种尝试使图例生效的实验中,有时会得到两组键:一组用于颜色,另一组用于线型。 This is obviously not what I'm after. 这显然不是我所追求的。

If someone could help me with this, I'd be forever in your debt! 如果有人可以帮助我,我将永远在您的债务中!

I spent yesterday away from the computer, and when I returned in the evening fixed the problems. 昨天我不在电脑上度过,晚上回来时解决了这些问题。 Here's how: 这是如何做:

  1. Stopped using autoplot. 使用自动绘图停止。 It's a classic case of hand-holding that throws you over the cliff. 这是一个经典的手握案例,可将您扔下悬崖。 In other words, it automatically formats the plot in ways that are difficult (impossible?) to customize. 换句话说,它会以难以(不可能)自定义的方式自动设置图的格式。 Instead, ggplot makes the initial plot. 而是由ggplot绘制初始图。

  2. Since I'm making a series of plots, moved all the shared features to a separate, preamble section. 由于我要进行一系列绘图,因此将所有共享功能移至单独的前言部分。 This section creates a base plot, sets the x-axis variable (the date of the observation), labels the x-axis, and formats its tick marks. 本部分创建基本图,设置x轴变量(观测日期),标记x轴,并设置其刻度线格式。 It also sets up standardized colors, line styles, and shapes to be used by all the "production" plots. 它还设置了标准化的颜色,线条样式和形状,供所有“生产”图使用。

  3. To set up the standardized elements, it uses scale_color_manual, etc. Each one has to be identical in all respects except those that are unique to its specific aesthetic attribute. 要设置标准化元素,它使用scale_color_manual等。每个元素在各个方面都必须相同,但对于其特定美学属性而言是唯一的。 Eg, scale_color_manual uses values like "red" whereas scale_linetype_manual uses values like "solid." 例如,scale_color_manual使用类似“ red”的值,而scale_linetype_manual使用类似“ solid”的值。 Each manual setting includes the following elements: legend.title*, values, labels, and guide = guide_legend()*. 每个手动设置都包含以下元素:legend.title *,值,标签和guide = guide_legend()*。 (Items marked with * must be identical, otherwise you'll get different legends for each one.) For each plot, the actual legend title is first stored in a variable, legend.title, and then used in all the manual scale setting. (标有*的项目必须相同,否则,每个图例将获得不同的图例。)对于每个图,实际的图例标题首先存储在变量legend.title中,然后在所有手动比例设置中使用。 This way the manual settings can be moved to the common section, but each plot has is own unique title for its legend. 这样,可以将手动设置移到公共部分,但是每个图都有其图例自己的唯一标题。

3A. 3A。 Actually, I was wrong about this. 实际上,我对此是错误的。 I was thinking LaTeX, where most things are evaluated where they appear at execution time. 我当时在想LaTeX,其中评估了大多数事物在执行时出现的位置。 So a scale_color_manual statement at the start could change later on just by changing the value of legend.title. 因此,一开始的scale_color_manual语句可以稍后更改,只需更改legend.title的值即可。 But in R, things are evaluated sequentially, and changing legend.title after the scale_color_manual statement is executed will have no effect. 但是在R中,事物将按顺序进行求值,并且在执行scale_color_manual语句后更改legend.title将无效。 I worked around this by defining several variables in the preamble (eg, one with the colors I'm using) and then using these variables in the various source_x_manual statements. 为此,我在序言中定义了几个变量(例如,使用我正在使用的颜色的变量),然后在各种source_x_manual语句中使用了这些变量。 This way, the only thing that change is the legend title. 这样,唯一更改的是图例标题。

  1. Then each production plot starts by copying the base plot, labeling the y-axis, and then adds the geometric objects that it needs. 然后,每个生产图首先复制基本图,标记y轴,然后添加所需的几何对象。

This approach has several advantages. 这种方法有几个优点。 1) It modularizes the plotting so that problems are easier to isolate and solve, and most solved problems in the preamble section are solved for all plots. 1)它使绘图模块化,以便更容易隔离和解决问题,并且对所有绘图都解决了序言部分中解决最多的问题。 2) It standardizes the plots, ensuring that their common features are formatted identically. 2)标准化图,确保其共同特征的格式相同。 3) It reduces each production plot to a few statements; 3)将每个生产图简化为几个语句; since this is the unique part for each plot, creating a new style of plot becomes relatively easy. 由于这是每个图的唯一部分,因此创建新样式的图变得相对容易。 4) The value added by autoplot becomes minimal because this approach, separating shared elements in a preamble, compensates by isolating reusable code and the preamble, once debugged, allows much more fine-grain customization. 4)自动绘图增加的值变得最小,因为这种方法将前同步码中的共享元素分开,通过隔离可重用的代码进行补偿,并且一旦调试后,前同步码允许进行更细粒度的自定义。

If you have any questions, please feel free to ask. 如有任何疑问,请随时提问。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM