繁体   English   中英

ggplot2:如何展示传奇

[英]ggplot2: how to show the legend

我用ggplot2做了一个简单的经典情节,这是两个图形ggplot2 但是,我正在努力展示传奇。 它没有显示传奇。 我没有使用融化和重塑方式,我只是使用经典的方式。 以下是我的代码。

df <- read.csv("testDataFrame.csv")

graph <- ggplot(df, aes(A)) + 
  geom_line(aes(y=res1), colour="1") +
  geom_point(aes(y=res1), size=5, shape=12) +
  geom_line(aes(y=res2), colour="2") +
  geom_point(aes(y=res2), size=5, shape=20) +
  scale_colour_manual(values=c("red", "green")) +
  scale_x_discrete(name="X axis") +
  scale_y_continuous(name="Y-axis") +
  ggtitle("Test") 
  #scale_shape_discrete(name  ="results",labels=c("Res1", "Res2"),solid=TRUE) 

print(graph)

数据框是:

 A,res1,res2
 1,11,25
 2,29,40
 3,40,42
 4,50,51
 5,66,61
 6,75,69
 7,85,75

有关如何显示上图的图例的任何建议?

ggplot2 ,为您设置的每个美学( aes )显示图例; groupcolourshape 要做到这一点,您必须以下列形式获取数据:

A variable value
1     res1    11
...    ...    ...
6     res1    85
7     res2    75

你可以用reshape2使用melt实现这个reshape2 (如下图所示):

require(reshape2)
require(ggplot2)

ggplot(dat = melt(df, id.var="A"), aes(x=A, y=value)) + 
      geom_line(aes(colour=variable, group=variable)) + 
      geom_point(aes(colour=variable, shape=variable, group=variable), size=4)

例如,如果您不想要点的colour ,那么只需从geom_point(aes(.))删除colour=variable 有关更多图例选项,请点击this link

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM