Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

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

I want to create a ggplot2-diagram with lines and bars, visualizing different values y and ybar. The lines are overlayed with dots also based on values y. There should be 2 legends in the end, one for the line (+dot) diagram, comprising colours, shapes and linestyle, and one for the bars, comprising the fill colour. The linestyles, dot-shapes and colours are changed accordingly to the variable type (y1 or y2). This works well, until I manually want to set the legend name and the item labels:

Minimal code for dataframe initialisation:

library(ggplot2)
library(reshape)
df = data.frame(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))
names(df)[1]="classes"
names(df)[2]="y1"
names(df)[3]="y2"
names(df)[4]="bary1"
names(df)[5]="bary2"
df$classes <- factor(df$classes,levels=c(10,20,40), labels=c("10m","20m","40m"))

Minimal code for creating point,line and bar diagram:

dfMelted <- melt(df)
diagram <- ggplot()
diagram <- diagram + theme_bw(base_size=16)
diagram <- diagram + geom_bar(data=subset(dfMelted,variable=="bary1" | variable=="bary2"), aes(x=factor(classes),y=value, fill=variable),stat="identity",position="dodge")
diagram <- diagram + geom_point(data=subset(dfMelted,variable=="y1" | variable=="y2"), size=4, aes(x=factor(classes),y=value, colour=variable, shape=variable)) 
diagram <- diagram + geom_line(data=subset(dfMelted,variable=="y1" | variable=="y2"), aes(x=factor(classes),y=value, group=variable, colour=variable, linetype=variable))

Preliminary result:

Looks good so far

Setting legend name / item labels via the following code:

diagram + scale_colour_brewer(name="Line Legend",labels=c("Foo","Bar")) + scale_fill_brewer(name="Bar Legend",labels=c("Foo Bar","Bar Bar"))

produces unwanted result:

Problem: legend split into color, linestyle, shape

In the final result, the two desired legend are split into three. There is no more legend, in which colours, linestyle and point shape are unified. Why's that? How to solve this? Also using scale_colour_manual() and scale_shape_manual() produces a similar result.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this does what you're asking for:

diagram + 
  scale_fill_brewer (name = "Bar Legend", labels = c("Foo Bar", "Bar Bar")) +
  scale_colour_brewer     (name = "Line Legend", labels = c("Foo", "Bar")) +
  scale_linetype_discrete (name = "Line Legend", labels = c("Foo", "Bar")) +
  scale_shape_discrete    (name = "Line Legend", labels = c("Foo", "Bar"))

Looks like the problem was that shape and line size were still being labeled in the legend with their original names, so you just have to add instructions to label them the same as the other "Line Legend" parameters.

Or, of course, you could just rename the variables in your data set...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...