【R语言】ggplot2---散点图的边框和颜色填充问题

在使用ggplot2绘制散点图的时候,在geom_point()图层中,边框和填充色的设置出现问题,可能出现无法设置边框色或者填充色。


总结:

  1. 1-20只能调整colour,即
#画图
ggplot+
geom_point(aes(colour = ...)) =

#自定义填充
scale_color_manual()
  1. 21-25既可以调整边框又可以填充,即
#画图
ggplot+
geom_point(aes(fill = ?,color = ?)) +
#边框自定义设置
scale_color_manual()+
#填充自定义设置
scale_fill_manual()

详细讲解:

  • 解决方法:在R中绘制散点的时候,常见的散点形状有25种,其中1至25号形状均只可调整colour属性,而21至25号形状既可以调整边框色也可以设置填充色,即fill和colour同时设置
df <- data.frame(x = rnorm(25), y = rnorm(25), 
                 z = paste0("shape", 1:25), group = rep(paste0("group", 1:5), 5))
df$z <- factor(df$z, levels = paste0("shape", 1:25))
ggplot(df, aes(x, y, shape = z)) + 
  geom_point(colour = "red", size = 5) + 
  scale_shape_manual(values = 1:25) + 
  labs(title = "Setting colour = \"red\" for all shapes")
ggplot(df, aes(x, y, shape = z)) + 
  geom_point(fill = "red", size = 5) + 
  scale_shape_manual(values = 1:25) + 
  labs(title = "Setting fill = \"red\" for all shapes")

在这里插入图片描述
在这里插入图片描述
因此,从上面可以看出,如果我们需要绘制边框色与填充色不一样的点形状,应该选择21至25号形状。比如,在df数据框中,根据group列填充颜色,如果选择15至20,则无法出现边框,只有选择21至25,将group列映射至fill,再设置colour。同时,对于21至25,还可以通过stroke改变边框厚度

ggplot(df, aes(x, y, colour = group)) + 
  geom_point(size = 5, shape = 16) + # default "black" 
  labs(title = "Shape with no border, shape = 16")
ggplot(df, aes(x, y, fill = group)) + 
  geom_point(colour = "black", size = 5, shape = 21) + 
  labs(title = "Shape with black border, shape = 21")
ggplot(df, aes(x, y, fill = group)) + 
  geom_point(colour = "black", size = 5, shape = 21, stroke = 2) + 
  labs(title = "Change border thickness, shape = 21")

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初于青丝mc终于白发

老爷们,觉得好的给个打赏呗!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值