ggplot2画散点图
当前位置:以往代写 > R语言教程 >ggplot2画散点图
2019-06-13

ggplot2画散点图

ggplot2画散点图

# 加载R包
library(ggplot2)
library(ggthemes)
library(extrafont)
library(datasets)
library(dplyr)

# 数据加载
data("airquality")
str(airquality)
# 抽取第三季度的数据
aq.trim <- dplyr::filter(airquality, Month == 7 | Month == 8 | Month == 9)
aq.trim$Month <- factor(
aq.trim$Month,
labels = c("July", "August", "September")
)
dim(aq.trim)
summary(aq.trim)
# 第一步:散点图基础
p1 <- ggplot(data = aq.trim, aes(x = Day, y = Ozone)) +
geom_point()
p1

# 第二步:设置点的形状
p2 <- ggplot(data = aq.trim, aes(x = Day, y = Ozone)) +
geom_point(shape = 21)
p2

# 第三步:调整x轴的刻度
p3 <- ggplot(data = aq.trim, aes(x = Day, y = Ozone)) +
geom_point(shape = 21) +
scale_x_continuous(breaks = seq(1, 31, 5))
p3

# 第四步:添加轴标签和标题
p4 <- ggplot(data = aq.trim, aes(x = Day, y = Ozone)) +
geom_point(shape = 21) +
scale_x_continuous(breaks = seq(1, 31, 5)) +
ggtitle("Air Quality in New York by Day") +
labs(x = "Day of the month", y = "Ozone (ppb)") +
theme(plot.title = element_text(size = 30, face = "bold", hjust = 0.5))
p4

# 第五步:设置颜色模板
p5 <- ggplot(data = aq.trim, aes(x = Day, y = Ozone)) +
geom_point(shape = 21, colour = "mediumvioletred",
fill = "springgreen") +
scale_x_continuous(breaks = seq(1, 31, 5)) +
ggtitle("Air Quality in New York by Day") +
labs(x = "Day of the month", y = "Ozone (ppb)") +
theme(plot.title = element_text(size = 30, face = "bold", hjust = 0.5))
p5
“`
**参考资料:**
1 http://t-redactyl.io/blog/2016/02/creating-plots-in-r-using-ggplot2-part-5-scatterplots.html

您在阅读中,关于**散点图**有什么见解,请评论。
关于R语言更多内容,请阅读[**R系列文章**](http://shujuren.org/article/514.html "R系列文章")。

想加入数据人圈子,请加微信luqin360。

————

    关键字:

在线提交作业