我的目的是用 ggplot2 (作者:Hadley Wickham)重现这个图[ref] .
这是我基于 geom_point 的努力和一些丑陋的数据准备(参见下面的代码):
How could I do that with geom_dotplot()?
在我的尝试中,我遇到了几个问题:(1)将geom_dotplot生成的默认密度映射到计数,(2)将轴切断,(3)没有意外的漏洞 . 我放弃了并且砍掉了 geom_point() .
我期望(并且仍然希望)它会如此简单
ggplot(data, aes(x,y)) + geom_dotplot(stat = "identity")
但不是 . 所以这就是我尝试过的和输出:
# Data
df
# dotplot based on geom_dotplot
geom_dots
require(ggplot2)
n = sum(count) # total number of dots to be drawn
b = round*round(n/round) # prettify breaks
x = rep(x, count) # make x coordinates for dots
if (is.null(breaks)) breaks = seq(0, 1, b/4/n)
ggplot(data.frame(x = x), aes(x = x)) +
geom_dotplot(method = "histodot", ...) +
scale_y_continuous(breaks = breaks,
#limits = c(0, max(count)+1), # doesn't work
labels = breaks * n)
}
geom_dots(x = df$x, count = df$y)
# dotplot based on geom_point
ggplot_dot
require(ggplot2)
message("The count variable must be an integer")
count = as.integer(count) # make sure these are counts
n = sum(count) # total number of dots to be drawn
x = rep(x, count) # make x coordinates for dots
count = count[count > 0] # drop zero cases
y = integer(0) # initialize y coordinates for dots
for (i in seq_along(count))
y
ggplot(data.frame(x = x, y = y), aes(x = x, y = y)) +
geom_point(...) # draw one dot per positive count
}
ggplot_dot(x = df$x, count = df$y,
size = 11, shape = 21, fill = "orange", color = "black") + theme_gray(base_size = 18)
# ggsave("dotplot.png")
ggsave("dotplot.png", width = 12, height = 5.9)
简短随机评论:使用 geom_point() 解决方案,保存绘图包括调整尺寸恰好以确保点接触(点大小和绘图高度/宽度) . 使用 geom_dotplot() 解决方案,我将标签四舍五入以使它们更漂亮 . 不幸的是,我无法在大约100处切断轴:使用 limits() 或 coord_cartesian() 会导致整个绘图的重新缩放而不是切割 . 另请注意,要使用 geom_dotplot() 我根据计数创建了一个数据向量,因为我无法直接使用count变量(我希望 stat="identity" 能够做到这一点,但我无法使其工作) .