I am trying to create a dot plot using geom_dotplot
of ggplot2
.
However, as shown in the examples on this page, the scales of y-axis range from 0 to 1. I wonder how I can change the y-axis scale so the values reflect the actual count of the data.
A dot plot is similar to a bar graph because the height of each “bar” of dots is equal to the number of items in a particular category. To draw one, count the number of data points falling in each bin (What is a BIN in statistics?) and draw a stack of dots that number high for each bin.
binaxis. The axis to bin along, "x" (default) or "y" method. "dotdensity" (default) for dot-density binning, or "histodot" for fixed bin widths (like stat_bin) binpositions.
Create dotplots with the dotchart(x, labels=) function, where x is a numeric vector and labels is a vector of labels for each point. You can add a groups= option to designate a factor specifying how the elements of x are grouped. If so, the option gcolor= controls the color of the groups label.
There are two methods you can use to create a stacked dot plot in R: Method 1: The stripchart() function in base R. Method 2: The geom_dotplot() function in ggplot2. This tutorial provides a brief example of how to use each of these methods to produce a stacked dot plot.
Here is an example which might be helpful.
library(ggplot2)
library(ggExtra)
library(dplyr)
# use the preloaded iris package in R
irisdot <- head(iris["Petal.Length"],15)
# find the max frequency (used `dplyr` package). Here n is the label for frequency returned by count().
yheight <- max(dplyr::count(irisdot, Petal.Length)["n"])
# basic dotplot (binwidth = the accuracy of the data)
dotchart = ggplot(irisdot, aes(x=Petal.Length), dpi = 600)
binwidth = 0.1
dotsize = 1
dotchart = dotchart + geom_dotplot(binwidth=binwidth, method="histodot", dotsize = dotsize, fill="blue")
# use coor_fixed(ratio=binwidth*dotsize*max frequency) to setup the right y axis height.
dotchart = dotchart +
theme_bw() +
coord_fixed(ratio=binwidth*dotsize*yheight)
# tweak the theme a little bit
dotchart = dotchart + theme(panel.background=element_blank(),
panel.border = element_blank(),
panel.grid.minor = element_blank(),
# plot.margin=unit(c(-4,0,-4,0), "cm"),
axis.line = element_line(colour = "black"),
axis.line.y = element_blank(),
)
# add more tick mark on x axis
dotchart = dotchart + scale_x_continuous(breaks = seq(1,1.8,0.1))
# add tick mark on y axis to reflect frequencies. Note yheight is max frequency.
dotchart = dotchart + scale_y_continuous(limits=c(0, 1), expand = c(0, 0), breaks = seq(0, 1,1/yheight), labels=seq(0,yheight))
# remove x y lables and remove vertical grid lines
dotchart = dotchart + labs(x=NULL, y=NULL) + removeGridX()
dotchart
I don't know why it works. It seems that the height of y axis for geom_dotplot is 1. The ratio between x and y was setup by coor_fixed(ratio=binwidth * dotsize * max frequency).
I would recommend you to use geom_histogram
instead.
library(ggplot2)
ggplot(mtcars, aes(x = mpg)) +
geom_histogram(binwidth=1)
The issue seem to be in that geom_dotplot
cannot be converted to count, as seen in the github issue here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With