覆盖geom_histogram或stat_bin上的geom_points

时间:2016-09-23 01:18:27

标签: r ggplot2

我想使用 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if text == "\n" { // Line added lineCount += 1 if lineCount == 5 { print("No more! lineCount at \(lineCount)") lineCount -= 1 textView.deleteBackward() } } else if !text.isEmpty { // Character added descCount += 1 } else if range.length == 1 && textView.text.characters.last == "\n" { // Line deleted lineCount -= 1 } else if text == "" && descCount != 0 { // Character deleted descCount -= 1 } let newLength = descCount + text.characters.count - range.length - lineCount // text.characters.count counts /n characters return newLength <= 202 // Arbitrary number } 绘制直方图(或使用ggplot绘制一个步骤图)并使用stat_bin在其上叠加几个点。

这是geom_point实施:

base

给出:

enter image description here

但是,当我尝试library(plotrix) set.seed(10) df <- data.frame(id=LETTERS,val=rnorm(length(LETTERS))) selected.ids <- sample(LETTERS,3,replace=F) h <- hist(df$val,plot=F,breaks=10) cols <- sapply(rainbow(length(selected.ids)),function(x) color.id(x)[1]) selected.df <- data.frame(id=selected.ids,col=cols,stringsAsFactors=F) selected.df$x <- df$val[which(df$id %in% selected.ids)] selected.df <- selected.df[order(selected.df$x),] selected.df$y <- h$counts[findInterval(selected.df$x,h$breaks)] selected.df$col <- factor(selected.df$col,levels=cols) plot(h) segments(x0=selected.df$x,x1=selected.df$x,y0=selected.df$y,y1=selected.df$y,cex=18,lwd=8,col=selected.df$col) 时:

ggplot

点和直方图未对齐: enter image description here

理想情况下,我想用步骤图绘制它:

ggplot(df,aes(x=val))+geom_histogram(bins=10,colour="black",alpha=0,fill="#FF6666")+geom_point(data=selected.df,aes(x=x,y=y,colour=factor(col)),size=2)+scale_fill_manual(values=levels(selected.df$col),labels=selected.df$id,name="id")+scale_colour_manual(values=levels(selected.df$col),labels=selected.df$id,name="id")

这看起来非常像ggplot(df,aes(x=val))+stat_bin(geom="step",bins=10)+geom_point(data=selected.df,aes(x=x,y=y,colour=factor(col)),size=2)+scale_fill_manual(values=levels(selected.df$col),labels=selected.df$id,name="id")+scale_colour_manual(values=levels(selected.df$col),labels=selected.df$id,name="id")

enter image description here

但我也希望线的两端接触y = 0线。

所以我使用stat_bin?

在步骤图中正确获取

1 个答案:

答案 0 :(得分:2)

selected.df y.values breaks hist()使用了geom_histogram() breaks,但geom_histogram(bins)使用了另一个hist(breaks)。 (breaks并不等同于ggplot_build(gg.obj)$data。此外,在步骤图中,向上发生在其plot(gg.obj)$data的中间值上。 base.plot(或base.plot)会为您提供一些信息,休息,计数等。

<强> geom_histgram
方式与breaks = h$breaks基本相同。如果您希望输出与bars = 10相同,请使用# a common part to base and ggplot2 library(plotrix) set.seed(10) df <- data.frame(id = LETTERS, val = rnorm(length(LETTERS))) selected.ids <- sample(LETTERS, 3, replace = F) cols <- sapply(rainbow(length(selected.ids)), function(x) color.id(x)[1]) selected.df <- data.frame(id=selected.ids, col=cols, stringsAsFactors = F) selected.df$x <- df$val[which(df$id %in% selected.ids)] selected.df <- selected.df[order(selected.df$x),] selected.df$col <- factor(selected.df$col, levels=cols) 代替# (1) make a histogram g <- ggplot(df, aes(x = val)) + geom_histogram(bins = 10, colour = "black", alpha = 0, fill = "#FF6666") # base; h <- hist(df$val, plot = F, breaks = 10) # (2) get its breaks g.data <- ggplot_build(g)$data[[1]] g.breaks <- c(g.data$xmin, tail(g.data$xmax, n=1)) # base; h$breaks # (3) get counts of specific x values selected.df$y <- g.data$count[findInterval(selected.df$x, g.breaks)] # base; selected.df$y <- h$counts[findInterval(selected.df$x,h$breaks)] # (4) draw g + geom_point(data = selected.df, aes(x = x, y = y, colour = factor(col)), size = 2) + scale_fill_manual(values = levels(selected.df$col), labels = selected.df$id,name = "id") + scale_colour_manual(values = levels(selected.df$col), labels = selected.df$id, name = "id")

geom_histgram

selected.df2 <- selected.df

# (1) make a step plot
s <- ggplot(df, aes(x = val)) + stat_bin(geom = "step", bins = 10)

# (2) get breaks and its middle values
s.data <- ggplot_build(s)$data[[1]]
s.breaks <- c(s.data$xmin, tail(s.data$xmax, n=1))
s.mid.breaks <- s.data$x

# (3) get counts of specific x values using middle values of breaks.
selected.df2$y <- s.data$count[findInterval(selected.df2$x, s.mid.breaks)]

# (4) add a new levels into breaks to start and end at y=0
s.add.breaks <- c(s.breaks[1] - 1.0E-6,    # making lower levels is easy
                  s.breaks, 
                  tail(s.breaks, n=1) + diff(s.breaks[1:2])) # upper need the same range

# (5) draw
ggplot(df, aes(x = val)) + stat_bin(geom = "step", breaks = s.add.breaks) +
  geom_point(data = selected.df2, aes(x = x, y = y, colour = factor(col)), size = 2) +
  scale_fill_manual(values = levels(selected.df2$col), labels = selected.df2$id, name = "id") + 
  scale_colour_manual(values = levels(selected.df2$col), labels = selected.df2$id, name="id")

enter image description here

<强> stat_bin
您可以使用与ng-xi18n相同的方式绘制它。重要的一点是上下不是在休息时间而是中间值。

@import '.+?';

enter image description here