使用geom_point时,ggplot2 v2.2.0的行为是否有所不同?

时间:2016-11-26 09:30:52

标签: r plot ggplot2

我最近更新到ggplot2的2.2.0版本,但在运行我的代码时注意到与以前版本的差异。 以前,我使用下面的代码用第一个geom_point线绘制无边框圆(形状21)。随后,使用第二个geom_point行,我将使用黑色边框来勾勒特定的圆圈(在这种情况下,任何$disp > 200)。这是我自己数据的快照:

enter image description here

然而,对于新的ggplot2,我无法绘制无边界的圆圈,并像以前一样标记其他人。

有没有解决方法?

library(ggplot2)
gg <- ggplot(data=mtcars, aes(x=gear, y=mpg))
gg <- gg + geom_point(data=mtcars, aes(fill=hp, size=cyl), shape=21, col=NA)
gg <- gg + scale_fill_gradient(low="yellow", high="red")
gg <- gg + geom_point(data=mtcars[which(mtcars$disp>200),], aes(size=cyl), shape=21, col="Black")
gg

enter image description here

gg <- ggplot(data=mtcars, aes(x=gear, y=mpg))
gg <- gg + geom_point(data=mtcars, aes(fill=hp, size=cyl), shape=21)
gg <- gg + scale_fill_gradient(low="yellow", high="red")
gg <- gg + geom_point(data=mtcars[which(mtcars$disp>200),], aes(size=cyl), shape=21)
gg

enter image description here

更新 我尝试按照下面的评论添加stroke=0,但它似乎完全忽略了它。也就是说,我和上一张图片一样。

rm(gg)
gg <- ggplot(data=mtcars, aes(x=gear, y=mpg))
gg<- gg + geom_point(data=mtcars, aes(fill=hp, size=cyl), shape=21, stroke = 0)
gg
gg <- gg + scale_fill_gradient(low="yellow", high="red")
gg
gg<- gg + geom_point(data=mtcars[which(mtcars$disp>200),], aes(size=cyl), shape=21, stroke = 0)
gg

进一步测试,如果我切换行程= 2,我的边框厚度会发生变化: enter image description here

因此,只有在我将其设置为0时才会忽略。

2 个答案:

答案 0 :(得分:0)

正如我在评论中所说,你必须将 if ($btn=="btn_add") { $status="yes"; $stmt = $db_conx->prepare('UPDATE tbl_collab SET approved=? WHERE collab_userid=? AND tbl_upload_id=?'); $stmt->bind_param('sss',$status,$user_id,$id); $stmt->execute(); if($stmt){ echo "<p>user approved</p>"; } } elseif ($btn=="btn_remove"){ $status="no"; $stmt = $db_conx->prepare('UPDATE tbl_collab SET approved=? WHERE collab_userid=? AND tbl_upload_id=?'); $stmt->bind_param('sss',$status,$user_id,$id); $stmt->execute(); if($stmt){ echo "<h1>user declined</h1>"; } } 放在stroke = 0之外。这样做的缺点是aes的图例不会显示。您可以使用以下方法查看效果:

size

这导致:

enter image description here

您可以使用gg <- ggplot(data = mtcars, aes(x = gear, y = mpg)) gg <- gg + geom_point(data=mtcars, aes(fill = hp, size = cyl), shape = 21, stroke = 0) gg <- gg + scale_fill_gradient(low = "yellow", high = "red") gg <- gg + geom_point(data = mtcars[which(mtcars$disp>200),], aes(size = cyl), shape = 21, stroke = 0) gg 修复图例:

override.aes

这导致:

enter image description here

答案 1 :(得分:0)

一种可能的解决方法是将color映射到整个点图层的fill相同的变量,但对感兴趣的子集使用黑色的默认颜色。

ggplot(data = mtcars, aes(x = gear, y = mpg, size = cyl)) +  
    geom_point(aes(fill = hp, color = hp), shape = 21) + 
    scale_fill_gradient(low = "yellow", high = "red") + 
    scale_color_gradient(low = "yellow", high = "red") + 
    geom_point(data = mtcars[mtcars$disp>200,], shape = 21)