2013-04-25 121 views

回答

5

由于@joran指出淡淡的线,你可以使用geom_dotplot

require(ggplot2) 
ggplot(mtcars, aes(x = mpg)) + geom_dotplot() 

enter image description here

+1

你能解释缩放吗? x轴是装箱的,但是代表实际数据点的y轴(标签“count”会显示)?如果是这样,为什么从0到1?这是非常违反直觉 – user248237dfsf 2013-04-25 15:13:26

+3

你是对的标签“计数”这是误导,因为这实际上是一个密度估计可能是你可以建议我们改变这个标签默认为“密度”。 dotplot的ggplot实现遵循Leland Wilkinson的原始实现,所以如果你想清楚地了解它是如何工作的,请看这篇文章http://www.cs.uic.edu/~wilkinson/Publications/dots.pdf – dickoa 2013-04-25 15:39:53

+0

是否有一个简单的转换来使y轴实际上被计数,即“观察次数”? – user248237dfsf 2013-04-25 20:18:02

5

是,ggplot2确实点阵图(顺便说一句,它可能做所有你能想象的情节,看看http://docs.ggplot2.org/current/index.html)。基本上,你要的是这样的:

require(ggplot2) 
set.seed(789) 
x <- data.frame(y = sample(1:20, 100, replace = TRUE)) 
ggplot(x, aes(y)) + geom_dotplot() 

为了使它像一个简单的点阵图,我们应该这样做:

ggplot(x, aes(y)) + geom_dotplot(binwidth=1, method='histodot')  

你应该得到这样的:

first plot

要解决密度问题,您必须添加另一个术语ylim(),以便您的剧情调用的格式为ggplot() + geom_dotplot() + ylim()

更具体地说,您将编写ylim(0, A),其中A将是计算1.00密度所需的堆叠点数。在上面的例子中,你可以做的最好的是7.5个点到达0.50密度标记。从那里,你可以推断出15个点将达到1.00。

所以,你的新的呼叫看起来是这样的:

ggplot(x, aes(y)) + geom_dotplot(binwidth=1, method='histodot') + ylim(0, 15) 

,这将给你这样的:

second plot

通常情况下,这种眼球估计会为点阵图工作,当然你可以尝试其他值来微调您的比例。

注意如何改变ylim值并不影响数据的显示方式,它只是改变y轴上的标签。

+1

您可以动态编程“A”,以便您不需要设置它视力检查? – user3022875 2015-09-01 18:53:11

+0

如果你需要binwidth是除1之外的东西(例如'binwidth = z'),你仍然可以通过设置宽高比匹配来控制缩放比例:'coord_fixed(z)'。 – 2016-11-03 22:14:12

1

我用@Waldir Leoncio的后一种方法介绍了一种确切的方法。

library(ggplot2); library(grid) 

set.seed(789) 
x <- data.frame(y = sample(1:20, 100, replace = TRUE)) 

g <- ggplot(x, aes(y)) + geom_dotplot(binwidth=0.8) 
g # output to read parameter 

### calculation of width and height of panel 
grid.ls(view=TRUE, grob=FALSE) 
real_width <- convertWidth(unit(1,'npc'), 'inch', TRUE) 
real_height <- convertHeight(unit(1,'npc'), 'inch', TRUE) 

### calculation of other values 
width_coordinate_range <- diff(ggplot_build(g)$panel$ranges[[1]]$x.range) 
real_binwidth <- real_width/width_coordinate_range * 0.8 # 0.8 is the argument binwidth 
num_balls <- real_height/1.1/real_binwidth # the number of stacked balls. 1.1 is expanding value. 
    # num_balls is the value of A 

g + ylim(0, num_balls) 

enter image description here

1

道歉:我没有足够的声誉 '注释'。

我喜欢墨鱼44的“确切的方法”,但要使其工作(与ggplot2 [2.2。1)我不得不更改从以下行:

### calculation of other values 
width_coordinate_range <- diff(ggplot_build(g)$panel$ranges[[1]]$x.range) 

### calculation of other values 
width_coordinate_range <- diff(ggplot_build(g)$layout$panel_ranges[[1]]$x.range) 
+0

diff(g $ layout $ panel_params [[1]] $ x.range) – PatrickT 2017-09-23 09:48:17