Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Different legend-keys inside same legend in ggplot2

Let's say I don't need a 'proper' variable mapping but still would like to have legend keys to help the chart understanding. My actual data are similar to the following df

df <- data.frame(id = 1:10, line = rnorm(10), points = rnorm(10))

library(ggplot2)

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, colour = "points"))

enter image description here

Basically, I would like the legend key relative to points to be.. just a point, without the line in the middle. I got close to that with this:

library(reshape2)

df <- melt(df, id.vars="id")

ggplot() +
  geom_point(aes(id, value, shape = variable), df[df$variable=="points",]) +
  geom_line(aes(id, value, colour = variable), df[df$variable=="line",])

but it defines two separate legends. Fixing the second code (and having to reshape my data) would be fine too, but I'd prefer a way (if any) to manually change any legend key (and keep using the first approch). Thanks!

EDIT :

thanks @alexwhan you refreshed my memory about variable mapping. However, the easiest way I've got so far is still the following (very bad hack!):

df <- data.frame(id = 1:10, line = rnorm(10), points = rnorm(10))

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, shape = "points")) +
  theme(legend.title=element_blank())

which is just hiding the title of the two different legends.

enter image description here

Other ideas more than welcome!!!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can use override.aes= inside guides() function to change default appearance of legend. In this case your guide is color= and then you should set shape=c(NA,16) to remove shape for line and then linetype=c(1,0) to remove line from point.

ggplot(df) +
  geom_line(aes(id, line, colour = "line")) +
  geom_point(aes(id, points, colour = "points"))+
  guides(color=guide_legend(override.aes=list(shape=c(NA,16),linetype=c(1,0))))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...