2016-11-26 44 views
0

我最近更新到ggplot2的版本2.2.0,但注意到運行我的代碼時與以前的版本不同。 之前,我使用下面的代碼來繪製第一個geom_point系列的無邊框圓形(形狀21)。隨後,使用第二行geom_point,我將使用黑色邊框來勾勒特定的圓圈(在這種情況下爲$disp > 200)。下面是我自己的數據快照:當使用geom_point時,ggplot2 v2.2.0的行爲有何不同?

enter image description here

然而,隨着新GGPLOT2我不能夠繪製bordeless圓圈和其他標記像以前一樣。

是否有任何解決方法?

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

UPDATE: 我嘗試添加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。

+0

嘗試設置stroke = 0。奇怪的是,lwd = 0使點非常小 – baptiste

+0

@baptiste:使用這個沒有區別:gg < - gg + geom_point(data = mtcars,aes(fill = hp,size = cyl,stroke = 0),shape = 21) – val

+0

你必須將'stroke = 0'放在'aes'之外。例如:'geom_point(data = mtcars,aes(fill = hp,size = cyl),shape = 21,stroke = 0)' – h3rm4n

回答

0

被忽略只正如我在評論中所說的,您必須在aes之外放置stroke = 0。這個缺點是size的圖例不會顯示。通過使用override.aes

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 

這導致

gg <- gg + guides(size = guide_legend(override.aes = list(stroke = 0.5))) 
gg 

這導致:

enter image description here

+0

據推測,第二層意味着中風? – baptiste

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) 
相關問題