I just fixed an issue I was having with geom_dotplot and have run into another. Namely that some of the levels have dots that are off centre from the x-axis tick marks which, whilst not a huge problem, makes my plot look a lot messier than it should.
my data is:
structure(list(ID = structure(c(14L, 16L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 10L, 10L, 13L, 14L, 15L,
16L, 16L, 19L), levels = c("A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"
), class = "factor"), value = c(2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2, 2.5, 2, 2.5, 1, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -24L), groups = structure(list(
ID = structure(c(2L, 3L, 6L, 7L, 10L, 13L, 14L, 15L, 16L,
19L), levels = c("A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"
), class = "factor"), .rows = structure(list(3:5, 6:9, 10:15,
16L, 17:18, 19L, c(1L, 20L), 21L, c(2L, 22L, 23L), 24L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))
my code is:
ggplot(data, aes(x = ID, fill=as.factor(value))) +
geom_dotplot(dotsize = 1, stackratio = 1.2,
method = "histodot",
stackgroups = TRUE) +
labs(title = "title",
x = "",
y = "",
fill = "value") +
theme_bw() +
scale_y_continuous(NULL, breaks = NULL) +
scale_x_discrete(drop = FALSE) +
theme(plot.title = element_text(size= 13),
axis.text.x = element_text(size = 10),
plot.margin = unit(c(0, 0.2, 0, 0.2), "inches"))
Here is an image of the plot:
I think that when your x-axis is discrete, a "histodot" may not be appropriate (histograms are only meaningful for numeric data). In that case, use the default method for binning (dotdensity).
ggplot(df1, aes(x=ID, fill=factor(value), group=value)) +
geom_dotplot(binpositions = "all",
dotsize = 1, stackratio = 1.2,
stackgroups = TRUE) + ...