用ggplot2顯示點和水平線(段)的意思 (Show means with points and horizontal lines (segments) with ggplot2)


問題描述

用ggplot2顯示點和水平線(段)的意思 (Show means with points and horizontal lines (segments) with ggplot2)

我想繪製三組數據。具體來說,我想顯示單個數據點,包括三組的平均值。這是我到目前為止所擁有的:

library(ggplot2)

df <‑ data.frame(group=rep(c("A", "B", "C"), each=10), value=rnorm(30))

ggplot(data=df, mapping=aes(x=group, y=value)) +
    geom_point() +
    stat_summary(fun="mean", geom="point", color="red", size=5) +
    stat_summary(fun="mean", geom="segment", mapping=aes(xend=..x.. + 0.25, yend=..y..))

這會產生下圖:

這就是我所擁有的

但是,我希望水平線段從每組平均值的左側開始,而不是從中心開始。我嘗試指定 mapping=aes(x=..x.. ‑ 0.25, xend=..x.. + 0.25,yend=..y..),但這只會給我一個錯誤:

Error: stat_summary requires the following missing aesthetics: x

我不明白為什麼我不能使用 ..x..<


參考解法

方法 1:

Try this (Maybe not the most elegant solution):

library(ggplot2)

df <‑ data.frame(group=rep(c("A", "B", "C"), each=10), value=rnorm(30))

ggplot(data=df, mapping=aes(x=group, y=value)) +
  geom_point() +
  stat_summary(fun="mean", geom="point", color="red", size=5) +
  stat_summary(fun="mean", geom="segment", mapping=aes(xend=..x.. ‑ 0.25, yend=..y..))+
  stat_summary(fun="mean", geom="segment", mapping=aes(xend=..x.. + 0.25, yend=..y..))

e chec

(by cbrnrDuck)

參考文件

  1. Show means with points and horizontal lines (segments) with ggplot2 (CC BY‑SA 2.5/3.0/4.0)

#R #ggplot2






相關問題

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

Tạo các thùng của mỗi hàng trong bảng và vẽ hình thanh ngăn xếp trong R (Make bins of each table row and draw stack bar figure in R)

Reading not quite correct .csv file in R (Reading not quite correct .csv file in R)

包'treemap'中的線條粗細 (Thickness of lines in Package ‘treemap’)

是否需要帶有 awk 的預處理文件,或者可以直接在 R 中完成? (Is preprocessing file with awk needed or it can be done directly in R?)

rpivotTable 選擇元素下拉菜單 (rpivotTable select elements drop down menu)

優化性能 - Shiny 中的大文件輸入 (Optimizing Performance - Large File Input in Shiny)

數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)

如何記錄全年的值? (How to note the values across year?)

R中的線性搜索 (Linear search in R)

在 dplyr/purrr 工作流程中動態連接多個數據集 (Dynamically join multiple datasets in a dplyr/purrr workflow)

如何將行值更改為列名 (R) (How change Row values to Column names (R))







留言討論