2016-08-11 100 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