1

I want to change the shape and size of a points on 2 lines and I can do that but a second legend appears.

When you run this code you will see 2 legends. I only want the "variable" legend.

library("ggplot2")
MyData<-data.frame(time= c(1,2,3,1,2,3), value = c(.4,.6,.7,.1,.2,.3), variable =         c("company a","company a","company a","company b","company b","company b")      )
MyData$pointsize <- ifelse(MyData$time==2, 5, 1)
MyData$shape <- ifelse(MyData$time==2, 4, 7)
MyData
ggplot(MyData, aes( x = time,  y=value,colour=variable, group= variable)  )   +           geom_line()   + geom_point(aes(shape = MyData$pointsize,size = MyData$pointsize) )+
  scale_shape_identity()

How do I remove the legend for Mydata$PointSize?

Thank you!

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • In addition to the answer below, you should know that you should really never be specifying columns like `Mydata$PointSize` inside of `aes`. You should only be mapping to the column name, `pointsize`. – joran Apr 10 '14 at 17:17

3 Answers3

2

You can set guide=FALSE inside the scale_size_continuous() to remove this legend.

+scale_size_continuous(guide=FALSE)
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
0

looks like the answer is to do: ........ + scale_shape_identity(guide="none")+scale_size_identity(guide="none")

user3022875
  • 8,598
  • 26
  • 103
  • 167
  • If you are using `scale_size_identity()` then there is no need for argument `guide="none"` because it is default value. Also identity scales are used only if the values should be used directly as they are. – Didzis Elferts Apr 10 '14 at 17:20
0

You can use the guides option. Use the aesthetics that appear as legend as parameter and set them to FALSE.

guides(colour=FALSE) would remove the one legend, guides(size=FALSE) the other one and guides(colour=FALSE, size=FALSE) would remove both.

library("ggplot2")
MyData<-data.frame(time=c(1,2,3,1,2,3),
                   value=c(.4,.6,.7,.1,.2,.3), 
                   variable=c("company a","company a","company a","company b","company b","company b"))
MyData$pointsize <- ifelse(MyData$time==2, 5, 1)
MyData$shape <- ifelse(MyData$time==2, 4, 7)
MyData
ggplot(MyData, aes(x=time, y=value,colour=variable, group=variable)) +
  geom_line() +
  geom_point(aes(shape=MyData$pointsize, size=MyData$pointsize)) +
  scale_shape_identity() + 
  guides(colour=FALSE, size=FALSE)

Note: The following is an old answer, which only refers to the initial question that was revised in between! It does no longer apply to the above question... Due to the mapping (aesthetics), you have two groups, thus all "time", "shape" and "pointsize" values appear twice (one time for each group). So you have 3 values for each group (line). However, you supply 6 values to geom_point, as you don't specify aesthetics. The data mapping for the plot using aes and the data used for geom_point have different lengths. A solution would be:

ggplot(MyData, aes(x=time, y=value, colour=variable, group=variable))+geom_line()+ geom_point(aes(shape=factor(shape), size=pointsize))
Daniel
  • 7,252
  • 6
  • 26
  • 38
  • You're right - I'm not sure whether I'm still in the right thread, I thought I read something about "error length aesthetics"... – Daniel Apr 10 '14 at 17:51
  • If these are the revisions: http://stackoverflow.com/posts/22994121/revisions my answer refers to the initial question... – Daniel Apr 10 '14 at 17:52