I have some data in a CSV file that I made up in order to create dot plots of different distributions.
These are the made-up data:
structure(list(uniform = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5), left_skew = c(1L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), right_skew = c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L,
2L, 2L, 1L), trunc_uni_left = c(3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), trunc_uni_right = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L), trunc_norm_left = c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), trunc_norm_right = c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), bimodal = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), extreme_left = c(3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L), extreme_right = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L)), row.names = c(NA,
-20L), class = "data.frame")
The dot-plot works when there are 'observations' in each of the five categories on the x-axis. However, if there are values missing then it only reflects those categories. For instance, in one plot there are no 1s and 2s so the plot only shows categories 3, 4, and 5.
I've tried using scale_x_discrete to set the limits and breaks but this doesn't work.
Here is the code I used to plot the data:
ggplot(df, aes(x = trunc_uni_left))+
geom_point()+
geom_dotplot(method = "histodot", binwidth = 0.25, fill = 'red', dotsize = 0.75)+
labs(x = 'Rating Categories', y = 'Rating Frequency')+
theme_bw()+
ylim(0 , 20)+
scale_x_discrete(breaks = c ("0.5", "1", "1.5", "2", "2.5"),
labels = c ("1", "2", '3', '4', '5'),
limits = c ("1", "2", "3", "4", "5"))+
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 16),
axis.title.x = element_text(size = 16, margin = margin(t = 20)),
axis.title.y = element_text(size = 16, margin = margin(r = 20)),
legend.title= element_text(size = 16))
Is there something I can do in ggplot to achieve this? Or alternatively, can I create a data frame in R that would allow me to do this?
I'm not the best coder in the world as you may be able to tell so would much appreciate the help.
Thanks!
scale_x_continuous(limits = range(as.matrix(df)))instead of the discrete x scale?