在双因素箱图中添加观察计数

时间:2018-03-06 13:08:00

标签: r ggplot2 boxplot

我已经在Add number of observations per group in ggplot2 boxplot

中看到了另一个类似的问题

但是这个解决方案并没有在我的情节中起作用,因为我有两个因素的箱图。 这是我试过的: 这是我的数据:

> dput(Simp_Shan_Baseline_Grp[1:5,1:20])
structure(list(Datasets = structure(1:5, .Label = c("30001.10001.Visit.1.Baseline.Day.0.1h..", 
"30001.10002.Visit.1.Baseline.Day.0.1h..", "30001.10003.Visit.1.Baseline.Day.0.1h..", 
"30001.10004.Visit.1.Baseline.Day.0.1h..", "30001.10005.Visit.1.Baseline.Day.0.1h..", 
"30001.10006.Visit.1.Baseline.Day.0.1h..", "30004.10001.Visit.1.Baseline.Day.0.1h..", 
"30004.10002.Visit.1.Baseline.Day.0.1h..", "30004.10003.Visit.1.Baseline.Day.0.1h..", 
"30004.10004.Visit.1.Baseline.Day.0.1h..", "30004.10006.Visit.1.Baseline.Day.0.1h..", 
"30004.10007.Visit.1.Baseline.Day.0.1h..", "30004.10008.Visit.1.Baseline.Day.0.1h..", 
"30005.10001.Visit.1.Baseline.Day.0.1h..", "30005.10002.Visit.1.Baseline", 
"30005.10003.Visit.1.Baseline.Day.0.1h..", "30005.10004.Visit.1.Baseline.Day.0.1h..", 
"30005.10005.Visit.1.Baseline.Day.0.1h..", "30005.10006.Visit.1.Baseline.Day.0.1h..", 
"30005.10007.Visit.1.Baseline.Day.0.1h..", "30005.10008.Visit.1.Baseline.Day.0.1h..", 
"30005.10009.Visit.1.Baseline.Day.0.1h..", "30006.10001.Visit.1.Baseline", 
"30006.10002.Visit.1.Baseline", "30007.10001.Visit.1.Baseline.Day.0.1h..", 
"30007.10002.Visit.1.Baseline.Day.0.1h..", "30008.10001.Visit.1.Baseline.Day.0.1h..", 
"30008.10002.Visit.1.Baseline.Day.0.1h..", "30008.10003.Visit.1.Baseline", 
"30008.10004.Visit.1.Baseline", "30008.10005.Visit.1.Baseline.Day.0.1h..", 
"30008.10006.Visit.1.Baseline.Day.0.1h..", "30008.10007.Visit.1.Baseline.Day.0.1h..", 
"30008.10008.Visit.1.Baseline.Day.0.1h..", "30009.10001.Visit.1.Baseline.Day.0.1h..", 
"30009.10002.Visit.1.Baseline.Day.0.1h..", "30009.10003.Visit.1.Baseline.Day.0.1h..", 
"30009.10004.Visit.1.Baseline.Day.0.1h..", "30009.10005.Visit.1.Baseline.Day.0.1h..", 
"30009.10007.Visit.1.Baseline.Day.0.1h..", "30010.10001.Visit.1.Baseline.Day.0.1h..", 
"30010.10002.Visit.1.Baseline.Day.0.1h..", "32005.10001.Visit.1.Baseline.Day.0.1h..", 
"33001.10001.Visit.1.Baseline.Day.0.1h..", "33001.10002.Visit.1.Baseline.Day.0.1h..", 
"33001.10003.Visit.1.Baseline.Day.0.1h..", "33001.10004.Visit.1.Baseline.Day.0.1h..", 
"33001.10005.Visit.1.Baseline.Day.0.1h..", "33001.10006.Visit.1.Baseline.Day.0.1h..", 
"33002.10001.Visit.1.Baseline.Day.0.1h..", "33002.10002.Visit.1.Baseline.Day.0.1h..", 
"33002.10003.Visit.1.Baseline.Day.0.1h..", "33002.10004.Visit.1.Baseline.Day.0.1h..", 
"33004.10001.Visit.1.Baseline.Day.0.1h..", "33005.10001.Visit.1.Baseline", 
"33005.10002.Visit.1.Baseline.Day.0.1h..", "33005.10004.Visit.1.Baseline.Day.0.1h..", 
"33006.10001.Visit.1.Baseline.Day.0.1h..", "33006.10002.Visit.1.Baseline.Day.0.1h..", 
"33006.10003.Visit.1.Baseline.Day.0.1h..", "33006.10005.Unscheduled.Visit.F.Day.0.8h..", 
"33006.10006.Visit.1.Baseline.Day.0.1h..", "33009.10001.Visit.1.Baseline.Day.0.1h..", 
"33009.10002.Visit.1.Baseline.Day.0.1h..", "33009.10003.Visit.1.Baseline.Day.0.1h..", 
"33009.10004.Visit.1.Baseline.Day.0.1h..", "33009.10005.Visit.1.Baseline.Day.0.1h..", 
"34003.10001.Visit.1.Baseline.Day.0.1h..", "34003.10002.Visit.1.Baseline.Day.0.1h..", 
"34003.10003.Visit.1.Baseline.Day.0.1h..", "34003.10004.Visit.1.Baseline.Day.0.1h..", 
"34003.10005.Visit.1.Baseline.Day.0.1h..", "34003.10006.Visit.1.Baseline.Day.0.1h..", 
"34003.10007.Visit.1.Baseline.Day.0.1h..", "34003.10008.Visit.1.Baseline.Day.0.1h..", 
"34004.10001.Visit.1.Baseline.Day.0.1h..", "34004.10002.Visit.1.Baseline.Day.0.1h..", 
"34004.10003.Visit.1.Baseline.Day.0.1h..", "34004.10004.Visit.1.Baseline.Day.0.1h..", 
"34004.10005.Visit.1.Baseline", "35104.10001.Visit.1.Baseline.Day.0.1h..", 
"35106.10001.Unscheduled.Visit.R.Day.0.7h..", "35107.10001.Visit.1.Baseline.Day.0.1h..", 
"35801.10001.Visit.1.Baseline", "35802.10002.Visit.1.Baseline.Day.0.1h..", 
"35802.10003.Visit.1.Baseline.Day.0.1h..", "36001.10001.Visit.1.Baseline.Day.0.1h..", 
"36001.10002.Visit.1.Baseline.Day.0.1h..", "36004.10003.Visit.1.Baseline.Day.0.1h..", 
"36004.10004.Visit.1.Baseline.Day.0.1h..", "36004.10005.Visit.1.Baseline.Day.0.1h..", 
"36004.10006.Visit.1.Baseline.Day.0.1h..", "36005.10001.Visit.1.Baseline.Day.0.1h..", 
"36007.10001.Visit.1.Baseline.Day.0.1h..", "36008.10001.Visit.1.Baseline.Day.0.1h..", 
"36008.10005.Visit.1.Baseline.Day.0.1h..", "36008.10007.Visit.1.Baseline.Day.0.1h..", 
"36008.10012.Visit.1.Baseline.Day.0.1h..", "36008.10017.Visit.1.Baseline.Day.0.1h..", 
"36008.10018.Visit.1.Baseline.Day.0.1h..", "36008.10020.Visit.1.Baseline.Day.0.1h..", 
"36008.10021.Visit.1.Baseline.Day.0.1h..", "36008.10022.Visit.1.Baseline.Day.0.1h..", 
"36009.10001.Visit.1.Baseline.Day.0.1h..", "36009.10002.Visit.1.Baseline.Day.0.1h..", 
"36009.10003.Visit.1.Baseline.Day.0.1h..", "36009.10004.Visit.1.Baseline.Day.0.1h..", 
"36009.10005.Visit.1.Baseline.Day.0.1h..", "36009.10006.Visit.1.Baseline.Day.0.1h..", 
"36010.10001.Visit.1.Baseline.Day.0.1h..", "36010.10002.Visit.1.Baseline.Day.0.1h..", 
"36010.10003.Visit.1.Baseline.Day.0.1h..", "38501.10001.Visit.1.Baseline.Day.0.1h..", 
"38501.10002.Visit.1.Baseline.Day.0.1h..", "38501.10003.Visit.1.Baseline.Day.0.1h..", 
"38505.10001.Visit.1.Baseline.Day.0.1h..", "38505.10002.Visit.1.Baseline.Day.0.1h..", 
"38506.10001.Visit.1.Baseline.Day.0.1h..", "38506.10002.Visit.1.Baseline.Day.0.1h..", 
"38506.10003.Visit.1.Baseline.Day.0.1h..", "38506.10004.Visit.1.Baseline.Day.0.1h..", 
"38601.10001.Visit.1.Baseline.Day.0.1h..", "38601.10003.Visit.1.Baseline.Day.0.1h..", 
"38601.10004.Visit.1.Baseline.Day.0.1h..", "38601.10006.Visit.1.Baseline.Day.0.1h..", 
"38601.10007.Visit.1.Baseline", "38602.10001.Visit.1.Baseline.Day.0.1h..", 
"38602.10002.Visit.1.Baseline.Day.0.1h..", "38603.10002.Visit.1.Baseline.Day.0.1h..", 
"38603.10003.Visit.1.Baseline.Day.0.1h..", "39001.10001.Visit.1.Baseline", 
"39001.10002.Visit.1.Baseline", "39001.10003.Visit.1.Baseline", 
"39001.10004.Visit.1.Baseline.Day.0.1h..", "39001.10005.Visit.1.Baseline", 
"39001.10006.Visit.1.Baseline.Day.0.1h..", "39001.10007.Visit.1.Baseline", 
"39001.10008.Visit.1.Baseline", "39001.10009.Visit.1.Baseline", 
"39001.10010.Visit.1.Baseline.Day.0.1h..", "39002.10001.Visit.1.Baseline.Day.0.1h..", 
"39003.10001.Visit.1.Baseline", "39004.10001.Visit.1.Baseline.Day.0.1h..", 
"39004.10002.Visit.1.Baseline.Day.0.1h..", "39004.10003.Visit.1.Baseline.Day.0.1h..", 
"39005.10001.Visit.1.Baseline.Day.0.1h..", "39005.10002.Visit.1.Baseline.Day.0.1h..", 
"39006.10001.Visit.1.Baseline.Day.0.1h..", "39006.10002.Visit.1.Baseline.Day.0.1h..", 
"39006.10003.Visit.1.Baseline.Day.0.1h..", "39006.10004.Visit.1.Baseline.Day.0.1h..", 
"39006.10005.Visit.1.Baseline.Day.0.1h..", "39006.10006.Visit.1.Baseline.Day.0.1h..", 
"39007.10001.Visit.1.Baseline.Day.0.1h..", "39007.10002.Visit.1.Baseline.Day.0.1h..", 
"39007.10003.Visit.1.Baseline.Day.0.1h..", "39010.10001.Visit.1.Baseline.Day.0.1h..", 
"40001.10002.Visit.1.Baseline.Day.0.1h..", "40001.10003.Visit.1.Baseline.Day.0.1h..", 
"40001.10005.Visit.1.Baseline", "40001.10006.Visit.1.Baseline", 
"40001.10007.Visit.1.Baseline", "40001.10011.Visit.1.Baseline.Day.0.1h..", 
"40001.10013.Visit.1.Baseline", "40001.10014.Visit.1.Baseline", 
"40001.10015.Visit.1.Baseline.Day.0.1h..", "40001.10016.Visit.1.Baseline.Day.0.1h..", 
"40001.10017.Visit.1.Baseline.Day.0.1h..", "40001.10019.Visit.1.Baseline.Day.0.1h..", 
"40002.10001.Visit.1.Baseline.Day.0.1h..", "40002.10002.Visit.1.Baseline.Day.0.1h..", 
"40002.10003.Visit.1.Baseline", "40002.10004.Visit.1.Baseline.Day.0.1h..", 
"40002.10005.Visit.1.Baseline", "40002.10006.Visit.1.Baseline", 
"40002.10007.Visit.1.Baseline.Day.0.1h..", "40002.10008.Visit.1.Baseline", 
"40002.10009.Visit.1.Baseline.Day.0.1h..", "40002.10010.Visit.1.Baseline", 
"40002.10012.Visit.1.Baseline", "40002.10013.Visit.1.Baseline.Day.0.1h..", 
"40002.10014.Visit.1.Baseline", "40002.10015.Visit.1.Baseline.Day.0.1h..", 
"40002.10016.Visit.1.Baseline", "40002.10017.Visit.1.Baseline", 
"40003.10001.Visit.1.Baseline.Day.0.1h..", "40003.10002.Visit.1.Baseline.Day.0.1h..", 
"40003.10003.Visit.1.Baseline.Day.0.1h..", "40003.10004.Visit.1.Baseline.Day.0.1h..", 
"40003.10005.Visit.1.Baseline.Day.0.1h..", "40003.10006.Visit.1.Baseline", 
"40003.10007.Visit.1.Baseline.Day.0.1h..", "40003.10008.Visit.1.Baseline.Day.0.1h..", 
"40003.10009.Visit.1.Baseline", "40003.10010.Visit.1.Baseline.Day.0.1h..", 
"40003.10011.Visit.1.Baseline.Day.0.1h..", "40003.10012.Visit.1.Baseline.Day.0.1h..", 
"40003.10013.Visit.1.Baseline", "40003.10014.Visit.1.Baseline.Day.0.1h..", 
"40003.10015.Visit.1.Baseline.Day.0.1h..", "40003.10016.Visit.1.Baseline.Day.0.1h..", 
"41001.10001.Visit.1.Baseline.Day.0.1h..", "41001.10002.Visit.1.Baseline.Day.0.1h..", 
"41001.10003.Visit.1.Baseline.Day.0.1h..", "41002.10001.Visit.1.Baseline.Day.0.1h..", 
"41004.10001.Visit.1.Baseline.Day.0.1h..", "42001.10001.Visit.1.Baseline.Day.0.1h..", 
"42001.10002.Visit.1.Baseline.Day.0.1h..", "42001.10004.Visit.1.Baseline.Day.0.1h..", 
"42001.10005.Visit.1.Baseline.Day.0.1h..", "42001.10006.Visit.1.Baseline.Day.0.1h..", 
"42001.10007.Visit.1.Baseline.Day.0.1h..", "42001.10008.Visit.1.Baseline.Day.0.1h..", 
"42002.10001.Visit.1.Baseline.Day.0.1h..", "42002.10002.Visit.1.Baseline.Day.0.1h..", 
"42002.10003.Visit.1.Baseline", "42002.10004.Visit.1.Baseline.Day.0.1h..", 
"42003.10001.Visit.1.Baseline.Day.0.1h..", "42003.10002.Visit.1.Baseline.Day.0.1h..", 
"42003.10004.Visit.1.Baseline.Day.0.1h..", "42004.10001.Visit.1.Baseline", 
"42004.10002.Visit.1.Baseline", "42004.10003.Visit.1.Baseline.Day.0.1h..", 
"42004.10004.Visit.1.Baseline.Day.0.1h..", "42005.10001.Visit.1.Baseline.Day.0.1h..", 
"42005.10002.Visit.1.Baseline.Day.0.1h..", "42005.10003.Unscheduled.Visit.R.Day.0.7h..", 
"42005.10004.Visit.1.Baseline.Day.0.1h..", "42005.10005.Visit.1.Baseline.Day.0.1h..", 
"42005.10006.Visit.1.Baseline", "42005.10007.Visit.1.Baseline.Day.0.1h..", 
"42005.10008.Visit.1.Baseline.Day.0.1h..", "43001.10001.Visit.1.Baseline.Day.0.1h..", 
"43002.10001.Visit.1.Baseline.Day.0.1h..", "43002.10002.Visit.1.Baseline.Day.0.1h..", 
"43003.10001.Visit.1.Baseline", "44003.10001.Visit.1.Baseline", 
"44005.10002.Visit.1.Baseline.Day.0.1h..", "44005.10003.Visit.1.Baseline.Day.0.1h..", 
"44008.10006.Visit.1.Baseline.Day.0.1h..", "44008.10009.Visit.1.Baseline.Day.0.1h..", 
"44008.10011.Visit.1.Baseline", "44008.10013.Visit.1.Baseline", 
"45004.10001.Visit.1.Baseline.Day.0.1h..", "45004.10003.Visit.1.Baseline", 
"46001.10001.Visit.1.Baseline.Day.0.1h..", "46001.10002.Visit.1.Baseline.Day.0.1h..", 
"46001.10003.Visit.1.Baseline.Day.0.1h..", "46001.10004.Visit.1.Baseline.Day.0.1h..", 
"46001.10005.Visit.1.Baseline.Day.0.1h..", "46002.10001.Visit.1.Baseline.Day.0.1h..", 
"46002.10003.Visit.1.Baseline.Day.0.1h..", "46004.10001.Visit.1.Baseline.Day.0.1h..", 
"46005.10001.Visit.1.Baseline.Day.0.1h..", "46005.10003.Visit.1.Baseline.Day.0.1h..", 
"48002.10001.Visit.1.Baseline.Day.0.1h..", "48002.10002.Visit.1.Baseline.Day.0.1h..", 
"48003.10001.Visit.1.Baseline.Day.0.1h..", "48003.10002.Visit.1.Baseline.Day.0.1h..", 
"48003.10003.Visit.1.Baseline.Day.0.1h..", "48003.10004.Visit.1.Baseline.Day.0.1h..", 
"48004.10001.Visit.1.Baseline.Day.0.1h..", "48004.10003.Visit.1.Baseline.Day.0.1h..", 
"48004.10005.Visit.1.Baseline", "48004.10006.Visit.1.Baseline.Day.0.1h..", 
"48004.10007.Visit.1.Baseline.Day.0.1h..", "48004.10008.Visit.1.Baseline.Day.0.1h..", 
"48004.10009.Visit.1.Baseline.Day.0.1h..", "48004.10011.Visit.1.Baseline.Day.0.1h..", 
"48004.10012.Visit.1.Baseline.Day.0.1h..", "48004.10014.Visit.1.Baseline.Day.0.1h..", 
"48004.10017.Visit.1.Baseline.Day.0.1h..", "48004.10018.Visit.1.Baseline.Day.0.1h..", 
"48004.10019.Visit.1.Baseline.Day.0.1h..", "48004.10020.Visit.1.Baseline.Day.0.1h..", 
"48004.10021.Visit.1.Baseline.Day.0.1h..", "48004.10022.Visit.1.Baseline.Day.0.1h..", 
"48004.10023.Visit.1.Baseline.Day.0.1h..", "48008.10001.Visit.1.Baseline.Day.0.1h..", 
"48008.10002.Visit.1.Baseline.Day.0.1h..", "48011.10001.Visit.1.Baseline.Day.0.1h..", 
"48011.10002.Visit.1.Baseline", "48012.10002.Visit.1.Baseline.Day.0.1h..", 
"48012.10004.Visit.1.Baseline.Day.0.1h..", "48012.10005.Visit.1.Baseline.Day.0.1h..", 
"49001.10001.Unscheduled.Visit.R.Day.0.7h..", "49001.10002.Visit.1.Baseline.Day.0.1h..", 
"49006.10002.Visit.1.Baseline.Day.0.1h..", "49006.10003.Visit.1.Baseline.Day.0.1h..", 
"49006.10006.Visit.1.Baseline", "49006.10007.Visit.1.Baseline.Day.0.1h..", 
"49006.10008.Visit.1.Baseline.Day.0.1h..", "49006.10009.Visit.1.Baseline.Day.0.1h..", 
"49008.10001.Visit.1.Baseline.Day.0.1h..", "49008.10002.Visit.1.Baseline.Day.0.1h..", 
"49008.10003.Visit.1.Baseline.Day.0.1h..", "49011.10001.Visit.1.Baseline.Day.0.1h..", 
"49011.10002.Visit.1.Baseline.Day.0.1h..", "49011.10003.Visit.1.Baseline.Day.0.1h..", 
"49012.10001.Visit.1.Baseline.Day.0.1h..", "49016.10002.Visit.1.Baseline.Day.0.1h..", 
"70001.10001.Visit.1.Baseline.Day.0.1h..", "70001.10002.Visit.1.Baseline.Day.0.1h..", 
"70001.10003.Visit.1.Baseline.Day.0.1h..", "70001.10004.Visit.1.Baseline.Day.0.1h..", 
"70001.10005.Visit.1.Baseline.Day.0.1h..", "70001.10006.Visit.1.Baseline.Day.0.1h..", 
"70001.10007.Visit.1.Baseline.Day.0.1h..", "70001.10008.Visit.1.Baseline.Day.0.1h..", 
"70003.10001.Visit.1.Baseline.Day.0.1h..", "70003.10002.Visit.1.Baseline.Day.0.1h..", 
"70003.10003.Visit.1.Baseline.Day.0.1h..", "70003.10004.Visit.1.Baseline.Day.0.1h..", 
"70003.10005.Visit.1.Baseline.Day.0.1h..", "70003.10006.Visit.1.Baseline.Day.0.1h..", 
"90002.10001.Visit.1.Baseline.Day.0.1h..", "90003.10001.Visit.1.Baseline.Day.0.1h..", 
"90003.10002.Visit.1.Baseline.Day.0.1h..", "90003.10003.Visit.1.Baseline.Day.0.1h..", 
"90003.10004.Visit.1.Baseline.Day.0.1h..", "90005.10001.Visit.1.Baseline.Day.0.1h..", 
"90005.10002.Visit.1.Baseline.Day.0.1h.."), class = "factor"), 
    Simp = c(0.562967424, 0.771395613, 0.720549673, 0.520301987, 
    0.498477511), Day = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "D0", class = "factor"), 
    Visit = structure(c(2L, 2L, 2L, 2L, 2L), .Label = c("U", 
    "V1"), class = "factor"), Group = structure(c(1L, 1L, 1L, 
    1L, 1L), .Label = "1_", class = "factor"), Timepoints = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = "1_D0", class = "factor"), Total.Sequence = c(16038L, 
    24250L, 13939L, 28722L, 19665L), specnumber = c(49L, 33L, 
    29L, 20L, 20L), Shan = c(1.237756795, 1.670299627, 1.617010117, 
    0.985164005, 0.960982468), TREATMENT = structure(c(2L, 1L, 
    1L, 1L, 1L), .Label = c("FIDAXOMICIN", "VANCOMYCIN"), class = "factor"), 
    SUBJID = c(3e+09, 3e+09, 3e+09, 3e+09, 3e+09), ARM = structure(c(2L, 
    1L, 1L, 1L, 1L), .Label = c("FIDAXOMICIN", "VANCOMYCIN"), class = "factor"), 
    TRT01PN = c(2L, 1L, 1L, 1L, 1L), SAFFL = structure(c(2L, 
    2L, 2L, 2L, 2L), .Label = c("N", "Y"), class = "factor"), 
    MFASFL = structure(c(2L, 2L, 2L, 2L, 2L), .Label = c("N", 
    "Y"), class = "factor"), MEASFL = structure(c(2L, 2L, 2L, 
    1L, 2L), .Label = c("N", "Y"), class = "factor"), SEX = structure(c(1L, 
    1L, 2L, 1L, 1L), .Label = c("F", "M"), class = "factor"), 
    AGE = c(86L, 76L, 60L, 83L, 85L), CRFL1 = structure(c(1L, 
    1L, 1L, 2L, 1L), .Label = c("Y", "N"), class = c("ordered", 
    "factor")), CRFL1_VF_YN = structure(c(4L, 1L, 2L, 1L, 2L), .Label = c("FIDAXOMICIN_N", 
    "FIDAXOMICIN_Y", "VANCOMYCIN_N", "VANCOMYCIN_Y"), class = "factor")), .Names = c("Datasets", 
"Simp", "Day", "Visit", "Group", "Timepoints", "Total.Sequence", 
"specnumber", "Shan", "TREATMENT", "SUBJID", "ARM", "TRT01PN", 
"SAFFL", "MFASFL", "MEASFL", "SEX", "AGE", "CRFL1", "CRFL1_VF_YN"
), row.names = c(NA, 5L), class = "data.frame")

