11

を使用してggplot2、Xより上のすべてのものが最終的なビンにグループ化されるヒストグラムを作成したいと思います。たとえば、私の分布の大部分が100から200の間で、10ずつビニングしたい場合、200を超えるものはすべて「200+」でビニングしたいと思います。

# create some fake data    
id <- sample(1:100000, 10000, rep=T)
visits <- sample(1:1200,10000, rep=T)

#merge to create a dataframe
df <- data.frame(cbind(id,visits))

#plot the data
hist <- ggplot(df, aes(x=visits)) + geom_histogram(binwidth=50)

制限したいデータを表現しながら、X軸を制限するにはどうすればよいですか?

4

2 に答える 2

6

breaksおそらく、次の引数を探しているでしょうgeom_histogram:

# create some fake data    
id <- sample(1:100000, 10000, rep=T)
visits <- sample(1:1200,10000, rep=T)

#merge to create a dataframe
df <- data.frame(cbind(id,visits))

#plot the data
require(ggplot2)
ggplot(df, aes(x=visits)) +
  geom_histogram(breaks=c(seq(0, 200, by=10), max(visits)), position = "identity") +
  coord_cartesian(xlim=c(0,210))

これは次のようになります (ここでは偽のデータがかなり悪く見え、ブレークに一致するように軸も調整する必要があることに注意してください):

ヒストグラムの手動ブレーク

編集:

たぶん、他の誰かがここで検討することができます:

# create breaks and labels
brks <- c(seq(0, 200, by=10), max(visits))
lbls <- c(as.character(seq(0, 190, by=10)), "200+", "")
# true
length(brks)==length(lbls)

# hmmm
ggplot(df, aes(x=visits)) +
  geom_histogram(breaks=brks, position = "identity") +
  coord_cartesian(xlim=c(0,220)) +
  scale_x_continuous(labels=lbls)

プロット エラー:

Error in scale_labels.continuous(scale) : 
  Breaks and labels are different lengths

このように見えますが、8 か月前に修正されました。

于 2012-07-23T17:35:42.007 に答える