我正在尝试在 x 轴上显示点图的 计数,如下所述:在 x 轴上显示点图的计数
library(ggplot2)
library(grid)
date = seq(as.Date("2016/1/5"), as.Date("2016/1/12"), "day")
value = c(11,11,12,12,13,14,14,14)
dat =data.frame(date = date, value = value)
### base plot
g <- ggplot(dat, aes(x = value)) + geom_dotplot(binwidth = 0.8) + coord_flip()
g # output to read parameter
### calculation of width and height of panel
current.vpTree()
seekViewport('panel.3-4-3-4')
real_width <- convertWidth(unit(1,'npc'), 'inch', TRUE)
real_height <- convertHeight(unit(1,'npc'), 'inch', TRUE)
### calculation of other values
height_coordinate_range <-diff(ggplot_build(g)$layout$panel_params[[1]]$y.range)
real_binwidth <- real_height / height_coordinate_range * 0.8 # 0.8 is the argument binwidth
num_balls <- real_width / 1.1 / real_binwidth # the number of stacked balls. 1.1 is expanding value.
g + ylim(0, num_balls)
但是,在我看来,这real_width
是指面板的宽度,而不是整个情节。当我使用时,这会导致计数刻度和点之间的错位:
ggsave(g,
filename = "g.png",
path = getwd(),
device = "png",
height = real_height,
width = real_width,
units = "cm")
鉴于我想要一个 6cm x 6cm 的图,我怎样才能找出面板的尺寸,以便我可以使用面板尺寸来计算num_balls
?