> head(Simp_Shan_Baseline_Grp[1:5,1:20])
                                 Datasets      Simp Day Visit Group
1 30001.10001.Visit.1.Baseline.Day.0.1h.. 0.5629674  D0    V1    1_
2 30001.10002.Visit.1.Baseline.Day.0.1h.. 0.7713956  D0    V1    1_
3 30001.10003.Visit.1.Baseline.Day.0.1h.. 0.7205497  D0    V1    1_
4 30001.10004.Visit.1.Baseline.Day.0.1h.. 0.5203020  D0    V1    1_
5 30001.10005.Visit.1.Baseline.Day.0.1h.. 0.4984775  D0    V1    1_
  Timepoints Total.Sequence specnumber      Shan   TREATMENT SUBJID
1       1_D0          16038         49 1.2377568  VANCOMYCIN  3e+09
2       1_D0          24250         33 1.6702996 FIDAXOMICIN  3e+09
3       1_D0          13939         29 1.6170101 FIDAXOMICIN  3e+09
4       1_D0          28722         20 0.9851640 FIDAXOMICIN  3e+09
5       1_D0          19665         20 0.9609825 FIDAXOMICIN  3e+09
          ARM TRT01PN SAFFL MFASFL MEASFL SEX AGE CRFL1   CRFL1_VF_YN
