每次找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軸標籤角度