围绕点放置边框 [英] Place a border around points
问题描述
我希望在散点图上的点上放置一个黑色边框,并使用ggplot2根据数据填充散点图。另外,我想避免为黑色边框添加图例条目,因为它会在每个点上。基本上我正在寻找这个情节,但是在每个点周围都有一个黑色边框。
df < - data.frame(id = runif(12),x = 1:12,y = runif(12 ))
ggplot(df,aes(x = x,y = y))+ geom_point(aes(color = id),size = 12)
作为奖励,我希望没有黑色边框的图例条目。我最好的尝试是:
df < - data.frame(id = runif(12),x = 1:12, y = runif(12))
。)
ggplot(df,aes(x = x,y = y))+ geom_point(aes(fill = id,color =black),size = 12)
$ b
我不明白为什么这样做 t给我我想要的,更糟的是(对于我在ggplot2中的教育)我不明白为什么它似乎没有将填充颜色映射到任何东西!任何帮助?
也许如果我可以得到轮廓和填充映射的权利,我可以使用像最后一组数字
此处可关闭图例。 解决方案这有点模糊,但你必须使用
pch
> 20(我认为21:25是相关的形状):fill
控制内部colo(u)环和color
控制边缘线。(g0 <-ggplot(df,aes(x = x,y = y))+ geom_point(aes(fill = id),
color = black,pch = 21,size = 5))
更新 :使用最近的ggplot2版本(例如2.0.0,不知道它有多远),默认指南是一个色彩棒。需要
g0 +指南(fill =legend)
才能获得带点的图例,如图所示。默认的休息时间也发生了变化:要精确地复制此图,您需要g0 + scale_fill_continuous(guide =legend,breaks = seq(0.2,0.8,by = 0.1) )
...
你应该把
color
外映射(aes
bit),如果你想绝对地设置它而不是根据某个变量的值...
I'd like to place a black border around points on a scatterplot that are filled based on data, using ggplot2. Also, I would like to avoid having a legend entry for the black border since it will be on each point. Basically I'm looking for this plot, but with a black border around each point.
df <- data.frame(id=runif(12), x=1:12, y=runif(12)) ggplot(df, aes(x=x, y=y))+geom_point(aes(colour=id), size=12)
As a bonus, I'd like to not have a legend entry for the black border. My best try is:
df <- data.frame(id=runif(12), x=1:12, y=runif(12)) ggplot(df, aes(x=x, y=y))+geom_point(aes(fill=id, colour="black"), size=12)
Which gives:
I don't understand why that doesn't give me what I want, and worse (for my education in ggplot2) I don't understand why it doesn't seem to map fill color to anything! Any help?
Perhaps if I can get the outline and fill mapping right I can use a hack like the one in hte last set of figures here to turn off the legend.
解决方案It's a bit obscure, but you have to use
pch
>20 (I think 21:25 are the relevant shapes):fill
controls the interior colo(u)ring andcolour
controls the line around the edge.(g0 <- ggplot(df, aes(x=x, y=y))+geom_point(aes(fill=id), colour="black",pch=21, size=5))
update: with recent ggplot2 versions (e.g. 2.0.0, don't know how far back it goes) the default guide is a colourbar. Need
g0 + guides(fill="legend")
to get a legend with points as in the plot shown here. The default breaks have changed, too: to exactly replicate this plot you needg0 + scale_fill_continuous(guide="legend",breaks=seq(0.2,0.8,by=0.1))
...Related but not identical: how to create a plot with customized points in R? . The accepted answer to that question uses the layering technique shown in @joran's answer, but (IMO) the answer by @jbaums, which uses the
pch=21
technique, is superior. (I thinkshape=21
is an alternative, and perhaps even preferred, topch=21
.)PS you should put
colour
outside the mapping (aes
bit) if you want to set it absolutely and not according to the value of some variable ...这篇关于围绕点放置边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!