ฉันต้องการที่จะขยาย boxplots ของฉันด้วยข้อมูลเพิ่มเติม นี่คือตัวอย่างการทำงานของ ggplot2:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Basic box plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()
# Rotate the box plot
p + coord_flip()
ฉันต้องการเพิ่มข้อมูลเพิ่มเติมจากเฟรมข้อมูลแยกต่างหาก ตัวอย่างเช่น:
extra <- data.frame(dose=factor(c(0.5,1,2)), label=c("Label1", "Label2", "Label3"), n=c("n=42","n=52","n=35"))
> extra
dose label n
1 0.5 Label1 n=42
2 1 Label2 n=52
3 2 Label3 n=35
ฉันต้องการสร้างรูปต่อไปนี้โดยที่ข้อมูลของแต่ละโดส (ปัจจัย) อยู่นอกพล็อตและสอดคล้องกับแต่ละระดับของโดส (ฉันทำสิ่งนี้ใน PowerPoint เป็นตัวอย่าง):
คำแนะนำใด ๆ ที่ชื่นชมอย่างมาก!
ขอแสดงความนับถือ ลุค
แก้ไข: ขอบคุณมากสำหรับคำตอบของคุณ! สมบูรณ์แบบ. ผมอยากจะขอคำแนะนำขยายคำถามเบื้องต้นครับ
แล้วส่วนขยายนี้ที่ฉันใช้การเติมเพื่อแบ่งขนาดยาออกเป็นสองกลุ่มล่ะ?
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ToothGrowth$group <- head(rep(1:2, 100), dim(ToothGrowth)[1])
ToothGrowth$group <- factor(ToothGrowth$group)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=group)) +
geom_boxplot()
# Rotate the box plot
p + coord_flip()
extra <- data.frame(
dose=factor(rep(c(0.5,1,2), each=2)),
group=factor(rep(c(1:2), 3)),
label=c("Label1A", "Label1B", "Label2A", "Label2B", "Label3A", "Label3B"),
n=c("n=12","n=30","n=20", "n=32","n=15","n=20")
)
เป็นไปได้หรือไม่ที่จะจัดข้อมูลจากกรอบข้อมูลใหม่ (พิเศษ 6 แถว) ให้สอดคล้องกับแต่ละขนาดยา/กลุ่มรวมกัน
ไชโย ลุค