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)): 这一行代码设置了图表的标题居中显示,并将图例放置在图表的右上角位置。

相关推荐
靡不有初1112 分钟前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
十八朵郁金香13 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
阿尔法波17 分钟前
python与pycharm如何设置文件夹为源代码根目录
开发语言·python·pycharm
xing251626 分钟前
pytest下allure
开发语言·python·pytest
眸笑丶30 分钟前
使用 Python 调用 Ollama API 并调用 deepseek-r1:8b 模型
开发语言·python
enyp801 小时前
Qt QStackedWidget 总结
开发语言·qt
gu201 小时前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
lly2024061 小时前
SQLite 删除表
开发语言
wjs20241 小时前
HTML 字符实体
开发语言
二十雨辰1 小时前
[Java基础]网络编程
java·开发语言