如何在 R 中使用 ggplot2 更改散点图的默认点类型?
要更改 ggplot2 几何对象的默认设置,我们需要使用 update_geom_defaults 函数。如果我们想更改散点图的点形状,可以使用以下语法:
update_geom_defaults("point",list(shape=”point_shape_number”))
点形状编号范围从 0 到 25。我们可以根据需要更改它。
考虑以下数据框:
示例
x<-rnorm(20) y<-rnorm(20,5,0.32) df<-data.frame(x,y) df
输出
x y 1 -0.005184157 5.338089 2 -1.044878945 4.662460 3 1.154191529 5.450373 4 -0.437975797 4.649176 5 -0.220793320 5.057522 6 -0.062619288 5.003642 7 -1.023676613 4.949002 8 0.902098608 4.632925 9 -0.259247125 4.792074 10 0.305164402 5.745798 11 -0.891792853 4.458326 12 -0.451765777 4.435208 13 -0.018278677 5.525500 14 0.917192389 4.740232 15 -1.366201294 5.363222 16 -0.491698757 5.305723 17 0.680064934 5.406010 18 -1.058529703 5.084462 19 1.862063812 4.851102 20 0.433153374 4.346126
加载 ggplot2 包并在 x 和 y 之间创建散点图:
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
输出
更新 ggplot2 包的散点图的点类型并创建图形:
示例
update_geom_defaults("point",list(shape=6)) ggplot(df,aes(x,y))+geom_point()
输出
广告