1、练习将25个点的符号绘制出来,然后用rainbow()返回25个颜色,后5个符号形状的背景颜色用蓝色填充,图的标题为"符号图",x轴标题为符号索引,y轴标题为符号形状。

2、根据员工的销售业绩画饼状图,添加图例(右上角),并将图例的形状改为pch=c(15:17),其中员工的销售业绩和姓名如下表:

姓名

业绩

小明

100

小红

200

小亮

300

效果图示例

3、随机生成1000个数据,均值为10 标准差为3。练习绘制直方图。

要求:(1)每个直方图用不同的颜色展示;(2)每个条柱上显示具体的数值;(3)绘制频数和频率直方图,并在频率直方图上添加曲线。

4、使用R语言内置的Titanic数据集,分析男性乘客和女性乘客的获救情况。要求:(1)绘制性别、获救情况的柱状图;(2)条柱上显示男性和女性的获救人数与遇难人数;(3)图的标题为性别与获救情况;(4)获救用绿色填充,遇难用红色填充。(5)添加图例,图例标题为获救情况。参考下图:

5、随机生成1000个数据,均值为10 标准差为5。练习绘制箱型图。要求:(1)图形主标题为箱形图;(2)使用text( )函数添加最大值、最小值、上下四分位数以及中位数;(3)打印出离群点。

6、练习使用R语言内置数据集stackloss,画出stackloss数据集各变量的直方图、箱型图、散点图,一次性画4个图。

 

1、

代码:

# 安装 ggplot2 和 viridis 包

#install.packages("ggplot2")

#install.packages("viridis")

# 导入所需的包

library(ggplot2)

library(viridis)

# 创建数据框

data <- data.frame(

  index = 1:25,

  shape = rep(c("circle", "square", "triangle", "diamond", "star"), 5)

)

# 绘制图形

p <- ggplot(data, aes(x = factor(index), y = shape, fill = shape)) +

  geom_point(shape = 21, size = 5) +

  scale_fill_viridis(discrete = TRUE) +

  theme_minimal() +

  labs(title = "符号图", x = "符号索引", y = "符号形状")

# 显示图形

print(p)

截图:

2、

代码:

# 员工销售业绩和姓名

sales <- c(100, 200, 300)

names <- c("小明", "小红", "小亮")

# 画饼状图

pie(sales, labels=names)

# 添加图例并更改图例形状

legend("topright", names, pch=15:17, title="员工销售业绩")

截图:

3、

代码:

# 生成符合要求的随机数据

mean_value <- 10

std_dev <- 3

data <- rnorm(1000, mean=mean_value, sd=std_dev)

# 绘制频数直方图

hist(data, breaks=30, col="skyblue", xlab="Value", ylab="Frequency", main="Histogram of Data")

text(data, 10, labels=round(data, 1), cex=0.6, srt=90, adj=c(0.5,0.5))

# 绘制频率直方图并添加曲线

par(mfrow=c(1, 2))

hist(data, breaks=30, freq=FALSE, col="lightgreen", xlab="Value", ylab="Frequency", main="Frequency Histogram with Density Curve")

lines(density(data), col="red")

截图:

4、

代码:

# 导入所需的包

library(ggplot2)

# 加载Titanic数据集

data("Titanic")

# 转换数据集为数据框格式

df <- as.data.frame(Titanic)

# 重新构造数据框

df <- subset(df, df$Survived == "Yes" | df$Survived == "No")

df <- subset(df, df$Class == "1st" | df$Class == "2nd" | df$Class == "3rd")

# 计算男性和女性的获救情况

survived_counts <- aggregate(Survived ~ Sex, data = df, FUN = function(x) sum(x == "Yes"))

died_counts <- aggregate(Survived ~ Sex, data = df, FUN = function(x) sum(x == "No"))

# 绘制柱状图

barplot_heights <- c(survived_counts$Survived, died_counts$Survived)

barplot_labels <- c(paste("幸存:", survived_counts$Survived), paste("遇难:", died_counts$Survived))

# 创建数据框

bar_data <- data.frame(

  Sex = rep(c("Female", "Male"), each = 2),

  Survival = rep(c("Survived", "Died"), times = 2),

  Counts = barplot_heights,

  Labels = barplot_labels

)

# 绘制柱状图

p <- ggplot(bar_data, aes(x = Sex, y = Counts, fill = Survival)) +

  geom_bar(stat = "identity", position = "dodge") +

  geom_text(aes(label = Labels), position = position_dodge(width = 0.9), vjust = -0.5, size = 3) +

  scale_fill_manual(values = c("green", "red"), name = "获救情况") +

  labs(title = "性别与获救情况", x = "性别", y = "人数") +

  theme_minimal()

# 显示图形

print(p)

截图:

5、

代码:

# 生成随机数据

data <- rnorm(1000, mean=10, sd=5)

# 绘制箱形图

boxplot(data, main="箱形图")

text(1, max(data), paste("最大值:", round(max(data), 2)))

text(1, quantile(data, 0.75), paste("上四分位数:", round(quantile(data, 0.75), 2)))

text(1, median(data), paste("中位数:", round(median(data), 2)))

text(1, quantile(data, 0.25), paste("下四分位数:", round(quantile(data, 0.25), 2)))

text(1, min(data), paste("最小值:", round(min(data), 2)))

截图:

6、

代码:

# 导入所需的包

library(ggplot2)

# 加载stackloss数据集

data("stackloss")

# 创建一个数据框来存储stackloss数据

df <- data.frame(stackloss)

# 绘制直方图

hist_plot <- ggplot(df, aes(x = stack.loss)) +

  geom_histogram(fill = "lightblue", color = "black", bins = 10) +

  labs(title = "stack.loss的直方图", x = "stack.loss", y = "频数")

# 绘制箱型图

boxplot_plot <- ggplot(df, aes(x = factor(1), y = stack.loss)) +

  geom_boxplot(fill = "lightgreen", color = "black") +

  labs(title = "stack.loss的箱型图", x = "", y = "stack.loss") +

  theme(axis.text.x = element_blank())

# 绘制散点图

scatter_plot <- ggplot(df, aes(x = Air.Flow, y = stack.loss)) +

  geom_point(color = "darkorange") +

  labs(title = "Air.Flow与stack.loss的散点图", x = "Air.Flow", y = "stack.loss")

# 绘制柱状图

barplot_plot <- ggplot(df, aes(x = factor(1), y = stack.loss, fill = row.names(df))) +

  geom_bar(stat = "identity", width = 0.5) +

  coord_polar(theta = "y") +

  labs(title = "stackloss数据集中各变量的柱状图", x = "", y = "stack.loss") +

  theme(axis.text.x = element_blank())

# 打印四个图形

multiplot(hist_plot, boxplot_plot, scatter_plot, barplot_plot, cols=2)

截图:

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部