Wednesday 15 September 2010

r - When I use stat_summary with line and point geoms I get a double legend -


I have data for 4 fields (A, B, C, D) and 5 years. I want to add 4 lines for every line, one point for each year and add a fifth row, which uses the stat_summary statement representing the line line and scale_color_manual Controls the line color through the logic problem in aces () is that if i add dot geom, the myth is divided into two parts, for point size and a The line colors. I did not understand how to get 1 legend in combination with colors and numbers

Here is an example. First we create the data frame dtfr as follows:

  a < - 100; B & L; - 100; C & lt; - 100; D [1] / 100) B [K] & lt; - b [k-1] for 10000 (k 2: 5) {a [k] & lt; - a [k-1] * (1 + rnorm (1) / 100)] [1] (1 + rnorm (1) / 100) c [k] & lt; - c [k-1] * (1 + rnorm (1) / 100) d [k] & lt; - d [k-1] * (1 + rnorm (1) / 100)} v & lt; - for numeric () (1: 5 in k) {v   

Now we should start attracting our code by ggpolt2 . In the first graph, we draw lines and without the mean line, the land is called the number:

  library (ggplot2) ggplot (dtfr, aes (x = year, y = value)) + Geom_line (AES (group = ets (color = "mean", group = 1), funny. Y = mean, GOM = "line" = GOM = dot (AES (color = sector, size = sector)) + # stat_summary AES (color = "mean", group = 1), size = 1.1) + scale_color_manual (values ​​= c ("# 004E00", "# 33FF00", "# FF9966", "# 3399FF", "#FF004C" )) + Ggtitle ("test for ggplot2 graph")   

In this article we have There is a legend with blue colors and dot shapes:

But if I have to draw the line using the following code:

Ggplot (dtfr, aes (x = year, y = value)) + geom_line (AES (group = sector, color = sector) = Geom_point (AES (color = sector, shape = sector)) + stat_summary (AES (color = "mean", group = 1), funny. Y = mean, GOM = "line", size = 1.1) + scale_color_manual ("# 004E00", "# 33FF00", "# FF9966", "# 3399FF "," #FF004C ")) + ggtitle (" test for ggplot2 graph ")

Their meaning is red (red) but the mythology is divided into two parts, one for line colors and one for the dot shape. My question at this point is: How do I get a median line graph with a kind of legend in the first graph? That is, how does one get a legend in which the lines and shapes are combined in the second graph where the mean line is drawn?

Try this:

  ggplot (dtfr, aes (x Mean = "mean", size = "+" = (, = y = value)) + geom_line (AES (group = sector, color = sector)) + geom_point (AES color = sector, size = sector)) + stat_summary (AES (color) = ", Group = 1), funny. Y = mean, geom =" line ", size = 1.1) + scale_color_manual (value = C (" # 004E00 "," # 33FF00 "," # FF9966 "," # 3399FF " "#FF004C") + scale_shape_manual (values ​​= c (1: 4, 32)) + ggtitle ("Ggplot2 graph")   

 ggplot2 example with a composite story

Maybe someone else Can know and fix my explanation (or provide better solution), but here's how I understand it: Your color scale has 5 values, but only 4 in the size of your size Are; If you are missing a value for "mean" then the scales are not actually compatible in a way. You can fix this by specifying your mean line by specifying an empty size (32).

No comments:

Post a Comment