美文网首页R学习与可视化
R绘图_ggplot2图形参数之点[4]

R绘图_ggplot2图形参数之点[4]

作者: 谢俊飞 | 来源:发表于2020-03-11 11:53 被阅读0次
火狐截图_2020-02-11T08-36-22.554Z.png

ggplot2 point shapes

ggplot2 :点形状、颜色、大小

本教程描述了如何更改使用R软件和ggplot2软件包生成的图形的点形状。
R中常用的不同点形状如下图所示:


图片.png

代码运行如下:

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")
Rplot01.png

相关文章

网友评论

    本文标题:R绘图_ggplot2图形参数之点[4]

    本文链接:https://www.haomeiwen.com/subject/nfsqjhtx.html