R语言学习case7:ggplot基础画图(核密度图)

step1: 导入ggplot2库文件

bash 复制代码
library(ggplot2)

step2:带入自带的iris数据集

bash 复制代码
iris <- datasets::iris

step3:查看数据信息

bash 复制代码
dim(iris)

维度为 [150,5]

bash 复制代码
head(iris)

查看数据前6行的信息

step4:画图展示

bash 复制代码
plot2 <- ggplot(iris,aes(Sepal.Width))+
  theme_minimal(base_size = 12)+
  geom_density(aes(colour = Species,fill = Species),alpha = 0.5)+
  labs(title = "Density 密度曲线")+
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = c(0.8,0.8))

plot2
  • ggplot(iris, aes(Sepal.Width)): 这一行代码指定了要绘制的密度曲线图的数据集为iris,并指定了Sepal.Width作为横坐标。

  • theme_minimal(base_size = 12): 这一行代码应用了一个简约的主题(theme_minimal()),并设置了基础字体大小为12。

  • geom_density(aes(colour = Species, fill = Species), alpha = 0.5): 这一行代码添加了密度曲线,并根据Species列的值对曲线进行着色。aes(colour = Species, fill = Species)告诉ggplot函数要根据Species列的值对曲线进行着色。alpha = 0.5设置了曲线的透明度为0.5,使得重叠部分能够更容易地辨认。

  • labs(title = "Density 密度曲线"): 这一行代码为图表添加了一个标题,标题为"Density 密度曲线"。

  • theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.8,0.8)): 这一行代码设置了图表的标题居中显示,并将图例放置在图表的右上角位置。

相关推荐
sonnet-102918 小时前
函数式接口和方法引用
java·开发语言·笔记
Bat U18 小时前
JavaEE|多线程(二)
java·开发语言
今天你TLE了吗18 小时前
LLM到Agent&RAG——AI概念概述 第二章:提示词
人工智能·笔记·后端·学习
烤麻辣烫18 小时前
JS基础
开发语言·前端·javascript·学习
red_redemption18 小时前
自由学习记录(168)
学习·已经运行中世界-模与约束·闭环
froginwe1119 小时前
C++ 文件和流
开发语言
Dxy123931021619 小时前
Python在图片上画矩形:从简单边框到复杂标注的全攻略
开发语言·python
独自破碎E19 小时前
面试官:你有用过Java的流式吗?比如说一个列表.stream这种,然后以流式去处理数据。
java·开发语言
꯭爿꯭巎꯭19 小时前
python下载手机版(python3手机版(免费))
开发语言·python·智能手机
2601_9549711319 小时前
2026数学专业,靠什么技能能发挥数理优势转数据岗?
学习