如何使用符号ggplot更改图例

时间:2016-06-26 15:35:29

标签: r

我在改变剧情中的传奇方面遇到了一些问题。由于我正在绘制符号,我希望我的传奇也带有符号。它应该很简单,但我不知道它是最好的方式。 我附上了我的数据样本:

        dat <- dput(dmam[1:3,])
         structure(list(A = c(20.3962664097025, 23.1851978051035, 24.100067818374
         ), C = c(21.3308712003524, 18.9836301631049, 18.0438507268565
         ), E = c(4.65577231445595, 5.31932828758369, 7.38133268738002
         ), N = c(7.27444462203666, 7.05405386380459, 6.86223246170241
         ), NE = c(3.17580210229611, 3.61284920419648, 4.61289092127168
         ), NW = c(9.24999105407481, 7.474673429406, 5.15097610969423), 
         S = c(7.5472564395321, 7.66985191633565, 8.38928522440817
         ), SE = c(2.93704709971095, 2.86345108676898, 4.23877287952622
         ), SW = c(6.66026053678963, 6.92699394482923, 5.47572225923119
         ), U = c(3.66624356005668, 5.29918120412581, 8.3455794646421
         ), W = c(13.1060448390873, 11.6107893469172, 7.3992896902522
         ), mod = c("BNU-ESM", "CCSM4", "CESM1-CAM5"), id = 1:3), .Names =  c("A", 
         "C", "E", "N", "NE", "NW", "S", "SE", "SW", "U", "W", "mod", 
         "id"), row.names = c("BNU-ESM", "CCSM4", "CESM1-CAM5"), class =   "data.frame")

我做了什么:

          df  <- melt(dat,id=c('mod','id'))
          s <- ggplot(df, aes(variable,value))
          s <- s + geom_point(aes(shape = id,colour=mod), size = 3) + 
                   scale_shape_identity()

但传说中没有显示符号......

1 个答案:

答案 0 :(得分:1)

id放入一个因子并放下scale_shape_identity,然后它将绘制一个图例。

s <- ggplot(df, aes(variable, value))
s <- s + geom_point(aes(shape = as.factor(id), colour = mod), size = 3) 
s