Reputation: 191
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.
Upvotes: 11
Views: 8757
Reputation: 431
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).
Upvotes: 5