我想使用ggplot绘制直方图(或使用stat_bin绘制阶梯图),并使用geom_point在其上覆盖一些点。

这是base的实现:

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)

这使:

r - 在geom_histogram或stat_bin上叠加geom_points-LMLPHP

但是,当我尝试ggplot时:
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")

点和直方图未对齐:
r - 在geom_histogram或stat_bin上叠加geom_points-LMLPHP

理想情况下,我想使用阶梯图来绘制它:
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")

看起来很像geom_histogram
r - 在geom_histogram或stat_bin上叠加geom_points-LMLPHP

但我也想让线的末端接触y = 0线。

所以我可以使用stat_bin在阶梯图中正确地获得它吗?

最佳答案

您的selected.dfy.values是使用breaks制作的,但hist()使用了另一个geom_histogram()。 (只是为了确保breaks不等于geom_histogram(bins))。此外,在阶梯图中,上下移动发生在其hist(breaks)的中间值上。 breaks(或ggplot_build(gg.obj)$data)为您提供一些信息,中断,计数等。

geom_histgram
方式基本上与plot(gg.obj)$data相同。如果您想要与base.plot相同的输出,请使用base.plot而不是breaks = h$breaks

# 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")

r - 在geom_histogram或stat_bin上叠加geom_points-LMLPHP

stat_bin
您可以使用与bars = 10相同的方式进行绘制。重要的一点是上下变动不是在休息时发生,而是在中间值时发生。
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")

r - 在geom_histogram或stat_bin上叠加geom_points-LMLPHP

关于r - 在geom_histogram或stat_bin上叠加geom_points,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39651126/

10-12 23:35