R語言的ggplot2包畫散點圖如果想給不同的變量映射形狀的話,可以選擇的形狀如下

這個雖然看起來有很多,但是個人認為可用的就只有4個,就是15,16,17,18,如果超過四個,搭配上其他的話就會有實心的和空心的,搭配到一起不太好看。而且還有一個問題,如果變量的分類超過6個,就會提示
Warning messages: 1: The shape palette can deal with a maximum of 6 discrete values because more than 6 becomes difficult to discriminate; you have 8. Consider specifying shapes manually if you must have them. 2: Removed 2 rows containing missing values (geom_point).
比如運行如下代碼你就會遇到上面的提示
df<-data.frame(x=LETTERS[1:8],
y=1:8)
library(ggplot2)
ggplot()+
geom_point(data=df,aes(x=x,y=y,shape=x,fill=x),size=5)
大體的意思就是超過6個形狀不太好區(qū)分,如果你非要用,就手動指定形狀,這個時候需要疊加scale_shape_manual()函數(shù)
今天在看ggtreeExtra論文中的代碼,發(fā)現(xiàn)其中提到了一個包是
ggstar,找到幫助文檔看了一下,主要的功能就是定義了新的函數(shù)geom_star()提供了更多的形狀選擇
幫助文檔的鏈接
https://cran.r-project.org/web/packages/ggstar/vignettes/ggstar.html
github主頁的鏈接
https://github.com/xiangpin/ggstar/
這里提供的形狀包括
p1 <- show_starshapes()
p1+theme_void()

一個簡單的用法
首先是安裝和加載
install.packages("ggstar")
library(ggstar)
library(ggplot2)
準備繪圖數(shù)據(jù)
df<-data.frame(x=LETTERS[1:8],
y=1:8)
畫圖
ggplot()+
geom_star(data=df,aes(x=x,y=y,starshape=x,fill=x),size=5)+
theme_bw()

如果想要手動挑選形狀,可以使用scale_starshape_manual()函數(shù)
ggplot()+
geom_star(data=df,aes(x=x,y=y,starshape=x,fill=x),
size=20)+
theme_bw()+
scale_starshape_manual(values = rep(16,8))

最后是一個有意思的簡單小例子
ggplot()+
geom_point(aes(x=1,y=1),shape=73,size=30,color="red")+
geom_star(aes(x=1.5,y=1),starshape=16,size=30,fill="red")+
geom_point(aes(x=2,y=1),shape=85,size=30,color="red")+
xlim(0.9,2.2)+
#theme_void()+
theme(aspect.ratio = 0.5)

歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學、基因組學、群體遺傳學文獻閱讀筆記;3、生物信息學入門學習資料及自己的學習筆記!