r - ggplot2: Shape, Color and Linestyle into one legend -


I want to create a ggplot2-diagram with lines and bars, to see various values ​​y and ybar. Lines are overlapped with dots, which are based on values. There should be 2 legends in the end, a (+ dot) diagram for the line, which includes color, shape and linestyle, and is one for bars, in which to fill the color. According to the Linestyles, dot-size and color according to the variable type (y1 or y2) it works well, as long as I want to set the name and item label for itself:

< P> Minimum code for dataframe initialization:

  Library (ggplot2) library (reshape) df = data. Frames (c (10,20,40), c (0.1.0.2.0.3) , C (0.1,0.4,0.5), C (0.05,0.1,0.2), C (0,0.2,0.4)) Name (DF) [1] = "Square" Name (DF) [2] = "y1" Name (df) [3] = "y2" name (df) [4] = "br 1" name (df) [5] = "bary2" df $ squares & l T; - factor (df $ squares, level = c (10,20,40), label = c ("10m", "20m", "40m"))  

point, line and Minimum code for creating bar diagrams:

  dfMelted & lt; - Melt diagram (DF) & lt; - ggplot () diagram & lt; - diagram + theme_bw base = size = 16) diagram and lieutenant- diagram + GOM_bar (data = subset (dfMelted, variable == "bary1" | variable == "bary2"), aes (x = factor (classes), y = Value = fill = variable) (data = subset (dfMelted, variable == "y1" | variable == "y2"), size = 4, aes (x = "y2"), size = 4, aes (x = factor) (Class), Y = value, color = variable, size = variable) diagram and diagram + diagram + Geom_line (data = subset (dfMelted, variable == "y1" | | variable == "y2"), AES X = factor (class), y = value, group = variable, color = variable, lintype = variable))  

Initial result:

  diagram + scale_colour_brewer (name = "line legend", label = c ("line legend");  

"Foo", "bar")) + scale_fill_brewer (name = "bar legend", label = c ("foo bar", "repeatedly"))

generates unwanted results :

Problem: color, linestyle, split legend into shape

In the end result, the two desired legend is divided into three, there is no legend, in which there is no legend C, linestyle was encountered and point size are integrated. This is the reason that? How to solve it? Apart from this, a similar result is generated using scale_colour_manual () and scale_shape_manual ().

I browsed through related posts and found no answer in this complex situation.

I think this is what you are asking:

  diagram + scale_fill_brewer (name = "bar legend", label = c ("foo bar", "repeatedly")) + scale_colour_brewer (name = "line legend", labels = c ("foo", "bar" ) + Scale_linetype_discrete (name = "line legend", labels = c ("foo" size "," bar ")) + scale_shape_discrete (name =" line legend ", label = c (" foo "," bar "))  

The problem seems to be that the size and line size are still being labeled if they are written with their original names. You must add instructions to label them similar to the "Line Legend" parameter.

Or, of course, you can just change the name of the variable in your data set ...


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

lua - HowTo create a fuel bar -