2016-08-11 102 views
-1

我想要一个点图,显示x轴上的计数。你怎么能得到下面的点阵来显示x-asix的数量?显示点坐标的x轴计数

谢谢。

date = seq(as.Date("2016/1/5"), as.Date("2016/1/11"), "day") 
    value = c(11,11,12,12,13,14,14) 
    dat =data.frame(date = date, value = value) 
    dat 
    library(ggplot2) 
    library(ggplot2) 
ggplot(dat, aes(x = value)) + geom_dotplot(binwidth = .8) + 
    scale_y_discrete(breaks= seq(1,max(table(dat$value))+2,1), 
        labels = seq(1,max(table(dat$value))+2,1)) #tried using scale_y discrete but it does nothing 
+2

我可能失去了一些东西,但你的意思是在y轴的数量? – steveb

回答

0

您可以添加coord_flip()来切换ggplot中的x和y轴。这里是您的脚本的示例:

date = seq(as.Date("2016/1/5"), as.Date("2016/1/11"), "day") 
    value = c(11,11,12,12,13,14,14) 
    dat =data.frame(date = date, value = value) 
dat 

编辑,依靠X轴:

这将给点阵图与简化的命令,而被算作x轴的标签。注意:binwidth已经从0.8变为1,以适应ylim的使用,而不是缩放。

library(ggplot2) 
    ggplot(dat, aes(x = value)) + 
    geom_dotplot(binwidth = 1) + 
    coord_flip() + 
    ylim(0,max(table(dat$value))+2) 

编辑,依靠y轴:

library(ggplot2) 
    ggplot(dat, aes(x = value)) + 
    geom_dotplot(binwidth = 1) + 
    ylim(0,max(table(dat$value))+2) 

enter image description here

+0

当我运行时,我不知道在x轴 – user3022875

+0

@ user3022875计数,那么编辑解决您的问题? – desc

+0

无需计数在y轴 – user3022875

2

ylim(0, A)给你想要的东西,其中A是堆放点的就要算1.00密度的数量。我们可以计算A的确切值(但有点复杂;对话方法可以给出近似值)。 (我下文称到post1post2post3

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 
grid.ls(view=TRUE,grob=FALSE) 
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)$panel$ranges[[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) 

# The dirty balls border probably comes from my environment. 

enter image description here