下午好,
我使用以下代码生成一个图:
ggplot() +
geom_line(data = disDataHeadItems, aes(x=disDataHeadItems$x,
y=disDataHeadItems$Freq)) +
geom_line(data = disDataLongTail, aes(x=disDataLongTail$x,
y=disDataLongTail$Freq)) +
xlab("Item id") +
ylab("# of occurrences") +
scale_x_continuous(breaks=seq(5, 75, 10), expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
geom_ribbon(aes(ymin=0, ymax=disDataHeadItems$Freq,
x=disDataHeadItems$x), fill="#CC6666") +
geom_ribbon(aes(ymin=0, ymax=disDataLongTail$Freq,
x=disDataLongTail$x), fill="#66CC99") +
geom_vline(xintercept=19, linetype="dotted")
这会创建以下情节,我非常满意:)
现在我想添加一个图例,它只是表示红色部分是顶部项目而绿色部分是数据的长尾部分。当我查阅它时,我只找到指示如何改变图例的答案。但是我的表现并没有出现。欢迎任何帮助:)
答案 0 :(得分:1)
将评论放在一起并添加一些随机数据帧结果:
library(ggplot2)
disDataHeadItems <- data.frame(
x = runif(100,1,100),
Freq = runif(100,5,10)
)
disDataLongTail <- data.frame(
x = runif(100,1,100),
Freq = runif(100,5,10)
)
ggplot() +
geom_line(data = disDataHeadItems, aes(x=x,
y=Freq)) +
geom_line(data = disDataLongTail, aes(x=x,
y=Freq)) +
xlab("Item id") +
ylab("# of occurrences") +
scale_x_continuous(breaks=seq(5, 75, 10), expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
geom_ribbon(aes(ymin=0, ymax=disDataHeadItems$Freq,
x=disDataHeadItems$x, fill="#CC6666")) +
geom_ribbon(aes(ymin=0, ymax=disDataLongTail$Freq,
x=disDataLongTail$x, fill="#66CC99")) +
geom_vline(xintercept=19, linetype="dotted")