尝试这个:
mydf <- melt(mydf,id.var = 1)
mydf$variable <- factor(mydf$variable,levels = rev(myvals$variable[order(myvals$value)]),ordered = TRUE)
# plot multiple lines
p <- ggplot(data = mydf) +
geom_line(aes(x = mydate,y = value,colour = variable,group = variable),size = 1) +
scale_colour_manual("", values = c("aaa" = "red", "bbb" = "black", "ccc" = "blue", "ddd" = "green")) +
scale_y_continuous(label = percent_format()) +
geom_text(data = myvals, aes(x = mydate + 30, y = value, label = sprintf("%+1.1f%%", myvals$value * 100)),
size = 4, colour = "grey50") +
opts(axis.title.y = theme_blank()) +
opts()
# and output
print(p)
我融化了你的完整数据集,为你节省了几行用于绘制代码的代码。像往常一样,关键是确保变量是有序因子。
为了解决评论中出现的问题,您可以传递您喜欢在图例本身中显示的任何标签,只要顺序正确即可:
ggplot(data = mydf) +
geom_line(aes(x = mydate,y = value,colour = variable,group = variable),size = 1) +
scale_colour_manual("", values = c("aaa" = "red", "bbb" = "black", "ccc" = "blue", "ddd" = "green"),labels = c('Company D','Company A','Company C','Company B')) +
scale_y_continuous(label = percent_format()) +
geom_text(data = myvals, aes(x = mydate + 30, y = value, label = sprintf("%+1.1f%%", myvals$value * 100)),
size = 4, colour = "grey50") +
opts(axis.title.y = theme_blank()) +
opts()
Note:从0.9.2版本开始opts
已经replaced http://docs.ggplot2.org/current/theme.html by theme
, e.g.:
+ theme(axis.title.y = element_blank())