使用 R 的标准情节或更好的 GGPLOT,有没有办法创建这样的情节?请特别注意选定条上带有星号的水平线。
问问题
1534 次
1 回答
1
I don't know of an easy way to annotate graphs like this in ggplot2
. Here's a relatively generic approach to make the data you'd need to plot. You can use a similar approach to annotate the relationships as necessary. I'll use the iris
dataset as an example:
library(ggplot2)
library(plyr) #for summarizing data
#summarize average sepal length by species
dat <- ddply(iris, "Species", summarize, length = mean(Sepal.Length))
#Create the data you'll need to plot for the horizontal lines
horzlines <- data.frame(x = 1,
xend = seq_along(dat$Species)[-1],
y = seq(from = max(dat$length), by = 0.5, length.out = length(unique(dat$Species))-1),
yend = seq(from = max(dat$length), by = 0.5, length.out = length(unique(dat$Species))-1),
label = c("foo", "bar")
)
ggplot() +
geom_histogram(data = dat, aes(Species, length), stat = "identity") +
geom_segment(data = horzlines, aes(x = x, xend = xend, y = y, yend = yend)) +
geom_text(data = horzlines, aes(x = (x + xend)/2, y = y + .25, label = label))
Giving you something like this:
于 2013-10-23T03:25:54.723 回答