2014-05-23 29 views
2

我搜索了,但我找不到答案。我想用geom_bin2d進一步處理我在R中創建的情節的數據。我已經使用如何提取R中間隔的端點?

> library(ggplot2) 
> my_plot <- ggplot(diamonds, aes(x = x, y = y))+ geom_bin2d(bins=3) 
> plot_data <- ggplot_build(my_plot) 
> data <- plot_data$data[[1]] 
> data$xbin[[1]] 
[1] [0,3.58] 
Levels: [0,3.58] (3.58,7.16] (7.16,10.7] (10.7,14.3] 

沒有我想的工作,包括minmean提取從這樣的情節倉(間隔)。我如何訪問像data$xbin[[1]]這樣的間隔的端點?

(更新:我把到例如基於內置的數據集的完整測試情況)

+2

考慮提供一個可重複的例子。 –

+1

['cut'中的最後一個例子](https://stat.ethz.ch/pipermail/r-help/2008-August/170445.html) – baptiste

回答

2

喜歡的東西

library(stringr) 
x <- cut(seq(1:5), breaks = 2) 
as.numeric(unlist(str_extract_all(as.character(x[1]), "\\d+\\.*\\d*"))) 

或您例如

my_plot <- ggplot(diamonds, aes(x = x, y = y))+ geom_bin2d(bins=3) 
plot_data <- ggplot_build(my_plot) 
data <- plot_data$data[[1]] 
x <- data$xbin[[1]] 
as.numeric(unlist(str_extract_all(as.character(x), "\\d+\\.*\\d*")))[2] 
3.58