rm(list = ls())
setwd("C:\\Users\\Administrator\\Desktop\\machine learning\\分组散点图")
data("mpg")
library(ggplot2)
data <- read.table("data.txt", sep = "\t", header = TRUE, row.names = 1)
# 自定义刻度和标签
custom_x_breaks <- c(28, 42, 56, 70) # 根据数据自定义刻度位置
custom_x_labels <- c("DAS28", "DAS42", "DAS56", "DAS70") # 自定义刻度标签
# 自定义颜色
custom_colors <- c("B73" = "#8FC9E2", "Mo17" = "#ECC97F") # 定义颜色
p1 <- ggplot(data = data, aes(x = time, y = abundance)) +
theme_minimal(base_size = 12) +
geom_point(aes(colour = gene, shape = gene), size = 4) + # 点的大小
geom_smooth(aes(colour = gene), method = "glm", size = 1.5) + # 曲线的大小
labs(x = "Growth Stage", y = "Relative Abundance", title = "Sphingobium") +
theme(plot.title = element_text(hjust = 0.5, size = 20), # 自定义标题大小
legend.position = c(0.85, 0.8),
legend.title = element_blank(), # 去掉图例标题
legend.key.size = unit(1, "cm"), # 自定义图例大小
legend.text = element_text(size = 12), # 自定义图例标签大小
panel.grid.major = element_blank(), # 去掉主要网格线
panel.grid.minor = element_blank(), # 去掉次要网格线
axis.line = element_line(size = 1.2, colour = "black"), # 自定义坐标轴线粗细
axis.text.x = element_text(size = 18), # 自定义横坐标刻度标签大小
axis.text.y = element_text(size = 18), # 自定义纵坐标刻度标签大小
axis.title.x = element_text(size = 18), # 自定义横坐标标题大小
axis.title.y = element_text(size = 18), # 自定义纵坐标标题大小
axis.ticks = element_line(size = 1.2)) + # 自定义刻度线粗细
scale_x_continuous(breaks = custom_x_breaks, labels = custom_x_labels) + # 使用自定义的横坐标刻度位置和标签
scale_y_continuous() + # 使用默认的纵坐标刻度标签
scale_color_manual(values = custom_colors) # 自定义点和线的颜色
p1
# 保存图形
ggsave('p.png', width = 8, height = 8, bg = 'white', dpi = 1200)
标签:mpg,Administrator,setwd,散点图,分组,data From: https://www.cnblogs.com/wzbzk/p/18408311