簡體   English   中英

如何在頻率圖的 y 軸上顯示刻度?

[英]How to show ticks on y axis on frequency plot?

我有一個這樣的代碼,它產生附加的情節。 我的問題是如何為 x 軸上的值顯示 y 軸上的確切值,即 1 和 2。另外,我將如何更改 x 和 y 軸上的文本? 以及如何使 y 軸達到 10000。

library(data.table)
library(dplyr)
a=fread("ukb_all_add.txt")
a=select(a,V3,V12)
a=na.omit(a)
colnames(a)=c("rs","pvalue")

# A blank data frame to hold the data you want to plot
plotData = data.frame(
     n = as.integer(),
     sample = as.character(),
     propfdr = as.double()
)

# Loop through 10000 times as an example
for (n in 1: 10000) {
     # create 'b' as a list of 30 points to take from a
     b <- sample ( 1 : length (a$rs), 30, replace = F )
     # put the 30 values of interest from a into c

     c <- a[ b , ]

     # do your stats
     c$fdr <- p.adjust ( c$pvalue, method = "BH" )

     # capture what you did
     mySample <- paste(b, collapse= ",")
     thisRow <- data.frame(
         n = as.integer(n),
         sample = as.character(mySample),
         fdrlt5 = as.integer(length(c$fdr[c$fdr<0.05]))
     )

     # merge this row into the data
     plotData <- rbind(
         plotData,
         thisRow
     )
}

hist(plotData$fdrlt5)

在此處輸入圖片說明

輸出:

dput(hist(plotData$fdrlt5))

structure(list(breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 
1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3), counts = c(9543L, 0L, 0L, 
0L, 419L, 0L, 0L, 0L, 0L, 33L, 0L, 0L, 0L, 0L, 5L), density = c(4.7715, 
0, 0, 0, 0.2095, 0, 0, 0, 0, 0.0165, 0, 0, 0, 0, 0.0025), mids = c(0.1, 
0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 
2.9), xname = "plotData$fdrlt5", equidist = TRUE), class = "histogram")
List of 6
 $ breaks  : num [1:16] 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 ...
 $ counts  : int [1:15] 9543 0 0 0 419 0 0 0 0 33 ...
 $ density : num [1:15] 4.77 0 0 0 0.21 ...
 $ mids    : num [1:15] 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 ...
 $ xname   : chr "plotData$fdrlt5"
 $ equidist: logi TRUE
 - attr(*, "class")= chr "histogram"

hist函數返回足夠的信息來做到這一點。

h <- hist(mtcars$disp)
str(h)
# List of 6
#  $ breaks  : int [1:10] 50 100 150 200 250 300 350 400 450 500
#  $ counts  : int [1:9] 5 7 4 1 4 4 4 1 2
#  $ density : num [1:9] 0.003125 0.004375 0.0025 0.000625 0.0025 ...
#  $ mids    : num [1:9] 75 125 175 225 275 325 375 425 475
#  $ xname   : chr "mtcars$disp"
#  $ equidist: logi TRUE
#  - attr(*, "class")= chr "histogram"

因此,您可以使用$mids作為 x 值(每個條形的中點)和$counts作為 y 值。

text(h$mids, h$counts, h$counts, adj=c(0.5, -0.5))

第一個帶有文本剪輯的直方圖

但是,正如您所看到的,這確實允許剪切某些標簽(取決於數據)。 這可以通過首先確定 y 限制應該是什么,然后用正確的限制進行繪圖來緩解。

h <- hist(mtcars$disp, plot = FALSE)
range(h$counts)
# [1] 1 7
h <- hist(mtcars$disp, ylim = c(0, 8))
text(h$mids, h$counts, h$counts, adj=c(0.5, -0.5))

沒有剪裁的第二個直方圖

然后解決您的其余問題:

  • 將 y 軸更改為 10000(我將在此處使用“10”):

     h <- hist(mtcars$disp, ylim = c(0, 10), yaxt = "n") axis(2, at = seq(0, 10, by = 2), labels = seq(0, 10, by = 2), las = 1)

    (我添加了las = 1來旋轉軸標簽。總的來說,我認為這樣旋轉數字會很好,但是當所有軸標簽沒有足夠的空間時,R 有時也會丟棄軸標簽,所以它可以是更重要。減輕這種情況的方法包括las=cex.axis=0.9或一些更低的正數。)

  • 只是一些列?

     text(h$mids[1:2], h$counts[1:2], h$counts[1:2], adj=c(0.5, -0.5))

擴展直方圖


我不知道為什么你的控制台上的事情對你來說是壞事。 我將使用hist的輸出將直方圖重新生成為手動條形圖。 這並不理想,但它對我有用。 (我將 y 軸增加到 11000 僅僅是因為 SO 的這個小圖的尺寸,一般不需要它。)

h2 <- hist(plotData$fdrlt5, plot = FALSE)
plot(NA, type='n', xlim=range(h2$mids)+c(-0.1,0.1),
     ylim=c(0,11000), frame.plot=F, xlab="Proportion", ylab="Frequency", yaxt="n")
axis(2, at = seq(0, 10000, by = 1000), labels = seq(0, 10000, by = 1000), las=1)
rect(h2$mids-0.1, 0, h2$mids+0.1, h2$counts)
text(h2$mids[1:2], h2$counts[1:2], h2$counts[1:2], adj=c(0.5, -0.5))

手動條形圖直方圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM