在ggplot上更改图例

时间:2015-01-12 00:39:16

标签: r

time<- as.POSIXct(c("2014-12-10 20:51:53.103","2014-12-10 20:56:54.204","2014-12-10 20:57:54.204"), tz= "GMT")
p<-c(49.32, 60,50)
s<-c("B","","S")
pointcolor<-c("green","black","red")
share<-c(35,0,6)
pointsize<-c(10,5,10)
shapeType<-c(16,10,16)
bigDF<-data.frame(time=time, p=p, s=s, pointcolor=pointcolor, share=share, pointsize=pointsize, shapeType=shapeType)
bigDF
#ggplot(bigDF, aes(x=time, y=p)) + geom_line() + 
 # geom_point( aes(shape = as.factor(shapeType), 
  #                size = as.factor(pointsize),
   #               color = pointcolor)) +
  #scale_color_manual(values = c("black", "green", "red")) +
  #scale_size_manual(values = 10)

ggplot(bigDF, aes(x=time, y=p)) + geom_line() + 
  geom_point( aes(shape = as.factor(shapeType),
                  size = as.factor(pointsize), 
                  colour = pointcolor)) +
  scale_color_manual(values = levels(as.factor(bigDF$pointcolor))   )

当你绘制它时,你会看到:

enter image description here

我想删除shapetype和pointsize legends,所以我这样做:

将这些2添加到图表中:

ggplot(bigDF, aes(x=time, y=p)) + geom_line() + 
  geom_point( aes(shape = as.factor(shapeType),
                  size = as.factor(pointsize), 
                  colour = pointcolor)) +
  scale_color_manual(values = levels(as.factor(bigDF$pointcolor))   )+ scale_shape_identity(guide="none") + scale_size_identity(guide="none")

我收到错误:

Error: non-numeric argument to binary operator

这是由于scale_size_identity(指南=&#34;无&#34;)。你知道怎么删除pointize传奇吗?

另外 - 你知道如何更改颜色图例,这样就不会读取黑色,绿色,红色,而只会说&#34;购买&#34;绿色和&#34;销售&#34;红色?

谢谢。

1 个答案:

答案 0 :(得分:1)

levels(bigDF$pointcolor) <- c(NA, "Buy", "Sell")

ggplot(bigDF, aes(x=time, y=p)) + geom_line() + 
    geom_point( aes(shape = as.factor(shapeType),
                    size = as.factor(pointsize), 
                    colour = pointcolor)) +
    scale_color_manual(name = "Status", values = c("Sell" = "red", "Buy" = "green"))+ scale_shape_discrete(guide=FALSE) + scale_size_discrete(guide = FALSE)

enter image description here