1  VANCOMYCIN       2     Y      Y      Y   F  86     Y  VANCOMYCIN_Y
2 FIDAXOMICIN       1     Y      Y      Y   F  76     Y FIDAXOMICIN_N
3 FIDAXOMICIN       1     Y      Y      Y   M  60     Y FIDAXOMICIN_Y
4 FIDAXOMICIN       1     Y      Y      N   F  83     N FIDAXOMICIN_N
5 FIDAXOMICIN       1     Y      Y      Y   F  85     Y FIDAXOMICIN_Y

现在我试过的箱图:

ggplot(data = Simp_Shan_Baseline_Grp, aes(x=CRFL1, y=Shan)) + geom_boxplot(aes(fill=TREATMENT)) + stat_summary(fun.data = give.n, geom = "text", fun.y = median,

My output plot

正如您所看到的那样,只添加了两个因子。但我需要计算每个盒子。 进一步是的我尝试在ggplot中移动美学解决了这个问题。现在我使用了代码ggplot(data = Simp_Shan_Baseline_Grp, aes(x=CRFL1, y=Shan, colour = factor(TREATMENT))) + geom_boxplot() + stat_summary(fun.data = give.n, geom = "text", fun.y = median, position = position_dodge(width = 0.75))。 现在得到的情节看起来像: enter image description here我没有设法获得填充色框。像我以前的图像。 理想情况下,我希望每个盒子都有填充颜色和黑色文本。 有人可以帮帮我吗? 谢谢, 米特拉

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码

# load lib
library(ggplot2)
library(dplyr)

# define UDF give.n
give.n <- function(x){
  return(c(y = median(x)*1.05, label = length(x))) 
  # experiment with the multiplier to find the perfect position
}

## some playing around with the original data
## if not changed it gives a very limited output
## first select only needed columns
Simp_Shan_Baseline_Grp2 <- Simp_Shan_Baseline_Grp %>%
  select(CRFL1, Shan, TREATMENT)

## then duplicate data given, and change some values
Simp_Shan_Baseline_Grp2 <- rbind(Simp_Shan_Baseline_Grp2, Simp_Shan_Baseline_Grp2)
Simp_Shan_Baseline_Grp2$CRFL1 <- factor(rep(c("Y", "N"), 5))
Simp_Shan_Baseline_Grp2$TREATMENT[3] <- "VANCOMYCIN"
Simp_Shan_Baseline_Grp2$TREATMENT[8] <- "VANCOMYCIN" 


# shift the fill-aes to the initial ggplot call
# add position adjustment
ggplot(data = Simp_Shan_Baseline_Grp2, aes(x=CRFL1, y=Shan, fill=TREATMENT)) + 
  geom_boxplot() + 
  stat_summary(fun.data = give.n, geom = "text", fun.y = median, 
               position = position_dodge(width = .750) )

这产生以下图:

enter image description here