gpt4 book ai didi

r - 在 geom_histogram 或 stat_bin 上叠加 geom_points

转载 作者:行者123 更新时间:2023-12-04 21:08:47 25 4
gpt4 key购买 nike

我想用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)

这使:

enter image description here

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

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

理想情况下,我想使用阶梯图绘制它:
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
enter image description here

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

那么我是否可以使用 stat_bin 在阶梯图中正确获得?

最佳答案

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

geom_histgram
方式与base.plot基本一致.如果您想要与 base.plot 相同的输出, 请使用 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")

enter image description here

stat_bin
你可以用和 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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com