在R中的特定点添加边框

时间:2019-02-06 00:52:46

标签: r ggplot2 border point

我只想为colunmn'exot'的第1级添加边框。我在很多网站上都在寻找,但是我只找到了一般如何界线(pch等)的说明。图1是一个示例(在Photoshop中制作),它说明了我如何在图形中添加边框。

感谢您的任何帮助

library(ggthemes)
library(ggplot2)

p<- ggplot(Dataset, aes(sp,log(num))) 
p + geom_point(aes(colour=recal, size = pf))+
  scale_fill_continuous() +
  scale_size_continuous(range = c(4,10)) +
  ggthemes::theme_few() +
  theme(axis.text.x = element_text(angle = 90, vjust = .5))

sp  num recal   pf  exot
A   47  2   7   0
B 22    0   3   0
C   5   0   0   0
D   4   0   0   0
E   2   0   0   0
F   2   0   0   0
G   2   0   0   1
H   2   0   0   0
I   1   0   1   0
J   1   0   0   0
L 1 5   0   0
M   1   0   0   0
N   1   0   0   0
O   1   0   0   0
P   1   0   0   0
Q   1   0   0   0
R   1   0   0   0
S   1   0   0   1
T   1   0   0   1
U   1   0   0   1

Figure 1

1 个答案:

答案 0 :(得分:1)

一种更接近您的解决方案是对点使用shape=21并将颜色设置为exot(注意,现在的颜色是指边框)。

使用scale_manual将值设置为"white""red",然后删除图例:

library(ggthemes)
library(ggplot2)

ggplot(dataset, aes(sp,log(num))) + 
  geom_point(aes(fill=recal, size = pf, color=as.factor(exot)), shape = 21, stroke = 2)+
  scale_fill_continuous() +
  scale_size_continuous(range = c(4,10)) +
  scale_colour_manual(values=c("white", "red")) + # set the border to the bg color
  ggthemes::theme_few() +
  guides(color = FALSE) + # remove the legend for the border
  theme(axis.text.x = element_text(angle = 90, vjust = .5))

enter image description here


如果您仍想为"pf"的图例使用“全”点,请使用以下方法:

library(ggthemes)
library(ggplot2)

ggplot(dataset, aes(sp,log(num))) + 
  geom_point(aes(fill=recal, size = pf, color=as.factor(exot)), shape = 21, stroke = 2)+
  scale_fill_continuous() +
  # change guide for the size 
  scale_size_continuous(range = c(4,10), guide=guide_legend(override.aes = list(shape=19))) + 
                                         # ^this part (forces the shape to 19)
  scale_colour_manual(values=c("white", "red")) + # set the border to the bg color
  ggthemes::theme_few() +
  guides(color = FALSE) + # remove the legend for the border
  theme(axis.text.x = element_text(angle = 90, vjust = .5))

enter image description here

数据:

tt <- "sp  num recal   pf  exot
A   47  2   7   0
B 22    0   3   0
C   5   0   0   0
D   4   0   0   0
E   2   0   0   0
F   2   0   0   0
G   2   0   0   1
H   2   0   0   0
I   1   0   1   0
J   1   0   0   0
L 1 5   0   0
M   1   0   0   0
N   1   0   0   0
O   1   0   0   0
P   1   0   0   0
Q   1   0   0   0
R   1   0   0   0
S   1   0   0   1
T   1   0   0   1
U   1   0   0   1"

dataset <- read.table(text=tt, header=T)