每次找R绘图美化都很麻烦,索性自己写个笔记慢慢补充绘图美化的指令
### 生成数据 ### # 设置种子以获得可重复的结果 set.seed(222) # 生成字符序列 "AAAABBBB" char_sequence <- c("A", "A", "A", "A", "B", "B", "B", "B") # 生成相应数量的随机数,这里使用正态分布 random_numbers <- runif(8, min = 0, max = 100) # 创建数据框 data_frame <- data.frame(Char = char_sequence, Value = random_numbers) # 计算每个条件下的平均值和标准差 summary_data <- data_frame %>% group_by(Char) %>% summarise( Mean = mean(Value), StdErr = sd(Value, na.rm = TRUE) / sqrt(n()) ) #### 绘图 #### ggplot() + geom_col(data = summary_data, aes(x = Char, y = Mean), fill = "lightblue") + # 绘制柱状图 geom_errorbar(data = summary_data, # 使用summary_data数据框中的均值和标准差 aes(x = Char, ymin = Mean - StdErr, ymax = Mean + StdErr), width = 0.2, position = position_dodge(0.5)) + geom_point(data = summary_data, # 在均值位置添加点 aes(x = Char, y = Mean), position = position_dodge(0.5), color = "red") + labs(x = "Character", y = "Value", title = "Bar Chart with Error Bars") + # 设置变量名称和title theme_minimal()+ #使用简洁主题 theme(axis.line = element_line(color = "black", size = 0.5), # 显示轴的线条 axis.text = element_text(size = 12), # 轴标签文字大小 axis.title = element_text(size = 14), # 轴标题文字大小 panel.grid = element_blank()) + # 去掉网格线 scale_y_continuous(limits =c(0, 90) ,expand = c(0,0)) # 贴近x轴 # + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # 旋转x轴标签角度
标签:06,text,errorbar,element,柱状图,绘图,summary,data,Mean From: https://www.cnblogs.com/MJnotes/p/18372392