I'm struggling to get polar_coords to work as I had hoped. I want each item to be represented by a coloured track, with a range of 1:50000. I then wanted to plot points over these tracks at the corresponding locations, with symbols representing the different categories. The points would then be annotated with the id.
Dataframe:
structure(list(item = structure(c(1L, 2L, 2L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L), .Label = c("AA", "AB", "AC", "AD", "AE",
"BA", "BB", "BC", "BD", "BE"), class = "factor"), location = c(10045L,
12041L, 15035L, 22054L, 19023L, 49411L, 39012L, 3041L, 23065L,
33015L, 42069L, 26859L), category = structure(c(1L, 1L, 2L, 3L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("X", "Y", "Z"), class = "factor"),
id = structure(c(1L, 8L, 2L, 7L, 6L, 10L, 5L, 1L, 1L, 3L,
4L, 9L), .Label = c("Apple", "Banana", "Cherry", "Grape",
"Mango", "Melon", "Orange", "Pear", "Raspberry", "Strawberry"
), class = "factor")), .Names = c("item", "location", "category",
"id"), class = "data.frame", row.names = c(NA, -12L))
my_data %>%
ggplot(aes(item, location, shape = category, label = id)) +
geom_col(aes(y = Inf), fill = "gray80") +
geom_point(size = 3) +
geom_text(vjust = -1) +
scale_x_discrete(expand = expand_scale(add = c(5,0))) +
coord_polar(theta = "y") +
theme_void()
If you want a break in the middle, you could change the item to a numeric value relating to it's desired position:
my_data %>%
mutate(item_pos = as.numeric(item),
item_pos = item_pos + if_else(item_pos > 5, 1, 0)) %>%
ggplot(aes(item_pos, location, shape = category, label = id)) +
...
Maybe you can work from this:
ggplot(data,aes(x=location, color=id, y=id)) +
geom_linerange(aes(y=id, xmin=0, xmax=50000, color=category), size=2, alpha=0.5) +
geom_point(size=3) +
coord_polar()
I have already seen another similar question in : Add number of observations per group in ggplot2 boxplot
But this solution doesn't work in my plot as I have two-factors boxplots.
This is what I tried:
Here is my data:
> 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
Now the boxplot I tried:
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,
As you see only two factors as Y/N has been added. But I need count for each box.
Further Yes I have tried moving the aesthetics in ggplot solves the problem. Now I have used the code 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)).
Now the resulting plot looks like:
enter image description here I don't manage to get fill colour boxes. Like my previous image.
Ideally I like to have fill colour and text in black for each box.
Can anybody please help me?
Thank you,
Mitra
You could use the following code
# 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) )
This yields the following plot:
I reformulated my question and hope it is a bit clearer now:
Here is my data:
ID Type X Y Sex
a1 Test -12.12609861 208.6810478 XY
a2 Test -1.32366642 63.0574351 XXY
a3 Test -9.02867948 114.1501293 XY
b4 NewTest 0.01101428 0.87207664 XX
b5 Test -1.14651604 -0.86714741 XX
b6 Test -13.05848944 155.5109551 XY
x7 NewTest -4.74479593 80.82528931 XY
x8 Test -8.17386444 124.4765311 XY
x9 Test 1.14870262 -0.36606683 XX
x10 Test 1.20879037 0.80972607 XX
x11 Test -1.04261274 0.35654895 XX
x12 Test -11.73602 185.5326725 XY
I would like to plot the data with different color according to whether the data is new or old.
The new data is added daily or weekly so the color change needs to be dynamic. N.B the new data always start with "newTest" in the column "TYPE"
The code:
for_loop_start<- (nrow(whole_data)-1)
len_of_whole_data<- nrow(whole_data)
for (j in c(for_loop_start:1)){
if (whole_data[j,2] == "NewTest"){
break
}
}
new_data <- with(whole_data,whole_data[j:len_of_whole_data,])
> p <- ggplot(data=whole_data,aes(x=X,y=Y)) + geom_point(colour = "black")
> ggplotly(p)
> p <- p + geom_point(data= new_data,
mapping=aes(x=X,y=Y,text=SampleID,colour = "darkgoldenrod2"))
> ggplotly(p)
Answer to edited version of the question
If the last "NewType" value in your "Type" column consistently starts "new data", this should work:
dat <- structure(list(ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 10L,
11L, 12L, 7L, 8L, 9L), .Label = c("a1", "a2", "a3", "b4", "b5",
"b6", "x10", "x11", "x12", "x7", "x8", "x9"), class = "factor"),
Type = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L), .Label = c("NewTest", "Test"), class = "factor"),
X = c(-12.12609861, -1.32366642, -9.02867948, 0.01101428,
-1.14651604, -13.05848944, -4.74479593, -8.17386444, 1.14870262,
1.20879037, -1.04261274, -11.73602), Y = c(208.6810478, 63.0574351,
114.1501293, 0.87207664, -0.86714741, 155.5109551, 80.82528931,
124.4765311, -0.36606683, 0.80972607, 0.35654895, 185.5326725
), Sex = structure(c(3L, 2L, 3L, 1L, 1L, 3L, 3L, 3L, 1L,
1L, 1L, 3L), .Label = c("XX", "XXY", "XY"), class = "factor")), .Names = c("ID",
"Type", "X", "Y", "Sex"), class = "data.frame", row.names = c(NA,
-12L))
lim.id <- max(which(dat$Type == "NewTest")) - 1
dat$Age <- c(rep("old", lim.id), rep("new", nrow(dat) - lim.id))
ggplot(dat, aes(x=X, y=Y, color = Age)) + geom_point() +
scale_color_manual(values = c("darkgoldenrod2", "black"))
Old answer
You could try to make a script that reads the modification time (see ?file.mtime) and use that to make a column, which specifies whether the entry is "new" or "old"
dat <- structure(list(ID = 1:12, Type = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("control", "Test"
), class = "factor"), X = c(-12.12609861, -1.32366642, -9.02867948,
0.01101428, -1.14651604, -13.05848944, -4.74479593, -8.17386444,
1.14870262, 1.20879037, -1.04261274, -11.73602), Y = c(208.6810478,
63.0574351, 114.1501293, 0.87207664, -0.86714741, 155.5109551,
80.82528931, 124.4765311, -0.36606683, 0.80972607, 0.35654895,
185.5326725), Sex = structure(c(3L, 2L, 3L, 1L, 1L, 3L, 3L, 3L,
1L, 1L, 1L, 3L), .Label = c("XX", "XXY", "XY"), class = "factor")), .Names = c("ID",
"Type", "X", "Y", "Sex"), class = "data.frame", row.names = c(NA,
-12L))
dat$Time <- seq(as.Date("2017-07-12"), as.Date("2017-06-12"), length = nrow(dat))
dat$Time.type <- ifelse(as.Date(Sys.time()) - dat$Time < 12, "new", "old")
library(ggplot2)
ggplot(dat, aes(x=X, y=Y, color = Time.type)) + geom_point() +
scale_color_manual(values = c("black", "darkgoldenrod2"))
you could also just set a variable which defines on which ID to split your dataframe for plotting (supposing df1 is your dataframe):
lim.id <- 7 #here you can put whatever value you would like to split your data.frame on
plot1 <- ggplot() +
geom_point(data = df1[df1$ID < lim.id, ], aes(x = X, y = Y), colour = "black")
plot1 <- plot1 +
geom_point(data = df1[df1$ID >= lim.id, ], aes(x = X, y = Y), colour = "darkgoldenrod2")
plot2 <- ggplotly(plot2)