r - How to overlay geom_bar and geom_line plots with different number of elements using ggplot2? -
assuming have 2 data.frames different data in same range of x-values
a <-data.frame(x=c(1,1,1,2,2,2,3,3,3), y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2), z=c("do","re","mi","do","re","mi","do","re","mi")) b <- data.frame(x=c(1,2,3),y=c(10,15,8))
both, , b have same range of x values (1,2,3) while data.frame 9 rows, b data.frame 3 rows.
i use geom_bar in order plot distribution of values of a, this:
ggplot(a, aes(x=x, y=y, fill=z)) + geom_bar(position="stack",stat="identity") + ylab("") + xlab("x")
and use geom_line plot b data, this:
ggplot(b, aes(x=x, y=y)) + geom_line(stat="identity") + ylab("") + xlab("x") + ylim(0,15)
now overlay geom_line plot previous geom_bar plot. first try following:
ggplot(a, aes(x=x, y=y, fill=z)) + geom_bar(position="stack",stat="identity") + ylab("") + xlab("x") + ggplot(b, aes(x=x, y=y)) + geom_line(stat="identity") + ylab("") + xlab("x") + ylim(0,15)
with no success.
how can overlay geom_line plot geom_bar plot?
try this
p <- ggplot() p <- p + geom_bar(data = a, aes(x=x, y=y, fill=z), position="stack",stat="identity") p <- p + geom_line(data = b, aes(x=x, y=y/max(y)), stat="identity") p
update: can rescale 1 y make them same. don't know relations between 2 ys, rescaled them using y/max(y)
. solve problem?
Comments
Post a Comment