ggplot2 point shapes
ggplot2 :點(diǎn)形狀涵紊、顏色傍妒、大小
本教程描述了如何更改使用R軟件和ggplot2軟件包生成的圖形的點(diǎn)形狀。
R中常用的不同點(diǎn)形狀如下圖所示:
代碼運(yùn)行如下:
rm(list = ls())
df <- mtcars[,c("mpg","cyl","wt")]
df$cyl <- as.factor(df$cyl)
head(df)
mpg cyl wt
Mazda RX4 21.0 6 2.620
Mazda RX4 Wag 21.0 6 2.875
Datsun 710 22.8 4 2.320
Hornet 4 Drive 21.4 6 3.215
Hornet Sportabout 18.7 8 3.440
Valiant 18.1 6 3.460
# Basic scatter plots
library(ggplot2)
ggplot(df, aes(x=wt, y=mpg)) +
geom_point()
# change the point shape
ggplot(df, aes(x=wt, y=mpg))+
geom_point(shape = 18)
# change the shape, color, fill, size
ggplot(df, aes(x=wt, y=mpg))+
geom_point(shape = 23, fill = "blue", color = "darkred", size = 3)
#Scatter plots with multiple groups
# Scatter plot with multiple groups shape depends on cyl
ggplot(df, aes(x=wt, y=mpg, group=cyl)) +
geom_point(aes(shape=cyl))
# Change point shapes and colors
ggplot(df, aes(x=wt, y=mpg, group=cyl)) +
geom_point(aes(shape=cyl, color=cyl))
# change point shapes, colors and sizes
ggplot(df, aes(x=wt, y=mpg, group=cyl)) +
geom_point(aes(shape=cyl, color=cyl, size=cyl))
# Change point shapes, colors and sizes manually :
# Change colors and shapes manually
ggplot(df, aes(x=wt, y=mpg, group=cyl)) +
geom_point(aes(shape=cyl, color=cyl), size=2)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
# Change the point size manually
ggplot(df, aes(x=wt, y=mpg, group=cyl)) +
geom_point(aes(shape=cyl, color=cyl, size=cyl))+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
scale_size_manual(values=c(2,3,4))+
theme(legend.position="top")